-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2665.cpp
93 lines (91 loc) · 2.04 KB
/
2665.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, color;
cin >> n;
color = cin.get();
vector<vector<int>> map(n, vector<int>(n));
vector<vector<int>> btw(n, vector<int>(n,2501));
queue<pair<int, int>> leastmove;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
color = cin.get();
if (color == 48) {
map[i][j] = 0;
}
else if (color == 49) {
map[i][j] = 1;
}
}
color = cin.get();
}
leastmove.push(make_pair(0, 0));
btw[0][0] = 0;
while (!leastmove.empty()) {
int u = leastmove.front().first;
int v = leastmove.front().second;
leastmove.pop();
if (u > 0) {
if (map[u - 1][v] == 1) {
if (btw[u - 1][v] > btw[u][v]) {
btw[u - 1][v] = btw[u][v];
leastmove.push(make_pair(u - 1, v));
}
}
else if (map[u - 1][v] == 0) {
if (btw[u - 1][v] > btw[u][v] + 1) {
btw[u - 1][v] = btw[u][v] + 1;
leastmove.push(make_pair(u - 1, v));
}
}
}
if (u < n - 1) {
if (map[u + 1][v] == 1) {
if (btw[u + 1][v] > btw[u][v]) {
btw[u + 1][v] = btw[u][v];
leastmove.push(make_pair(u + 1, v));
}
}
else if (map[u + 1][v] == 0) {
if (btw[u + 1][v] > btw[u][v] + 1) {
btw[u + 1][v] = btw[u][v] + 1;
leastmove.push(make_pair(u + 1, v));
}
}
}
if (v > 0) {
if (map[u][v - 1] == 1) {
if (btw[u][v - 1] > btw[u][v]) {
btw[u][v - 1] = btw[u][v];
leastmove.push(make_pair(u, v - 1));
}
}
else if (map[u][v - 1] == 0) {
if (btw[u][v - 1] > btw[u][v] + 1) {
btw[u][v - 1] = btw[u][v] + 1;
leastmove.push(make_pair(u, v - 1));
}
}
}
if (v < n - 1) {
if (map[u][v + 1] == 1) {
if (btw[u][v + 1] > btw[u][v]) {
btw[u][v + 1] = btw[u][v];
leastmove.push(make_pair(u, v + 1));
}
}
else if (map[u][v + 1] == 0) {
if (btw[u][v + 1] > btw[u][v] + 1) {
btw[u][v + 1] = btw[u][v] + 1;
leastmove.push(make_pair(u, v + 1));
}
}
}
}
cout << btw[n - 1][n - 1];
}