-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbj_2178.cpp
More file actions
62 lines (51 loc) · 1.22 KB
/
bj_2178.cpp
File metadata and controls
62 lines (51 loc) · 1.22 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
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
int N, M, x, y, nx, ny;
int Min = 100000000;
int visit[101][101] = {};
int maze[101][101] = {};
int Move[4][2] = {
{0, 1}, {0, -1},
{1, 0}, {-1, 0},
};
void BFS(){
queue <pair<int, int> > q;
q.push(pair<int, int>(0, 0));
visit[0][0] = 1;
while(!q.empty()) {
x = q.front().first;
y = q.front().second;
q.pop();
if(x == N-1 && y == M-1)
return;
for(int i = 0; i<4; i++) {
nx = x + Move[i][0];
ny = y + Move[i][1];
if(nx >= 0 && ny >= 0 && nx < N && ny < M) {
if(visit[nx][ny] == 0 && maze[nx][ny] == 1) {
q.push(pair<int, int>(nx, ny));
visit[nx][ny] = visit[x][y] + 1;
}
}
}
}
}
int main() {
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> N >> M;
for(int i = 0; i < N; i++) {
string str;
cin >> str;
for(int j = 0; j<M; j++) {
maze[i][j] = stoi(str.substr(j,1));
}
}
BFS();
cout << visit[N-1][M-1] << "\n";
return 0;
}