Abbott的复仇(Abbott's Revenge)

来源:互联网 发布:刚转行做程序员后悔了 编辑:程序博客网 时间:2024/04/28 00:19
Abbott's Revenge

Time limit: 3.000 seconds

Abbott’s Revenge

Abbott’s Revenge 

The 1999 World FinalsContest included a problem based on a “dicemaze.” At the time the problem was written, the judges were unable todiscover the original source of the dice maze concept. Shortly afterthe contest, however, Mr. Robert Abbott, the creator of numerous mazesand an author on the subject, contacted the contest judges andidentified himself as the originator of dice mazes. We regret that wedid not credit Mr. Abbott for his original concept in last year’sproblem statement. But we are happy to report that Mr. Abbott hasoffered his expertise to this year’s contest with his original andunpublished “walk-through arrow mazes.”

As are most mazes, awalk-through arrow maze is traversed by moving from intersection tointersection until the goal intersection is reached. As eachintersection is approached from a given direction, a sign near theentry to the intersection indicates in which directions theintersection can be exited. These directions are always left, forwardor right, or any combination of these.

Figure 1 illustrates awalk-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 youapproach from the south indicates that you may exit (1,1) only bymaking 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 isalso 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 havethe option of continuing straight or turning left. Continuing straightwould take you on toward (1,1), while turning left would take you southto (2,2). The actual (unique) solution to this maze is the followingsequence 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 programto solve valid walk-through arrow mazes. Solving a maze means (ifpossible) finding a route through the maze that leaves the Entrance inthe prescribed direction, and ends in the Goal. This route should notbe longer than necessary, of course. But if there are several solutionswhichare equally long, you can chose any of them.

Input 

The input file willconsist of one or more arrow mazes. The first line of each mazedescription contains the name of the maze, which is an alphanumericstring of no more than 20 characters. The next line contains, in thefollowing order, the starting row, the starting column, the startingdirection, the goal row, and finally the goal column. All are delimitedby a single space. The maximum dimensions of a maze for this problemare 9 by 9, so all row and column numbers are single digits from 1 to9. The starting direction is one of the characters N, S, E or W,indicating north, south, east and west, respectively.

All remaining inputlines for a maze have this format: two integers, one or more groups ofcharacters, and a sentinel asterisk, again all delimited by a singlespace. The integers represent the row and column, respectively, of amaze intersection. Each character group represents a sign at thatintersection. The first character in the group is N, S, E or W toindicate in what direction of travel the sign would be seen. Forexample, S indicates that this is the sign that is seen when travellingsouth. (This is the sign posted at the north entrance to theintersection.) Following this first direction character are one tothree arrow characters. These can be L, F or R indicating left,forward, and right, respectively.

The list ofintersections is concluded by a line containing a single zero in thefirst column. The next line of the input starts the next maze, and soon. The end of input is the word END on a single line by itself.

Output 

For each maze, theoutput file should contain a line with the name of the maze, followedby 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 allother 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 thesolution should contain exactly 10 intersections.

The first maze in thefollowing sample input is the maze in Figure 1.



Sample InputOutput for the Sample Input
SAMPLE3 1 N 3 31 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 *0NOSOLUTION3 1 N 3 21 1 WL NR *1 2 NL ER *2 1 SL WR NFR *2 2 SR EL *0END
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



Figure 1: An Example Walk-ThroughArrow Maze



Figure 2: Robert Abbott’s AtlantaMaze


Robert Abbott’swalk-through arrow mazes are actually intended forlarge-scale construction, not paper. Although his mazes areunpublished, some of them have actually been built. One of these is ondisplay at an Atlanta museum. Others have been constructed by theAmerican Maze Company over the past two summers. As their name suggeststhese mazes are intended to be walked through.

For the adventurous, Figure 2 is a graphic of Robert Abbott’sAtlanta maze. Solving it isquite difficult, even when you have an overview of the entire maze.Imagine trying to solve this by actually walking through the maze andonly seeing one sign at a time! Robert Abbott himself indicated thatthe maze is too complex and most people give up before finishing. Amongthe people that did not give up was Donald Knuth: it took him aboutthirty minutes to solve the maze.



ACM World Finals 2000, Problem A

【分析】

       利用队列实现广度搜索BFS来遍历图寻找最短路径。

       用一个三元组(r, c, dir)表示“位于(r, c),面朝dir”这个状态。假设入口位置为(r0, c0),朝向为dir,则初始状态并不是(r0, c0, dir),而是(r1, c1, dir),其中,(r1, c1)是(r0, c0)沿着方向dir走一步之后的坐标。此处用d[r][c][dir]表示初始状态到(r, c, dir)的最短路长度,并且用p[r][c][dir]保存了状态(r, c, dir)在BFS树中的父结点。

       在输入过程中,读取r0,c0,dir,并且计算出r1,c1即初始状态位置,读取终点位置r2,c2。读取交叉点的位置允许出去的方向,将朝向 dir 和转弯方向 turn 转化为编号0~3和0~2,并储存在has_edge数组中,其中has_edge[r][c][dir][turn]表示当前状态是(r, c, dir),是否可以沿着转弯方向turn行走。在BFS遍历的过程中,可以依据has_edge[r][c][dir][turn]判断位置(r, c, dir)是否可以这样转弯走到新状态。

用C++语言编写程序,代码如下:

#include<iostream>#include<string>#include<cstring>#include<queue>using namespace std;string maze_name;int r0, c0, r1, c1, dir, r2, c2;const char* dirs = "NESW";const char* turns = "FLR";int dir_id(char c) {return (strchr(dirs, c) - dirs);}int turn_id(char c) {return (strchr(turns, c) - turns);}const int dr[] = {-1, 0, 1, 0};const int dc[] = {0, 1, 0, -1};const int maxn = 20;int has_edge[maxn][maxn][maxn][maxn];void read_edge() {memset(has_edge, 0, sizeof(has_edge));int pr, pc, pdir, pturn;string temp;while (cin >> pr) {if (pr == 0) break;cin >> pc;while (cin >> temp) {if (temp == "*") break;pdir = dir_id(temp[0]);for (int i = 1; i < temp.length(); i++) {pturn = turn_id(temp[i]);has_edge[pr][pc][pdir][pturn] = 1;}}}}bool read_list() {cin>>maze_name;if (maze_name == "END") return false;char dir0;cin >> r0 >> c0 >> dir0 >> r2 >> c2;dir = dir_id(dir0);r1 = r0 + dr[dir];c1 = c0 + dc[dir];read_edge();return true;}struct Node {int r, c, dir;Node(int r = 0, int c = 0, int dir = 0) : r(r), c(c), dir(dir) {}};int d[maxn][maxn][maxn];Node p[maxn][maxn][maxn];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;//r + dr[dir], c + dc[dir]表示在(r, c)位置朝dir方向前进一格。//所以向左转或向右转时,我们只要先改变朝向后再让原先的行r加上dr[dir]和列c加上dc[dir]就行了return Node(u.r + dr[dir], u.c + dc[dir], dir);}bool inside(int r, int c) {if (r >= 1 && r <= 9 && c >= 1 && c <= 9)return true;return false;}void print_ans(Node u) {//从目标结点逆序追溯到初始结点vector<Node> nodes;while (true) {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(Node(r0, c0, dir));cout << maze_name << endl;//打印解,每行10个int cnt = 0;for (int i = nodes.size() - 1; i >= 0; i--) {if (cnt % 10 == 0) cout << " ";cout << " (" << nodes[i].r << "," << nodes[i].c << ")";if (++cnt % 10 == 0) cout << endl;}if (nodes.size() % 10 != 0) cout << endl;}void solve() {queue<Node> q;memset(d, -1, sizeof(d));Node n1(r1, c1, dir);q.push(n1);d[r1][c1][dir] = 0;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);}}}cout << maze_name << endl;cout << "  No Solution Possible" << endl;}int main() {while (read_list()) {solve();}return 0;}



0 0