-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1261.cpp
More file actions
58 lines (53 loc) · 1.19 KB
/
1261.cpp
File metadata and controls
58 lines (53 loc) · 1.19 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
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
int n,m;
int map[100][100];
int check[100][100];
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
struct Pos{
int x,y,c;
};
void bfs(){
queue<Pos> q;
q.push({0,0,0});
check[0][0] = 0;
while(!q.empty()){
int x = q.front().x;
int y = q.front().y;
int c = q.front().c;
q.pop();
for(int i=0;i<4;i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= n || ny >= m)
continue;
if(map[nx][ny]){
if(check[nx][ny] > c+1){
check[nx][ny] = c+1;
q.push({nx,ny,c+1});
}
}
else{
if(check[nx][ny] > c){
check[nx][ny] = c;
q.push({nx,ny,c});
}
}
}
}
}
int main(){
scanf("%d %d",&m,&n);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
scanf("%1d",&map[i][j]);
check[i][j] = INF;
}
bfs();
printf("%d\n",check[n-1][m-1]);
return 0;
}