forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuk.cpp
99 lines (78 loc) · 1.93 KB
/
vuk.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
#include <bits/stdc++.h>
using namespace std;
int inf = 100000000;
int main() {
int n, m;
cin >> n >> m;
// Read in grid
vector<string> grid(n);
for(auto& i : grid) cin >> i;
// Find start points
int vi, vj, ji, jj;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(grid[i][j] == 'V') {
vi = i;
vj = j;
}
if(grid[i][j] == 'J') {
ji = i;
jj = j;
}
}
}
// Calculate how for each spot is to the nearest tree
vector<vector<int>> totree(n,vector<int>(m,inf));
queue<pair<int,int>> treeq;
vector<int> dx = {1,-1,0,0};
vector<int> dy = {0,0,1,-1};
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(grid[i][j] == '+') {
totree[i][j] = 0;
treeq.push({i,j});
}
}
}
while(!treeq.empty()) {
pair<int,int> curr = treeq.front();
treeq.pop();
for(int i = 0; i < 4; i++) {
int currx = curr.first + dx[i];
int curry = curr.second + dy[i];
if(currx < 0 || currx >= n) continue;
if(curry < 0 || curry >= m) continue;
if(totree[currx][curry] != inf) continue;
totree[currx][curry] = totree[curr.first][curr.second] + 1;
treeq.push({currx,curry});
}
}
for(int dist = n*m+5; dist >= 0; dist--) {
// See if we can make a path in this dist or better
if(totree[vi][vj] < dist) continue;
if(totree[ji][jj] < dist) continue;
vector<vector<bool>> vis(n,vector<bool>(m,false));
queue<pair<int,int>> bestq;
bestq.push({vi,vj});
while(!bestq.empty()) {
pair<int,int> curr = bestq.front();
bestq.pop();
if(vis[curr.first][curr.second]) {
continue;
}
vis[curr.first][curr.second] = true;
for(int i = 0; i < 4; i++) {
int nextx = curr.first + dx[i];
int nexty = curr.second + dy[i];
if(nextx < 0 || nextx >= n) continue;
if(nexty < 0 || nexty >= m) continue;
if(totree[nextx][nexty] < dist) continue;
bestq.push({nextx,nexty});
}
}
if(vis[ji][jj]) {
cout << dist << endl;
return 0;
}
}
}