-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva10653.cpp
129 lines (82 loc) · 1.74 KB
/
uva10653.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
#include <queue>
#define neighbours 4
#define BFS_WHITE -1
using namespace std;
int dist[1010][1010];
vector<vector<int> > map;
int R, C;
pair<int, int> dest;
int xAxis[] = {0, -1, 1, 0};
int yAxis[] = {-1, 0, 0, 1};
void search(pair<int, int> src)
{
queue<pair<int, int> > q;
q.push(src);
dist[src.first][src.second] = 0;
while(!q.empty() && q.front() != dest)
{
pair<int, int> pos = q.front();
q.pop();
int row = pos.first;
int col = pos.second;
for(int i = 0; i < neighbours; i++)
{
if(0 <= row + xAxis[i] && row + xAxis[i] < R)
{
if(0 <= col + yAxis[i] && col + yAxis[i] < C)
{
if(map[row+xAxis[i]][col+yAxis[i]] == 1 &&
dist[row+xAxis[i]][col+yAxis[i]] == BFS_WHITE) /* not a mine */
{
dist[row+xAxis[i]][col+yAxis[i]] = dist[row][col] + 1;
q.push(make_pair(row+xAxis[i], col+yAxis[i]));
}
}
}
}
}
}
int main()
{
int mine_rows, curent_row, num_mines, mine_col;
int r1, c1, r2, c2;
pair<int, int> src;
while(true)
{
cin >> R >> C;
if(R == 0 && C == 0)
break;
map.clear();
map.resize(R);
for(int row = 0; row < R; row++)
map[row].resize(C, 1);
cin >> mine_rows;
for(int i = 0; i < mine_rows; i++)
{
cin >> curent_row;
cin >> num_mines;
for(int j = 0; j < num_mines; j++)
{
cin >> mine_col;
map[curent_row][mine_col] = 0;
}
}
cin >> r1 >> c1;
src = make_pair(r1, c1);
cin >> r2 >> c2;
dest = make_pair(r2, c2);
memset(dist, BFS_WHITE, sizeof(dist));
search(src);
cout << dist[r2][c2] << endl;
}
return 0;
}