-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABC_176_D.cpp
93 lines (76 loc) · 1.55 KB
/
ABC_176_D.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 <string>
#include <deque>
#include <utility>
#include <cstring>
using namespace std;
int h, w;
int dx, dy;
string map[1000];
bool visited[1000][1000];
int level[1000][1000];
const int direction[][2] = { -1, 0, 0, -1, 0, 1, 1, 0 };
int main() {
int cx, cy;
cin >> h >> w >> cx >> cy >> dx >> dy;
--cx;
--cy;
--dx;
--dy;
for (int i = 0; i < h; i++)
{
cin >> map[i];
for (int j = 0; j < w; j++)
{
level[i][j] = 987654321;
}
}
deque<pair<int, int> > que;
que.push_back(make_pair(cx, cy));
level[cx][cy] = 0;
while (!que.empty())
{
pair<int, int> current = que.front();
que.pop_front();
if (visited[current.first][current.second])
{
continue;
}
visited[current.first][current.second] = true;
// 5x5
for (int i = -2; i <= 2; i++)
{
for (int j = -2; j <= 2; j++)
{
int x = current.first + i;
int y = current.second + j;
if (x < 0 || x >= h || y < 0 || y >= w || map[x][y] != '.' || visited[x][y])
{
continue;
}
que.push_back(make_pair(x, y));
level[x][y] = min(level[x][y], level[current.first][current.second] + 1);
}
}
// 4x4
for (int d = 0; d < 4; d++)
{
int x = current.first + direction[d][0];
int y = current.second + direction[d][1];
if (x < 0 || x >= h || y < 0 || y >= w || map[x][y] != '.' || visited[x][y])
{
continue;
}
que.push_front(make_pair(x, y));
level[x][y] = level[current.first][current.second];
}
}
if (level[dx][dy] == 987654321)
{
cout << -1 << endl;
}
else
{
cout << level[dx][dy] << endl;
}
}