-
Notifications
You must be signed in to change notification settings - Fork 0
/
미로 탐색.cc
52 lines (41 loc) · 1.08 KB
/
미로 탐색.cc
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
#include <iostream>
#include <deque>
#include <utility>
using namespace std;
int graph[101][101];
int N, M;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int bfs(int start_x, int start_y) {
deque<pair<int, int>> dq;
dq.push_back({start_x, start_y});
while(!dq.empty()) {
int x = dq.front().first;
int y = dq.front().second;
dq.pop_front();
for(int i=0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || nx >= N || ny < 0 || ny >= M)
continue;
if(graph[nx][ny] != 1)
continue;
dq.push_back({nx, ny});
graph[nx][ny] = graph[x][y] + 1;
}
}
return graph[N-1][M-1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for(int i=0; i < N; i++) {
string tmp;
cin >> tmp;
for(int j=0; j < M; j++)
graph[i][j] = tmp[j] == '0' ? 0 : 1;
}
cout << bfs(0, 0);
return 0;
}