UVa816 - Abbott's Revenge
题设
The 1999 World Finals Contest included a problem based on a dice maze. At the time the problem was written, the judges were unable to discover the original source of the dice maze concept. Shortly after the contest, however, Mr. Robert Abbott, the creator of numerous mazes and an author on the subject, contacted the contest judges and identified himself as the originator of dice mazes. We regret that we did not credit Mr. Abbott for his original concept in last years problem statement. But we are happy to report that Mr. Abbott has offered his expertise to this years contest with his original and unpublished walk-through arrow mazes.
As are most mazes, a walk-through arrow maze is traversed by moving from intersection to inter- section until the goal intersection is reached. As each intersection is approached from a given direction, a sign near the entry to the intersection indicates in which directions the intersection can be exited. These directions are always left, forward or right, or any combination of these.
Figure 1 illustrates a walk-through arrow maze. The intersections are identified as (row, column) pairs, with the upper left being (1,1). The Entrance intersection for Figure 1 is (3,1), and the Goal intersection is (3,3). You begin the maze by moving north from (3,1). As you walk from (3,1) to (2,1), the sign at (2,1) indicates that as you approach (2,1) from the south (traveling north) you may continue to go only forward. Continuing forward takes you toward (1,1). The sign at (1,1) as you approach from the south indicates that you may exit (1,1) only by making a right. This turns you to the east now walking from (1,1) toward (1,2). So far there have been no choices to be made. This is also the case as you continue to move from (1,2) to (2,2) to (2,3) to (1,3). Now, however, as you move west from (1,3) toward (1,2), you have the option of continuing straight or turning left. Continuing straight would take you on toward (1,1), while turning left would take you south to (2,2). The actual (unique) solution to this maze is the following sequence of intersections: (3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (1,3) (1,2) (1,1) (2,1) (2,2) (1,2) (1,3) (2,3) (3,3).
You must write a program to solve valid walk-through arrow mazes. Solving a maze means (if possible) finding a route through the maze that leaves the Entrance in the prescribed direction, and ends in the Goal. This route should not be longer than necessary, of course.
Input
The input file will consist of one or more arrow mazes. The first line of each maze description contains the name of the maze, which is an alphanumeric string of no more than 20 characters. The next line contains, in the following order, the starting row, the starting column, the starting direction, the goal row, and finally the goal column. All are delimited by a single space. The maximum dimensions of a maze for this problem are 9 by 9, so all row and column numbers are single digits from 1 to 9. The starting direction is one of the characters N, S, E or W, indicating north, south, east and west, respectively.
All remaining input lines for a maze have this format: two integers, one or more groups of characters, and a sentinel asterisk, again all delimited by a single space. The integers represent the row and column, respectively, of a maze intersection. Each character group represents a sign at that intersection. The first character in the group is ‘N’, ‘S’, ‘E’ or ‘W’ to indicate in what direction of travel the sign would be seen. For example, ‘S’ indicates that this is the sign that is seen when travelling south. (This is the sign posted at the north entrance to the intersection.) Following this first direction character are one to three arrow characters. These can be ‘L’, ‘F’ or ‘R’ indicating left, forward, and right, respectively.
The list of intersections is concluded by a line containing a single zero in the first column. The next line of the input starts the next maze, and so on. The end of input is the word ‘END’ on a single line by itself.
Output
For each maze, the output file should contain a line with the name of the maze, followed by one or more lines with either a solution to the maze or the phrase ‘No Solution Possible’. Maze names should start in column 1, and all other lines should start in column 3, i.e., indented two spaces. Solutions should be output as a list of intersections in the format (R,C) in the order they are visited from the start to the goal, should be delimited by a single space, and all but the last line of the solution should contain exactly 10 intersections.
Note:
Robert Abbotts walk-through arrow mazes are actually intended for large-scale construction, not paper. Although his mazes are unpublished, some of them have actually been built. One of these is on dis- play at an Atlanta museum. Others have been constructed by the American Maze Company over the past two summers. As their name suggests these mazes are in- tended to be walked through.
For the adventurous, Figure 2 a graphic of Robert Abbotts Atlanta maze. Solving it is quite difficult, even when you have an overview of the entire maze. Imagine trying to solve this by actually walking through the maze and only seeing one sign at a time! Robert Abbott him- self indicated that the maze is too com- plex and most people give up before fin- ishing. Among the people that did not give up was Donald Knuth: it took him about thirty minutes to solve the maze.
The first maze in the following sample input is the maze in Figure 1.
Sample Input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SAMPLE
3 1 N 3 3
1 1 WL NR *
1 2 WLF NR ER *
1 3 NL ER *
2 1 SL WR NF *
2 2 SL WF ELF *
2 3 SFR EL *
0
NOSOLUTION
3 1 N 3 2
1 1 WL NR *
1 2 NL ER *
2 1 SL WR NFR *
2 2 SR EL *
0
END
Sample Output
1
2
3
4
5
SAMPLE
(3,1) (2,1) (1,1) (1,2) (2,2) (2,3) (1,3) (1,2) (1,1) (2,1)
(2,2) (1,2) (1,3) (2,3) (3,3)
NOSOLUTION
No Solution Possible
题解
题设很长,图片未加进来,请自行前往UVa官网查看。这是一道BFS题,这道题的核心要点是记录状态,这里的状态不再是最基础的BFS中二维的坐标位置,而是三维的坐标+方向。同时,需要对输入进行翻译,转化为整型来存储,这道题展示了一种非常好的转化方式。此外,这道题还运用了路径存储和回溯,可谓是“面面俱到“。
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
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const char* dirs = "NESW";
const char* turns = "FLR";
int dir_id(char c) { return strchr(dirs, c) - dirs; } // strchr的运用
int turn_id(char c) { return strchr(turns, c) - turns; }
const int dr[] = {-1, 0, 1, 0}; // dr和dc与上面的两个函数相对应,十分巧妙
const int dc[] = {0, 1, 0, -1};
struct Node {
int r, c, dir; // 三维节点表示
};
int d[10][10][4]; // 存储距离,注意有方向
Node p[10][10][4]; // 存储父节点,注意这里也是有方向的
bool has_edge[10][10][4][3]; // 在输入时记录在某点某方向进入是否能转向
int r0, c0, dir1, r1, c1, r2, c2;
void init() {
memset(d, -1, sizeof d);
memset(has_edge, false, sizeof has_edge);
}
void print_ans(Node u) {
vector<Node> nodes; // 用vector记录,便于回溯打印
for (;;) {
nodes.push_back(u);
if (d[u.r][u.c][u.dir] == 0) {
break;
}
u = p[u.r][u.c][u.dir];
}
nodes.push_back({r0, c0, dir1});
int cnt = 0;
for (int i = nodes.size() - 1; i >= 0; i--) {
if (cnt % 10 == 0) {
printf(" ");
}
printf(" (%d,%d)", nodes[i].r, nodes[i].c);
if (++cnt % 10 == 0) {
printf("\n");
}
}
if (nodes.size() % 10 != 0) {
printf("\n");
}
}
Node walk(const Node& u, int turn) {
int dir = u.dir;
if (turn == 1) {
dir = (dir + 3) % 4; // 非常巧妙的处理
}
if (turn == 2) {
dir = (dir + 1) % 4;
}
return {u.r + dr[dir], u.c + dc[dir], dir};
}
bool inside(int r, int c) { return r >= 1 && r <= 9 && c >= 1 && c <= 9; }
void solve() {
queue<Node> q;
d[r1][c1][dir1] = 0;
q.push({r1, c1, dir1});
while (!q.empty()) {
Node u = q.front();
q.pop();
if (u.r == r2 && u.c == c2) {
print_ans(u);
return;
}
for (int i = 0; i < 3; i++) {
Node v = walk(u, i);
if (has_edge[u.r][u.c][u.dir][i] && inside(v.r, v.c) &&
d[v.r][v.c][v.dir] < 0) {
d[v.r][v.c][v.dir] = d[u.r][u.c][u.dir] + 1;
p[v.r][v.c][v.dir] = u;
q.push(v);
}
}
}
printf(" No Solution Possible\n");
}
int main() {
string name;
while (cin >> name && name != "END") {
init();
char dir0;
cin >> r0 >> c0 >> dir0 >> r2 >> c2;
dir1 = dir_id(dir0);
r1 = r0 + dr[dir1], c1 = c0 + dc[dir1];
int r, c;
while (cin >> r && r) {
cin >> c;
string s;
while (cin >> s && s[0] != '*') {
int dir = dir_id(s[0]);
for (int i = 1; i < s.length(); i++) {
int turn = turn_id(s[i]);
has_edge[r][c][dir][turn] = true;
}
}
}
cout << name << '\n';
solve();
}
return 0;
}