forked from b-chae/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2178.cpp
More file actions
66 lines (55 loc) · 1.36 KB
/
Copy path2178.cpp
File metadata and controls
66 lines (55 loc) · 1.36 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
65
66
#include <iostream>
#include <list>
using namespace std;
list<int> bfsQueueX;
list<int> bfsQueueY;
int N, M;
int board[101][101];
bool visited[101][101];
void bfs() {
if (bfsQueueX.empty()) return;
int x = bfsQueueX.front();
int y = bfsQueueY.front();
bfsQueueX.pop_front(); bfsQueueY.pop_front();
int level = board[x][y];
if (x > 0 && board[x - 1][y] == 1 && !visited[x - 1][y]) {
visited[x - 1][y] = true;
board[x - 1][y] = level + 1;
bfsQueueX.push_back(x - 1); bfsQueueY.push_back(y);
}
if (y > 0 && board[x][y - 1] == 1 && !visited[x][y - 1]) {
visited[x][y - 1] = true;
board[x][y - 1] = level + 1;
bfsQueueX.push_back(x); bfsQueueY.push_back(y - 1);
}
if (x < N - 1 && board[x + 1][y] == 1 && !visited[x + 1][y]) {
visited[x + 1][y] = true;
board[x + 1][y] = level + 1;
bfsQueueX.push_back(x + 1); bfsQueueY.push_back(y);
}
if (y < M - 1 && board[x][y + 1] == 1 && !visited[x][y + 1]) {
visited[x][y + 1] = true;
board[x][y + 1] = level + 1;
bfsQueueX.push_back(x); bfsQueueY.push_back(y + 1);
}
bfs();
}
int main()
{
int i, j;
cin >> N >> M;
for (i = 0; i < N; i++) {
string tmp;
cin >> tmp;
for (j = 0; j < M; j++) {
visited[i][j] = false;
if (tmp[j] == '0') board[i][j] = 0;
else board[i][j] = 1;
}
}
bfsQueueX.push_back(0);
bfsQueueY.push_back(0);
visited[0][0] = true;
bfs();
cout << board[N - 1][M - 1];
}