giabinh

10653_Bombs!NoTheyAreMines

Sep 14th, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int R, C, a[1003][1003], ans[1003][1003], adj[1003];
  6.  
  7. void BFS(int x, int y) {
  8.     queue <int> q;
  9.     q.push(x); q.push(y);
  10.     ans[x][y] = 0;
  11.  
  12.     int X[] = {-1, 0, 0, 1};
  13.     int Y[] = {0, -1, 1, 0};
  14.     while (!q.empty())
  15.     {
  16.         int Ux = q.front(); q.pop();
  17.         int Uy = q.front(); q.pop();
  18.  
  19.         for (int i=0; i<4; ++i)
  20.         {
  21.             int Vx = Ux + X[i];
  22.             int Vy = Uy + Y[i];
  23.  
  24.             if ((Vx>=0 && Vx<=R) && (Vy>=0 && Vy <= C) && !a[Vx][Vy])
  25.                 if (!ans[Vx][Vy])
  26.                 {
  27.                     ans[Vx][Vy] = ans[Ux][Uy] + 1;
  28.                     q.push(Vx); q.push(Vy);
  29.                 }
  30.         }
  31.     }
  32. }
  33.  
  34. int main() {
  35.     #ifndef ONLINE_JUDGE
  36.         freopen("uva10653.inp", "r", stdin);
  37.     #endif // ONLINE_JUDGE
  38.  
  39.     while (1)
  40.     {
  41.         scanf("%d%d", &R, &C);
  42.         if (!R || !C) break;
  43.  
  44.         memset(a, 0, sizeof(a));
  45.         memset(ans, 0, sizeof(ans));
  46.  
  47.         int r;
  48.         scanf("%d", &r);
  49.         for (int i=0; i<r; ++i)
  50.         {
  51.             int row, bomb;
  52.             scanf("%d%d", &row, &bomb);
  53.             for (int j=0; j<bomb; ++j)
  54.             {
  55.                 int x;
  56.                 scanf("%d", &x);
  57.                 a[row][x] = 1;
  58.             }
  59.         }
  60.  
  61.         scanf("%d%d%d%d", &Sx, &Sy, &Tx, &Ty);
  62.         BFS(Sx, Sy);
  63.         printf("%d\n", ans[Tx][Ty]);
  64.     }
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment