【Jason's_ACM_解题报告】Abbott’s Revenge

来源:互联网 发布:江苏省人工智能会议 编辑:程序博客网 时间:2024/04/28 18:36

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 year’s problem statement. But we are happy to report that Mr. Abbott has offered his expertise to this year’s 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 intersection 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. But if there are several solutions which are equally long, you can chose any of them.

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.
The first maze in the following sample input is the maze in Figure 1.

Sample Input
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
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-Through Arrow Maze

Figure 2: Robert Abbott’s Atlanta Maze

Robert Abbott’s 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 display 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 intended to be walked through.

For the adventurous, Figure 2 is a graphic of Robert Abbott’s 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 himself indicated that the maze is too complex and most people give up before finishing. Among the people that did not give up was Donald Knuth: it took him about thirty minutes to solve the maze.


此题十分重要!!BFS求无平行边图最短路径。求图最短路时,随时存储当前节点的上一节点,输出时则不需使用递归方式输出路径了。

要特别注意has_edge[][][][]数组的作用,由于从不同的方向来到一点可选择继续行走的方式不同,所以用数组表示,当前节点的位置及走来的方向,选择继续行走的方式是否可行。

注意:在某一点,是面朝的方向,转过后也是面朝的方向。



附代码如下:
#include<cstdio> #include<cstring>#include<queue>using namespace std;const char* dirs="NESW";const char* turns="FLR";const int dr[]={-1,0,1,0};const int dc[]={0,1,0,-1};struct NODE{int r,c,dir;NODE(int r=0,int c=0,int dir=0):r(r),c(c),dir(dir){}};NODE st,ed,st1;NODE pre[10][10][4];bool has_edge[10][10][4][3];int dis[10][10][4];int dir_id(char ch){return strchr(dirs,ch)-dirs;}int turn_id(char ch){return strchr(turns,ch)-turns;}bool inside(int x,int y){if(1<=x&&x<=9&&1<=y&&y<=9)return true;return false;}bool input(){char name[25],ch[25];if(scanf("%s%d%d%s%d%d",name,&st.r,&st.c,&ch,&ed.r,&ed.c)!=6)return false;printf("%s\n",name);st1.dir=dir_id(ch[0]);st1.r=st.r+dr[st1.dir];st1.c=st.c+dc[st1.dir];memset(has_edge,false,sizeof(has_edge));for(;;){int x,y;char str[5];scanf("%d",&x);if(!x)break;scanf("%d",&y);while(scanf("%s",str)==1&&str[0]!='*'){for(int i=1;i<strlen(str);i++){has_edge[x][y][dir_id(str[0])][turn_id(str[i])]=true;}}}return true;}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 NODE(u.r+dr[dir],u.c+dc[dir],dir);}void output(NODE u){vector<NODE> ans;for(;;){ans.push_back(u);if(!dis[u.r][u.c][u.dir])break;u=pre[u.r][u.c][u.dir];}ans.push_back(st);int tot=0;for(int i=ans.size()-1;i>=0;i--){if(tot%10==0)printf(" ");printf(" (%d,%d)",ans[i].r,ans[i].c);if(++tot%10==0)printf("\n");}if(ans.size()%10!=0)printf("\n");}void solve(){queue<NODE> q;memset(dis,-1,sizeof(dis));NODE u(st1.r,st1.c,st1.dir);dis[u.r][u.c][u.dir]=0;q.push(u);while(!q.empty()){NODE u=q.front();q.pop();if(u.r==ed.r&&u.c==ed.c){output(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&&dis[v.r][v.c][v.dir]<0)){dis[v.r][v.c][v.dir]=dis[u.r][u.c][u.dir]+1;pre[v.r][v.c][v.dir]=u;q.push(v);}}}printf("  No Solution Possible\n");}int main(){while(input()){solve();}return 0;}

0 0
原创粉丝点击