forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherraticants.cpp
104 lines (92 loc) · 2.45 KB
/
erraticants.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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int START = 65;
int inf = 1 << 30;
struct spot {
bool N;
bool S;
bool E;
bool W;
int dist = inf;
};
int main() {
int cases;
cin >> cases;
while(cases--) {
// Build grid
vector<vector<spot>> v;
spot s;
s.N = false, s.S = false;
s.E = false, s.W = false;
s.dist = inf;
v.resize(130, vector<spot>(130, s));
// Read in path
int n;
cin >> n;
vector<char> path(n);
for(int i = 0; i < path.size(); i++) {
cin >> path[i];
}
// Fill in grid
int x = START;
int y = START;
for(auto c : path) {
if(c == 'N') {
v[x][y].N = true;
y++;
v[x][y].S = true;
}
if(c == 'S') {
v[x][y].S = true;
y--;
v[x][y].N = true;
}
if(c == 'E') {
v[x][y].E = true;
x--;
v[x][y].W = true;
}
if(c == 'W') {
v[x][y].W = true;
x++;
v[x][y].E = true;
}
}
queue<pair<int, int>> q;
q.push({START, START});
v[START][START].dist = 0;
while(!q.empty()) {
int currx = q.front().first;
int curry = q.front().second;
q.pop();
spot curr = v[currx][curry];
if(curr.N) {
if(curr.dist + 1 < v[currx][curry+1].dist) {
v[currx][curry+1].dist = curr.dist + 1;
q.push({currx, curry+1});
}
}
if(curr.S) {
if(curr.dist + 1 < v[currx][curry-1].dist) {
v[currx][curry-1].dist = curr.dist + 1;
q.push({currx, curry-1});
}
}
if(curr.E) {
if(curr.dist + 1 < v[currx-1][curry].dist) {
v[currx-1][curry].dist = curr.dist + 1;
q.push({currx-1, curry});
}
}
if(curr.W) {
if(curr.dist + 1 < v[currx+1][curry].dist) {
v[currx+1][curry].dist = curr.dist + 1;
q.push({currx+1, curry});
}
}
}
cout << v[x][y].dist << endl;
}
}