-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekJonn_2178.cpp
More file actions
54 lines (43 loc) · 1.05 KB
/
BaekJonn_2178.cpp
File metadata and controls
54 lines (43 loc) · 1.05 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int N, M;
int arr[101][101] = {};
int ansArr[101][101] = {};
bool visited[101][101] = {};
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,1,-1 };
queue<pair<int, int>> mq;
void BFS() {
while (!mq.empty()) {
int x = mq.front().first;
int y = mq.front().second;
mq.pop();
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 && arr[nx][ny] == 1 && !visited[nx][ny]) {
mq.push({ nx,ny });
ansArr[nx][ny] = ansArr[x][y] + 1;
visited[nx][ny] = true;
}
}
}
}
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++) {
string line;
cin >> line;
for (int j = 0; j < M; j++) {
arr[i][j] = line[j] - '0';
}
}
mq.push({ 0,0 });
visited[0][0] = true;
ansArr[0][0] = 1;
BFS();
cout << ansArr[N - 1][M - 1] << endl;
return 0;
}