-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2178.cpp
More file actions
51 lines (38 loc) · 1.01 KB
/
2178.cpp
File metadata and controls
51 lines (38 loc) · 1.01 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
#include <iostream>
#include <queue>
#include <utility>
#include <string>
using namespace std;
int N, M;
int check[100][100] = { 0, };
bool v[100][100] = { false, };
string arr[100];
int dx[4] = { 0, 1, 0, -1};
int dy[4] = { -1, 0, 1, 0};
void bfs(int i, int j) {
v[i][j] = true;
queue<pair<int, int> > q;
q.push(make_pair(i,j));
while(!q.empty()) {
int x = q.front().second;
int y = q.front().first;
q.pop();
for (int k = 0; k < 4; k++) {
int newX = x + dx[k];
int newY = y + dy[k];
if (0 <= newX && newX < M && 0 <= newY && newY < N && arr[newY][newX] == '1' && !v[newY][newX] && check[newY][newX] == 0) {
check[newY][newX] = check[y][x] + 1;
v[newY][newX] = true;
q.push(make_pair(newY, newX));
}
}
}
}
int main() {
int i;
cin >> N >> M;
for (i = 0; i < N; i++) cin >> arr[i];
bfs(0, 0);
printf("%d\n", check[N-1][M-1]+1);
return 0;
}