-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14923.cpp
75 lines (70 loc) · 1.51 KB
/
14923.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
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int N, M;
int hx, hy;
int sx, sy;
struct hk {
int x, y, time, br;
};
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int map[1024][1024];
bool visited[1024][1024][2];
int bfs() {
queue<hk> que;
visited[sx][sy][1] = true;
que.push({ sx,sy,0,1 });
while (!que.empty()) {
int x = que.front().x;
int y = que.front().y;
int time = que.front().time;
int br = que.front().br;
que.pop();
if (x == hx && y == hy) return time;
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 (visited[nx][ny][br]) continue;
if (map[nx][ny] == 0) {
visited[nx][ny][br] = true;
que.push({ nx,ny,time + 1,br });
}
else if(map[nx][ny] == 1 && br == 1) {
visited[nx][ny][br] = true;
que.push({ nx,ny,time + 1,br - 1 });
}
/*if (nx >= 0 && ny >= 0 && nx < N && ny < M) {
if (map[nx][ny] == 0 && !visited[nx][ny][br]) {
visited[nx][ny][br] = true;
que.push({ nx,ny,time + 1,br });
}
else if (map[nx][ny] == 1 && !visited[nx][ny][br] && br == 1) {
visited[nx][ny][br] = true;
que.push({ nx,ny,time + 1,br - 1 });
}
}
*/
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
cin >> sx >> sy;
sx--, sy--;
cin >> hx >> hy;
hx--, hy--;
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
cin >> map[x][y];
}
}
cout << bfs();
return 0;
}