forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoneyheist.cpp
114 lines (94 loc) · 2.52 KB
/
honeyheist.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
#include <bits/stdc++.h>
using namespace std;
pair<int,vector<vector<int>>> buildrows(int n) {
vector<int> lens;
for(int i = 0; i <= n-1; i++) {
lens.push_back(n+i);
}
for(int i = n-2; i >= 0; i--) {
lens.push_back(n+i);
}
vector<vector<int>> actual(lens.size());
int t = 0;
for(int i = 0; i < lens.size(); i++) {
for(int j = 0; j < lens[i]; j++) {
actual[i].push_back(t);
t++;
}
}
return {t,actual};
}
int main() {
int r, n, start, end, m;
cin >> r >> n >> start >> end >> m;
// Build rows, count total squares
pair<int,vector<vector<int>>> p = buildrows(r);
int total = p.first;
vector<vector<int>> idx = p.second;
// Build Adjacency
vector<vector<int>> adj(total);
vector<pair<int,int>> edges;
// Join across
for(auto list : idx) {
for(int i = 1; i < list.size(); i++) {
edges.push_back({list[i],list[i-1]});
}
}
// Join up (top half)
for(int i = 1; i < r; i++) {
// join first straight up
edges.push_back({idx[i][0], idx[i-1][0]});
// join others left and up
for(int j = 1; j < idx[i].size(); j++) {
if(j != idx[i].size()-1) {
edges.push_back({idx[i][j], idx[i-1][j]});
}
edges.push_back({idx[i][j], idx[i-1][j-1]});
}
}
// Join up (bottom half)
for(int i = r; i < 2*r-1; i++) {
// join all right and up
for(int j = 0; j < idx[i].size(); j++) {
edges.push_back({idx[i][j], idx[i-1][j]});
edges.push_back({idx[i][j], idx[i-1][j+1]});
}
}
for(auto i : edges) {
adj[i.first].push_back(i.second);
adj[i.second].push_back(i.first);
}
// Make sure start and end are correct
start--; end--;
// Build visited
vector<bool> vis(total, false);
vector<int> dist(total, 1000000);
// Mark used squares
for(int i = 0; i < m; i++) {
int t;
cin >> t;
t--;
vis[t] = true;
}
queue<int> q;
q.push(start);
dist[start] = 0;
vis[start] = true;
while(!q.empty()) {
int curr = q.front();
q.pop();
for(auto next : adj[curr]) {
if(!vis[next]) {
vis[next] = true;
dist[next] = dist[curr] + 1;
q.push(next);
}
}
}
if(dist[end] <= n) {
cout << dist[end] << endl;
}
else {
cout << "No" << endl;
}
}