-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmallMania_UVA11101OJ.cpp
113 lines (86 loc) · 2.56 KB
/
mallMania_UVA11101OJ.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
#include <bits/stdc++.h>
using namespace std;
const int N = 2000;
const int maxD = N + N + 1;
struct point
{
int x;
int y;
};
point _point(int x, int y)
{
point temp{x, y}; return temp;
}
bool g(int x, int y, int maxX, int maxY)
{
return x >= 0 && x <= maxX && y >= 0 && y <= maxY;
}
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 p1; cin >> p1;
if (p1 == 0) break;
int maxX = 0, maxY = 0;
vector<vector<bool>> visit(N + 1, vector<bool>(N + 1, false));
vector<vector<char>> type(N + 1, vector<char>(N + 1, -1));
vector<vector<int>> d(N + 1, vector<int>(N + 1, maxD));
vector<point> mall0(p1);
for (int i = 0; i <= p1 - 1; ++i)
{
cin >> mall0[i].x >> mall0[i].y;
type[mall0[i].x][mall0[i].y] = 0;
d[mall0[i].x][mall0[i].y] = 0;
maxX = max(maxX, mall0[i].x); maxY = max(maxY, mall0[i].y);
}
int p2; cin >> p2;
vector<point> mall1(p2);
for (int i = 0; i <= p2 - 1; ++i)
{
cin >> mall1[i].x >> mall1[i].y;
type[mall1[i].x][mall1[i].y] = 1;
maxX = max(maxX, mall1[i].x); maxY = max(maxY, mall1[i].y);
}
int ans = maxD;
queue<point> q;
q.push(mall0[0]);
visit[mall0[0].x][mall0[0].y] = true;
while (q.empty() == false)
{
point now = q.front();
q.pop();
for (int _p = 0; _p <= 3; ++_p)
{
point next;
next.x = now.x + xP[_p];
next.y = now.y + yP[_p];
if (g(next.x, next.y, maxX, maxY) == false)
{
continue;
}
else if (type[next.x][next.y] == 1)
{
ans = min(ans, d[now.x][now.y] + 1);
continue;
}
if (d[next.x][next.y] > d[now.x][now.y] + 1)
{
d[next.x][next.y] = d[now.x][now.y] + 1;
}
else if (visit[next.x][next.y] == true)
{
continue;
}
visit[next.x][next.y] = true;
q.push(next);
}
}
cout << ans << '\n';
}
cout.flush();
return 0;
}