Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int R, C, a[1003][1003], ans[1003][1003], adj[1003];
- void BFS(int x, int y) {
- queue <int> q;
- q.push(x); q.push(y);
- ans[x][y] = 0;
- int X[] = {-1, 0, 0, 1};
- int Y[] = {0, -1, 1, 0};
- while (!q.empty())
- {
- int Ux = q.front(); q.pop();
- int Uy = q.front(); q.pop();
- for (int i=0; i<4; ++i)
- {
- int Vx = Ux + X[i];
- int Vy = Uy + Y[i];
- if ((Vx>=0 && Vx<=R) && (Vy>=0 && Vy <= C) && !a[Vx][Vy])
- if (!ans[Vx][Vy])
- {
- ans[Vx][Vy] = ans[Ux][Uy] + 1;
- q.push(Vx); q.push(Vy);
- }
- }
- }
- }
- int main() {
- #ifndef ONLINE_JUDGE
- freopen("uva10653.inp", "r", stdin);
- #endif // ONLINE_JUDGE
- while (1)
- {
- scanf("%d%d", &R, &C);
- if (!R || !C) break;
- memset(a, 0, sizeof(a));
- memset(ans, 0, sizeof(ans));
- int r;
- scanf("%d", &r);
- for (int i=0; i<r; ++i)
- {
- int row, bomb;
- scanf("%d%d", &row, &bomb);
- for (int j=0; j<bomb; ++j)
- {
- int x;
- scanf("%d", &x);
- a[row][x] = 1;
- }
- }
- scanf("%d%d%d%d", &Sx, &Sy, &Tx, &Ty);
- BFS(Sx, Sy);
- printf("%d\n", ans[Tx][Ty]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment