Advertisement
updown

USACO Laserphones

Jun 20th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define For(i, a, b) for(int i=a; i<b; i++)
  5. #define ffi For(i, 0, N)
  6. #define ffj For(j, 0, M)
  7. #define s <<" "<<
  8. #define w cout
  9. #define e endl
  10. #define pb push_back
  11. //500,000,000 operations
  12. //Global Variables
  13. int N, M, grid[100][100], sx, sy, ex, ey;
  14. char inp[100][100];
  15. int out = 100000;
  16. bool fs = false;
  17. queue<int> que1, que2;
  18.  
  19. void search(int x, int y) {
  20.     For (i, x+1, N) {
  21.         if (grid[i][y] == -2) break;
  22.         if (grid[i][y] == -1) {
  23.             grid[i][y] = grid[x][y]+1;
  24.             que1.push(i);
  25.             que2.push(y);
  26.         }
  27.     }
  28.     for (int i=x-1; i>=0; i--) {
  29.         if (grid[i][y] == -2) break;
  30.         if (grid[i][y] == -1) {
  31.             grid[i][y] = grid[x][y]+1;
  32.             que1.push(i);
  33.             que2.push(y);
  34.         }
  35.     }
  36.     For (i, y+1, M) {
  37.         if (grid[x][i] == -2) break;
  38.         if (grid[x][i] == -1) {
  39.             grid[x][i] = grid[x][y]+1;
  40.             que1.push(x);
  41.             que2.push(i);
  42.         }
  43.     }
  44.     for (int i=y-1; i>=0; i--) {
  45.         if (grid[x][i] == -2) break;
  46.         if (grid[x][i] == -1) {
  47.             grid[x][i] = grid[x][y]+1;
  48.             que1.push(x);
  49.             que2.push(i);
  50.         }
  51.     }
  52. }
  53. void s1(int x, int y) {
  54.     For (i, x+1, N) {
  55.         if (grid[i][y] == -2) break;
  56.         if (grid[i][y] == -1) {
  57.             grid[i][y] = grid[x][y];
  58.             que1.push(i);
  59.             que2.push(y);
  60.         }
  61.     }
  62.     for (int i=x-1; i>=0; i--) {
  63.         if (grid[i][y] == -2) break;
  64.         if (grid[i][y] == -1) {
  65.             grid[i][y] = grid[x][y];
  66.             que1.push(i);
  67.             que2.push(y);
  68.         }
  69.     }
  70.     For (i, y+1, M) {
  71.         if (grid[x][i] == -2) break;
  72.         if (grid[x][i] == -1) {
  73.             grid[x][i] = grid[x][y];
  74.             que1.push(x);
  75.             que2.push(i);
  76.         }
  77.     }
  78.     for (int i=y-1; i>=0; i--) {
  79.         if (grid[x][i] == -2) break;
  80.         if (grid[x][i] == -1) {
  81.             grid[x][i] = grid[x][y];
  82.             que1.push(x);
  83.             que2.push(i);
  84.         }
  85.     }
  86.     while (!que1.empty()) {
  87.         int a = que1.front();
  88.         int b = que2.front();
  89.         que1.pop();
  90.         que2.pop();
  91.         search(a, b);
  92.     }
  93. }
  94.  
  95. int main() {
  96.     //ifstream cin ("test.in");
  97.     //ofstream cout ("test.out");
  98.     cin >> M >> N;
  99.     ffi {
  100.         ffj {
  101.             grid[i][j] = -1;
  102.             cin >> inp[i][j];
  103.             if (inp[i][j] == 'C') {
  104.                 if (fs) {ex = i; ey = j;}
  105.                 else {fs = true; sx = i; sy = j;}
  106.             }
  107.             if (inp[i][j] == '*') grid[i][j] = -2;
  108.         }
  109.     }
  110.     grid[sx][sy] = 0;
  111.     s1(sx, sy);
  112.     w<<grid[ex][ey]<<e;
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement