-
Notifications
You must be signed in to change notification settings - Fork 6
/
hotdogs.cpp
86 lines (80 loc) · 2.44 KB
/
hotdogs.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
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using qi = queue<int>;
using vvi = vector<vector<int>>;
using vp = vector<pair<int, int>>;
using pii = pair<int, int>;
using hm = unordered_map<int, vector<int>>;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vp delta = {pii(-1, 0), pii(1, 0), pii(0, 1), pii(0, -1)};
int t, n, R, C, r, c, u, d, dr, dc, nr, nc;
cin >> t;
while (t--) {
cin >> n >> R >> C;
qi q;
vvi D;
hm M;
for (int i = 0; i < R; i++) {
D.push_back(vi(C, -1));
}
for (int i = 0; i < n; i++) {
cin >> r >> c;
D[r][c] = 0;
q.push(r*C+c);
}
// BFS
while (!q.empty()) {
u = q.front();
q.pop();
r = u/C;
c = u%C;
d = D[r][c];
for (pii p : delta) {
dr = p.first;
dc = p.second;
nr = r+dr;
nc = c+dc;
if (0<=nr && nr<R && 0<=nc && nc<C) {
if (D[nr][nc] == -1) {
D[nr][nc] = d+1;
if (M.find(d+1) == M.end()) {
M[d+1] = vi();
}
M[d+1].push_back(nr*C+nc);
q.push(nr*C+nc);
}
}
}
}
if (M.empty()) {
cout << R+C-2 << '\n';
} else {
int lo=0, hi=d, found, mid, x1, x2, y1, y2;
while (lo <= hi) {
found = 0;
mid = (lo + hi)/2;
if (M.find(mid) != M.end()) {
for (int u1 : M[mid]) {
for (int u2 : M[mid]) {
x1 = u1/C;
x2 = u2/C;
y1 = u1%C;
y2 = u2%C;
if (abs(x1-x2) + abs(y1-y2) >= mid) { found = 1; }
if (found) { break; }
}
if (found) { break; }
}
}
if (!found && mid != 0) { hi = mid-1; }
else { lo = mid+1; }
}
cout << hi << '\n';
}
}
return 0;
}