-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02178_maze_BFS.cpp
More file actions
64 lines (59 loc) · 1.23 KB
/
02178_maze_BFS.cpp
File metadata and controls
64 lines (59 loc) · 1.23 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
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
int N, M;
int map[101][101];
int dr[] = {0, 1, 0, -1};
int dc[] = {1, 0, -1, 0};
queue<pair<int, int>> q;
void input()
{
cin >> N >> M;
for (int i = 1; i <= N; i++)
{
string str;
cin >> str;
for (int j = 0; j < str.length(); j++)
if (str[j] - '0')
map[i][j + 1] = 1;
}
q.push(make_pair(1, 1));
}
void BFS()
{
while (!q.empty())
{
int r = q.front().first;
int c = q.front().second;
q.pop();
for (int i = 0; i < 4; i++)
{
int nr = r + dr[i];
int nc = c + dc[i];
if (nr > 0 && nr <= N && nc > 0 && nc <= M && map[nr][nc] == 1)
{
q.push(make_pair(nr, nc));
if (map[nr][nc] == 1)
{
map[nr][nc] = map[r][c] + 1;
}
else
{
map[nr][nc] = min(map[nr][nc], map[r][c] + 1);
}
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
input();
BFS();
cout << map[N][M];
return 0;
}