-
Notifications
You must be signed in to change notification settings - Fork 5
/
UVA-816.cpp
161 lines (146 loc) · 2.78 KB
/
UVA-816.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAXN 12
using namespace std;
const int LT[4] = {-1, 0, 1, 0};
const int RT[4] = {0, 1, 0, -1};
struct NodePath {
int l, r;
int dir, dirold;
};
// 结点来源
NodePath prevArr[MAXN][MAXN][5];
// 结点允许走的路
bool pathArr[MAXN][MAXN][5][5];
int begl, begr, endl, endr, dirbeg;
int dir2int(char c) {
switch(c) {
case 'N': return 0;
case 'E': return 1;
case 'S': return 2;
case 'W': return 3;
}
}
int turn2int(char c) {
switch(c) {
case 'F': return 0;
case 'L': return 3;
case 'R': return 1;
}
}
void inputNodes() {
int l, r, dir;
int i, j;
char s[10];
while(1) {
scanf("%d %d", &l, &r);
if(l == 0) {
break;
}
while(scanf("%s", s) > 0) {
if(s[0] == '*') {
break;
}
dir = dir2int(s[0]);
for(i = 1; s[i] != 0; ++i) {
pathArr[l][r][dir][turn2int(s[i])] = true;
}
}
}
}
void clear() {
memset(prevArr, 0, sizeof(prevArr));
memset(pathArr, 0, sizeof(pathArr));
begl = 0;
begr = 0;
endl = 0;
endr = 0;
}
NodePath createNodePath(int l ,int r, int dir, int i) {
NodePath np;
np.l = l + LT[(dir + i) % 4];
np.r = r + RT[(dir + i) % 4];
np.dir = (dir + i) % 4;
np.dirold = dir;
return np;
}
NodePath count(int dir) {
queue<NodePath> qu;
int i, j, k;
qu.push(createNodePath(begl, begr, dir, 0));
while(!qu.empty()) {
NodePath np = qu.front();
qu.pop();
if(np.l == endl && np.r == endr) {
return np;
}
for(i = 0; i < 4; ++i) {
if(pathArr[np.l][np.r][np.dir][i]) {
NodePath npw = createNodePath(np.l, np.r, np.dir, i);
if(prevArr[npw.l][npw.r][npw.dir].l) {
continue;
}
prevArr[npw.l][npw.r][npw.dir] = np;
qu.push(npw);
}
}
}
return {0, 0, 0, 0};
}
NodePath getPrevPath(NodePath np) {
np = prevArr[np.l][np.r][np.dir];
return np;
}
void countRouter(vector<NodePath> &v, NodePath np) {
while(np.l != 0 && (np.l != begl || np.r != begr || np.dir != dirbeg || np.dirold != dirbeg)) {
v.push_back(np);
np = getPrevPath(np);
}
v.push_back({
begl, begr
});
}
void output(vector<NodePath> &v) {
NodePath np;
int i, j;
for(i = 0; i < v.size() / 2; ++i) {
np = v[i];
v[i] = v[v.size() - i - 1];
v[v.size() - i - 1] = np;
}
printf(" ");
for(i = 0; i < v.size(); ++i) {
if(i != 0 && i % 10 == 0) {
printf("\n ");
}
if(i != 0) {
putchar(' ');
}
printf("(%d,%d)", v[i].l, v[i].r);
}
printf("\n");
}
int main() {
char s[30];
char dir[10];
while(scanf("%s", s) > 0) {
if(strcmp(s, "END") == 0) {
break;
}
clear();
scanf("%d %d %s %d %d", &begl, &begr, dir, &endl, &endr);
dirbeg = dir2int(dir[0]);
inputNodes();
printf("%s\n", s);
NodePath np = count(dirbeg);
if(np.l == 0) {
printf(" No Solution Possible\n");
continue;
}
vector<NodePath> v;
countRouter(v, np);
output(v);
}
return 0;
}