-
Notifications
You must be signed in to change notification settings - Fork 0
/
11281.cpp
63 lines (63 loc) · 1.68 KB
/
11281.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
#include <iostream>
#include <queue>
using namespace std;
inline bool valid(int x, int y, int m, int n) {
return x >= 0 && y >= 0 && x < m && y < n;
}
int main() {
int m, n, m1, m2;
cin >> m >> n >> m1 >> m2;
int startx = 0, starty = 0, endx = 0, endy = 0;
int* data = new int[m * n];
int* dist = new int[m * n];
bool* vis = new bool[m * n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cin >> data[n * i + j];
dist[n * i + j] = 2147483647;
vis[n * i + j] = false;
if (data[n * i + j] == 3) {
startx = i;
starty = j;
dist[n * i + j] = 0;
}
if (data[n * i + j] == 4) {
endx = i;
endy = j;
data[n * i + j] = 1;
}
}
}
queue<int> q;
q.push(startx * n + starty);
vis[startx * n + starty] = true;
int x, y, tmp;
while (!q.empty()) {
tmp = q.front();
q.pop();
x = tmp / n;
y = tmp % n;
if (x == endx && y == endy) {
cout << dist[tmp];
return 0;
}
for (int i = -1; i < 2; i += 2) {
for (int j = -1; j < 2; j += 2) {
if (valid(x + i * m1, y + j * m2, m, n) && data[(x + i * m1) * n + y + j * m2] == 1 && !vis[(x + i * m1) * n + y + j * m2]) {
vis[(x + i * m1) * n + y + j * m2] = true;
dist[(x + i * m1) * n + y + j * m2] = dist[tmp] + 1;
q.push((x + i * m1) * n + y + j * m2);
}
if (valid(x + i * m2, y + j * m1, m, n) && data[(x + i * m2) * n + y + j * m1] == 1 && !vis[(x + i * m2) * n + y + j * m1]) {
vis[(x + i * m2) * n + y + j * m1] = true;
dist[(x + i * m2) * n + y + j * m1] = dist[tmp] + 1;
q.push((x + i * m2) * n + y + j * m1);
}
}
}
}
delete[] data;
delete[] dist;
delete[] vis;
return 0;
}