-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10653.cpp
76 lines (56 loc) · 1.62 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
70
71
72
73
74
75
76
//uva 10653
//Bombs! NO they are Mines!!
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int BFS(vector <vector <bool> > & arr, int xs, int ys, int xd, int yd);
int main(void)
{
int r, c;
while (true){
cin >> r >> c;
if (r == 0 && c == 0)
break;
vector < vector <bool> > arr(r, vector <bool> (c, true));
int n;
cin >> n;
for (int i = 0; i < n; ++i){
int row, count;
cin >> row >> count;
for (int j = 0; j < count; ++j){
int t;
cin >> t;
arr[row][t] = false;
}
}
int xs, ys, xd, yd;
cin >> xs >> ys >> xd >> yd;
cout << BFS(arr, xs, ys, xd, yd) << endl;
}
return 0;
}
int BFS(vector <vector <bool> > & arr, int xs, int ys, int xd, int yd)
{
int r = arr.size();
int c = arr[0].size();
vector <vector <int> > visited(r, vector<int> (c, -1));
queue < pair <int, int> > Queue;
Queue.push(make_pair(xs, ys));
visited[xs][ys] = 0;
int xx[] = {-1, 0, 0, 1};
int yy[] = {0, -1, 1, 0};
while (!Queue.empty()){
pair <int, int> front = Queue.front();
Queue.pop();
for (int i = 0; i < 4; ++i){
int nx = front.first + xx[i];
int ny = front.second + yy[i];
if (nx >= 0 && nx < r && ny >= 0 && ny < c && arr[nx][ny] && visited[nx][ny] == -1){
visited[nx][ny] = visited[front.first][front.second] + 1;
Queue.push(make_pair(nx, ny));
}
}
}
return visited[xd][yd];
}