-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingIn2DGridBFS.cpp
More file actions
90 lines (71 loc) · 2.27 KB
/
MovingIn2DGridBFS.cpp
File metadata and controls
90 lines (71 loc) · 2.27 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/// UVa OJ - 10653
/// GUIDE - http://www.shafaetsplanet.com/planetcoding/?p=604
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
int fx[] = {1, -1, 0, 0}; /// Direction array
int fy[] = {0, 0, 1, -1}; /// Direction array
int cell[1000][1000]; /// If cell[x][y] = -1, then there is a mine
int dist[1000][1000];
bool visited[1000][1000];
int row;
int col;
void BFS(int sourceX, int sourceY, int desX, int desY);
void Initialize();
int main()
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int bombRows, i, j, bombR, c, bombC, sX, sY, dX, dY;
while (scanf("%d%d", &row, &col)) {
if (row == 0 && col == 0) {
break;
}
Initialize();
scanf("%d", &bombRows);
for (i = 1; i <= bombRows; i++) {
scanf("%d%d", &bombR, &c);
for (j = 1; j <= c; j++) {
scanf("%d", &bombC);
cell[bombR][bombC] = -1;
}
}
scanf("%d%d%d%d", &sX, &sY, &dX, &dY);
BFS(sX, sY, dX, dY);
printf("%d\n", dist[dX][dY]);
}
return 0;
}
void Initialize()
{
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++) {
cell[r][c] = dist[r][c] = 0;
visited[r][c] = false;
}
}
}
void BFS(int sourceX, int sourceY, int desX, int desY)
{
visited[sourceX][sourceY] = true;
queue<pii>Q;
Q.push(pii(sourceX, sourceY));
while (!Q.empty()) {
pii top = Q.front();
Q.pop();
for (int i = 0; i < 4; i++) {
int tx = top.first + fx[i];
int ty = top.second + fy[i];
/// Checking whether the neighbor is valid and not visited before
if (tx >= 0 && tx < row && ty >= 0 && ty < col && cell[tx][ty] != -1 && visited[tx][ty] == false) {
/// Marking it visited
visited[tx][ty] = true;
/// Any movement means Total Moves = Previous Moves + 1
dist[tx][ty] = dist[top.first][top.second] + 1;
/// Pushing the new pair in the Q
Q.push(pii(tx, ty));
if (tx == desX && ty == desY) return;
}
}
}
}