-
Notifications
You must be signed in to change notification settings - Fork 1
/
uva_10653.cpp
69 lines (63 loc) · 1.51 KB
/
uva_10653.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = a, _b = b; i < _b; ++i)
#define FOR(i, a, b) for(int i = a, _b = b; i <= _b; ++i)
#define endl "\n"
#define puts(_content_) cout << (_content_) << "\n"
#define newline cout << "\n"
using namespace std;
typedef tuple<int, int, int> iii;
const int MX = 1010;
bool visited[MX][MX];
char grid[MX][MX];
int R, C;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int bfs (int Sx, int Sy, int Dx, int Dy) {
queue<iii> que;
que.push(iii(Sx, Sy, 0));
while(!que.empty()) {
int x, y, d;
tie(x, y, d) = que.front();
que.pop();
if(x == Dx && y == Dy) {
return d;
}
if(visited[x][y]) continue;
visited[x][y] = true;
REP(i, 0, 4) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && nx < R && ny >= 0 && ny < C && grid[nx][ny] != 'x' && !visited[nx][ny]) {
que.push(iii(nx, ny, d+1));
}
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef Home
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
while(cin >> R >> C && R + C) {
memset(visited, 0, sizeof visited);
memset(grid, 0, sizeof grid);
int bombs;
cin >> bombs;
REP(i, 0, bombs) {
int r, c, n;
cin >> r >> n;
REP(i, 0, n) {
cin >> c;
grid[r][c] = 'x';
}
}
int Sx, Sy, Dx, Dy;
cin >> Sx >> Sy >> Dx >> Dy;
int answer = bfs(Sx, Sy, Dx, Dy);
puts(answer);
}
return 0;
}