-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2665.cpp
More file actions
61 lines (50 loc) · 1.03 KB
/
2665.cpp
File metadata and controls
61 lines (50 loc) · 1.03 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
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
int n;
int arr[50][50];
queue<pair<int,int>>q;
int visit[50][50];
int dx[4] = {0,1,-1,0};
int dy[4] = { 1,0,0,-1 };
priority_queue<int> pq;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
for (int j = 0; j < n; j++) {
arr[i][j] = str[j]-'0';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
visit[i][j] = 99999;
}
}
q.push({ 0,0 });
visit[0][0] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
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 >= n) continue;
if (arr[nx][ny] == 0) {
if (visit[nx][ny] <= visit[x][y] + 1) continue;
visit[nx][ny] = visit[x][y] + 1;
q.push({ nx, ny });
}
else {
if (visit[nx][ny] <= visit[x][y]) continue;
visit[nx][ny] = visit[x][y];
q.push({ nx,ny });
}
}
}
cout << visit[n-1][n-1];
}