-
Notifications
You must be signed in to change notification settings - Fork 0
/
p37_maze.cpp
77 lines (53 loc) · 1.25 KB
/
p37_maze.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
#include <iostream>
#include <queue>
const int INF = 100000000;
const int N_MAX = 100, M_MAX = 100;
int N,M;
char field[M_MAX][N_MAX];
// to save shortest distances from start
int d[M_MAX][N_MAX];
// start point (x,y)
int sx, sy;
// goal point (x,y)
int gx, gy;
// vector for 4 direction
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
typedef std::pair<int, int> P;
// breath-first search : 幅優先探索
int bfs(){
std::queue<P> que;
for( int y=0; y<N; y++)
for( int x=0; x<M; x++) d[x][y] = INF;
// push start pos into que
que.push(P(sx, sy));
d[sx][sy] = 0; // set start pos d = 0
// if que is empty. it'll finish
while(que.size()){
P p = que.front(); que.pop();
if (p.first == gx && p.second == gy) break;
for (int i=0; i<4; i++){
int nx = p.first + dx[i], ny = p.second + dy[i];
if (nx>=0 && nx<M && ny>=0 && ny<N && field[nx][ny]!='#' && d[nx][ny]==INF){
que.push(P(nx,ny));
d[nx][ny] = d[p.first][p.second] + 1;
}
}
}
return d[gx][gy];
}
int main(){
std::cin >> N >> M;
for(int y=0; y<N; y++){
for(int x=0; x<M; x++){
std::cin >> field[x][y];
if( field[x][y]== 'S'){
sx = x;
sy = y;
}else if(field[x][y] == 'G'){
gx = x;
gy = y;
}
}
}
std::cout << bfs() << "\n";
}