-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetaway_UVA949OJ.cpp
144 lines (116 loc) · 3.34 KB
/
getaway_UVA949OJ.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
struct segment
{
bool wait;
int t;
int num;
};
segment _segment(bool wait, int t, int num)
{
segment temp{wait, t, num}; return temp;
}
bool g0(int x, int y, int nx, int ny)
{
return x >= 0 && x <= nx - 1 && y >= 0 && y <= ny - 1;
}
bool g1(int num, int nx, int ny)
{
return num >= 0 && num <= nx * ny - 1;
}
int turn(int x, int y, int nx, int ny)
{
return y * nx + x;
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
vector<int> xP = {-1, 1, 0, 0},
yP = {0, 0, -1, 1};
while (true)
{
int nx = 0, ny = 0; cin >> nx >> ny;
if (nx + ny == 0) break;
vector<int> numP = {-nx, -1, nx, 1};
vector<vector<bool>> a(nx * ny, vector<bool>(nx * ny, false));
for (int y = 0; y <= ny - 1; ++y)
{
for (int x = 0; x <= nx - 1; ++x)
{
for (int p = 0; p <= 3; ++p)
{
int nextX = x + xP[p],
nextY = y + yP[p];
if (g0(nextX, nextY, nx, ny) == true)
{
a[turn(x, y, nx, ny)][turn(nextX, nextY, nx, ny)] = true;
}
}
}
}
int r; cin >> r;
for (int c = 1; c <= r; ++c)
{
int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
a[turn(x1, y1, nx, ny)][turn(x2, y2, nx, ny)] = false;
}
int m; cin >> m;
vector<int> pass(nx * ny * 100, -1);
for (int c = 1; c <= m; ++c)
{
int t, x, y; cin >> t >> x >> y;
pass[t] = turn(x, y, nx, ny);
}
queue<segment> q; q.push(_segment(false, 0, turn(0, 0, nx, ny)));
vector<int> d(nx * ny, nx * ny + 1);
int ans = -1;
while (q.empty() == false && ans == -1)
{
segment now = q.front(); q.pop();
if (now.num == turn(nx - 1, ny - 1, nx, ny))
{
ans = now.t;
break;
}
if (d[now.num] <= now.t && now.wait == false)
{
continue;
}
d[now.num] = now.t;
for (int p = 0; p <= 3; ++p)
{
segment next;
next.wait = false;
next.t = now.t + 1;
next.num = now.num + numP[p];
if (g1(next.num, nx, ny) == false)
{
continue;
}
else if (a[now.num][next.num] == false)
{
continue;
}
else if (d[next.num] < nx * ny + 1)
{
continue;
}
else if (pass[next.t] == next.num)
{
continue;
}
q.push(next);
}
if (pass[now.t + 1] != now.num)
{
q.push(_segment(true, now.t + 1, now.num));
}
}
cout << ans << '\n';
}
cout.flush();
return 0;
}