Advertisement
Vince14

What's Up With Gravity

Feb 25th, 2023 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. using namespace std;
  16. #define pii pair<int, int>
  17. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  18. const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  19. const int dl[2] = {1, -1};
  20. const int MOD = 1e9;
  21. const int MAX = 505;
  22.  
  23.  
  24. int n, m;
  25. int world[MAX][MAX]; // 1 = Blocked, 0 = Empty, 2 = Start, 3 = End;
  26. int dist[MAX][MAX][2];
  27. pii spos, epos;
  28. pii fall[MAX][MAX][2];
  29.  
  30. void print_world(){
  31.     for(int i = 0; i < n; i++){
  32.         for(int j = 0; j < m; j++){
  33.             cout << world[i][j];
  34.         }
  35.         cout << "\n";
  36.     }
  37. }
  38.  
  39. void print_dist(){
  40.     for(int i = 0; i < n; i++){
  41.         for(int j = 0; j < m; j++){
  42.             cout << dist[i][j][0] << " ";
  43.         }
  44.         cout << "\n";
  45.     }
  46.     for(int i = 0; i < n; i++){
  47.         for(int j = 0; j < m; j++){
  48.             cout << dist[i][j][1] << " ";
  49.         }
  50.         cout << "\n";
  51.     }
  52. }
  53.  
  54. void input(){
  55.     for(int i = 0; i < n; i++){
  56.         string s;
  57.         cin >> s;
  58.         for(int j = 0; j < s.size(); j++){
  59.             int tmp;
  60.             if(s[j] == '#'){
  61.                 tmp = 1;
  62.             }
  63.             else if(s[j] == 'C'){
  64.                 tmp = 2;
  65.                 spos = make_pair(i, j);
  66.             }
  67.             else if(s[j] == 'D'){
  68.                 tmp = 3;
  69.                 epos = make_pair(i, j);
  70.             }
  71.             else{
  72.                 tmp = 0;
  73.             }
  74.             world[i][j] = tmp;
  75.         }
  76.     }
  77. }
  78.  
  79. // gravity: 0 = inc, 1 = dec
  80. // (-1, -1) = dead;
  81.  
  82. void init_gravity(){
  83.     for(int i = 0; i < n; i++){
  84.         for(int j = 0; j < m; j++){
  85.             fall[i][j][0] = make_pair(-2, -2);
  86.             fall[i][j][1] = make_pair(-2, -2);
  87.         }
  88.     }
  89. }
  90.  
  91. pii check_gravity(int x, int y, int gravity){
  92.     if(fall[x][y][gravity] != make_pair(-2, -2)){
  93.         return fall[x][y][gravity];
  94.     }
  95.     if(gravity == 0){
  96.         if(x == n - 1){
  97.             return fall[x][y][gravity] = make_pair(-1, -1);
  98.         }
  99.         else if(world[x + 1][y] != 1){
  100.             return fall[x][y][gravity] = check_gravity(x + 1, y, gravity);
  101.         }
  102.         else{
  103.             return fall[x][y][gravity] = make_pair(x, y);
  104.         }
  105.     }
  106.     else if(gravity == 1){
  107.         if(x == 0){
  108.             return fall[x][y][gravity] = make_pair(-1, -1);
  109.         }
  110.         else if(world[x - 1][y] != 1){
  111.             return fall[x][y][gravity] = check_gravity(x - 1, y, gravity);
  112.         }
  113.         else{
  114.             return fall[x][y][gravity] = make_pair(x, y);
  115.         }
  116.     }
  117. }
  118.  
  119. void init_fall(){
  120.     for(int i = 0; i < n; i++){
  121.         for(int j = 0; j < m; j++){
  122.             if(world[i][j] != 1){
  123.                 check_gravity(i, j, 0);
  124.                 check_gravity(i, j, 1);
  125.             }
  126.         }
  127.     }
  128. }
  129.  
  130. void print_fall(){
  131.     for(int i = 0; i < n; i++){
  132.         for(int j = 0; j < m; j++){
  133.             cout << "Gravity of: " << i << " " << j << "\n";
  134.             cout << "Down: " << fall[i][j][0].first << " " << fall[i][j][0].second << "\n";
  135.             cout << "Up: " << fall[i][j][1].first << " " << fall[i][j][1].second << "\n";
  136.         }
  137.     }
  138. }
  139.  
  140. void init_dist(){
  141.     for(int i = 0; i < n; i++){
  142.         for(int j = 0; j < m; j++){
  143.             dist[i][j][0] = -1;
  144.             dist[i][j][1] = -1;
  145.         }
  146.     }
  147. }
  148.  
  149. void BFS(){
  150.     deque<pair<pii, pii>> q; // coords, gravity
  151.     q.push_front(make_pair(spos, make_pair(0, 0)));
  152.     while(!q.empty()){
  153.         int x = q.front().first.first;
  154.         int y = q.front().first.second;
  155.         int g = q.front().second.first;
  156.         int flips = q.front().second.second;
  157.         q.pop_front();
  158.         // cout << "BFS: " << x << " " << y << " " << g << " " << flips << "\n";
  159.         if(dist[x][y][g] != -1){
  160.             // cout << "Visited\n";
  161.             continue;
  162.         }
  163.         dist[x][y][g] = flips;
  164.         pii coords = fall[x][y][g];
  165.         if(coords == make_pair(-1, -1)){
  166.             for(int i = x; i < n and i >= 0; i -= g * 2 - 1){
  167.                 if(dist[i][y][g] == -1){
  168.                     dist[i][y][g] = flips;
  169.                 }
  170.                 else{
  171.                     dist[i][y][g] = min(dist[i][y][g], flips);
  172.                 }
  173.             }
  174.             // cout << "Fell out\n";
  175.             continue;
  176.         }
  177.         if(coords == make_pair(x, y)){
  178.             // cout << "No change from gravity\n";
  179.         }
  180.         else{
  181.             if(x < coords.first){
  182.                 for(int i = x; i <= coords.first; i++){
  183.                     if(dist[i][y][g] == -1){
  184.                         dist[i][y][g] = flips;
  185.                     }
  186.                     else{
  187.                         dist[i][y][g] = min(dist[i][y][g], flips);
  188.                     }
  189.                 }
  190.             }
  191.             else{
  192.                 for(int i = coords.first; i <= x; i++){
  193.                     if(dist[i][y][g] == -1){
  194.                         dist[i][y][g] = flips;
  195.                     }
  196.                     else{
  197.                         dist[i][y][g] = min(dist[i][y][g], flips);
  198.                     }
  199.                 }
  200.             }
  201.             x = coords.first;
  202.             y = coords.second;
  203.         }
  204.         for(int i = 0; i < 2; i++){
  205.             int ny = y + dl[i];
  206.             // cout << x << " " << ny << ": ";
  207.             if(ny < 0 or ny >= m){
  208.                 // cout << "out of bounds\n";
  209.                 continue;
  210.             }
  211.             else if(world[x][ny] == 1){
  212.                 // cout << "blocked\n";
  213.                 continue;
  214.             }
  215.             else{
  216.                 // cout << "pushed\n";
  217.                 q.push_front(make_pair(make_pair(x, ny), make_pair(g, flips)));
  218.             }
  219.         }
  220.         // cout << "gravity flipped\n";
  221.         q.push_back(make_pair(make_pair(x, y), make_pair(1 - g, flips + 1)));
  222.     }
  223. }
  224.  
  225. void ans(){
  226.     int ex = epos.first;
  227.     int ey = epos.second;
  228.     if(dist[ex][ey][0] == -1 and dist[ex][ey][1] == -1){
  229.         cout << "-1";
  230.     }
  231.     else if(dist[ex][ey][0] == -1){
  232.         cout << dist[ex][ey][1];
  233.     }
  234.     else if(dist[ex][ey][1] == -1){
  235.         cout << dist[ex][ey][0];
  236.     }
  237.     else{
  238.         cout << min(dist[ex][ey][1], dist[ex][ey][0]);
  239.     }
  240. }
  241.  
  242. int main() {
  243.     FAST;
  244.     freopen("gravity.in", "r", stdin);
  245.     freopen("gravity.out", "w", stdout);
  246.     cin >> n >> m;
  247.     input();
  248.     init_gravity();
  249.     init_fall();
  250.     // print_fall();
  251.     // print_world();
  252.     init_dist();
  253.     BFS();
  254.     // print_dist();
  255.     ans();
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement