-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10653.cpp
More file actions
69 lines (67 loc) · 1.8 KB
/
10653.cpp
File metadata and controls
69 lines (67 loc) · 1.8 KB
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
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define inf HUGE_VAL
#define mem(a, b) memset(a, b, sizeof(a))
#define NN 1000
int a[NN + 7][NN + 7];
int dist[NN + 7][NN + 7];
queue<int> X;
queue<int> Y;
int dr[] = {0, -1, 0, 1};
int dc[] = {-1, 0, 1, 0};
int R, C;
int bfs(int srcx, int srcy, int dstx, int dsty) {
X = queue<int>();
Y = queue<int>();
mem(dist, -1);
X.push(srcx);
Y.push(srcy);
dist[srcx][srcy] = 0;
while (!X.empty()) {
int xx = X.front();
int yy = Y.front();
X.pop();
Y.pop();
for (int i = 0; i < 4; i++) {
int x = xx + dr[i];
int y = yy + dc[i];
if (x < 0 || y < 0 || x >= R || y >= C) continue;
if (dist[x][y] == -1 && a[x][y] == 0) {
X.push(x);
Y.push(y);
dist[x][y] = dist[xx][yy] + 1;
if (x == dstx && y == dsty) return dist[x][y];
}
}
}
return 0;
}
int main() {
// freopen("C:\\Users\\Maruf Tuhin\\Desktop\\in.txt","r",stdin);
int i, j, k, l, n, r, c;
int tc, t = 1;
// scanf("%d",&tc);
while (~scanf("%d%d", &R, &C)) {
if (R == 0 && C == 0) return 0;
mem(a, 0);
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d", &r, &k);
while (k--) scanf("%d", &c), a[r][c] = 1;
}
int srcx, srcy, dstx, dsty;
scanf("%d%d%d%d", &srcx, &srcy, &dstx, &dsty);
int sum = bfs(srcx, srcy, dstx, dsty);
printf("%d\n", sum);
}
return 0;
}