-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1261.cpp
54 lines (48 loc) · 1.3 KB
/
1261.cpp
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>
#include <limits.h>
using namespace std;
int M, N;
int arr[101][101];
vector<vector<int>> dist(101, vector<int>(101, INT_MAX));
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void bfs(){
queue<pair<int, int>> q;
q.push({1, 1});
dist[1][1] = 0;
while(!q.empty()){
int row = q.front().first;
int col = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
int nRow = row + dy[i];
int nCol = col + dx[i];
if(nRow <= 0 || nRow > N || nCol <= 0 || nCol > M)
continue;
if(arr[nRow][nCol] == 1 && (dist[nRow][nCol] > dist[row][col] + 1)){
dist[nRow][nCol] = dist[row][col] + 1;
q.push({nRow, nCol});
}
else if(!arr[nRow][nCol] && (dist[nRow][nCol] > dist[row][col])){
dist[nRow][nCol] = dist[row][col];
q.push({nRow, nCol});
}
}
}
}
int main(void) {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> M >> N;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= M; j++){
char ch;
cin >> ch;
arr[i][j] = ch - '0';
}
}
bfs();
cout << dist[N][M];
return 0;
}