Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
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 | Output for the Sample Input |
---|---|
SAMPLE |
SAMPLE |
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. |
ACM World Finals 2000, Problem A
time:32ms
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <list> 13 #include <iomanip> 14 #include <cstdlib> 15 #include <sstream> 16 using namespace std; 17 const int INF=0x5fffffff; 18 const double EXP=1e-8; 19 const int MS=10; 20 struct node 21 { 22 int r,c,dir; //从哪个dir到达r,c 23 node(int r=0,int c=0,int dir=0):r(r),c(c),dir(dir){} 24 }; 25 node p[MS][MS][4]; 26 bool edge[MS][MS][4][3]; //从dir方向到达r,c,是否可以向FLR方向前进 27 int d[MS][MS][4]; // 距离 也起防止重复访问的作用 28 const char *dirs="ESWN"; 29 const char *turns="FLR"; 30 31 int dd[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; 32 int r0,c0,r1,c1,r2,c2,dir; 33 34 int dir_id(char c) 35 { 36 return strchr(dirs,c)-dirs; 37 } 38 39 int turn_id(char c) 40 { 41 return strchr(turns,c)-turns; 42 } 43 node walk(const node &u,int turn) 44 { 45 int dir=u.dir; 46 if(turn==1) 47 dir=(dir-1+4)%4; 48 if(turn==2) 49 dir=(dir+1)%4; 50 return node(u.r+dd[dir][0],u.c+dd[dir][1],dir); 51 } 52 53 bool inside(int r,int c) 54 { 55 return r>=1&&r<=9&&c>=1&&c<=9; 56 } 57 58 bool read_case() 59 { 60 char s[100],s2[100]; 61 if(scanf("%s%d%d%s%d%d",s,&r0,&c0,s2,&r2,&c2)!=6) 62 return false; 63 printf("%s\n",s); 64 dir=dir_id(s2[0]); 65 r1=r0+dd[dir][0]; 66 c1=c0+dd[dir][1]; 67 memset(edge,0,sizeof(edge)); 68 while(1) 69 { 70 int r,c; 71 scanf("%d",&r); 72 if(r==0) 73 break; 74 scanf("%d",&c); 75 while(scanf("%s",s)==1&&s[0]!='*') 76 { 77 for(int i=1;i<(int)strlen(s);i++) 78 edge[r][c][dir_id(s[0])][turn_id(s[i])]=1; 79 } 80 } 81 return true; 82 } 83 84 void print_node(node u) 85 { 86 vector<node > nodes; 87 while(1) 88 { 89 nodes.push_back(u); 90 if(d[u.r][u.c][u.dir]==0) 91 break; 92 u=p[u.r][u.c][u.dir]; 93 } 94 nodes.push_back(node(r0,c0,dir)); 95 int cnt=0; 96 for(int i=nodes.size()-1;i>=0;i--) 97 { 98 if(cnt%10==0) 99 printf(" "); 100 printf(" (%d,%d)",nodes[i].r,nodes[i].c); 101 if(++cnt%10==0) 102 printf("\n"); 103 } 104 if(nodes.size()%10!=0) 105 printf("\n"); 106 107 } 108 109 110 void solve() 111 { 112 queue<node > que; 113 memset(d,-1,sizeof(d)); 114 node u(r1,c1,dir); 115 d[u.r][u.c][dir]=0; //注意厨师起点 116 que.push(u); 117 while(!que.empty()) 118 { 119 node u=que.front(); 120 que.pop(); 121 if(u.r==r2&&u.c==c2) 122 { 123 print_node(u); 124 return ; 125 } 126 for(int i=0;i<3;i++) 127 { 128 node v=walk(u,i); 129 if(edge[u.r][u.c][u.dir][i]&&inside(v.r,v.c)&&d[v.r][v.c][v.dir]<0) 130 { 131 d[v.r][v.c][v.dir]=d[u.r][u.c][u.dir]+1; 132 p[v.r][v.c][v.dir]=u; 133 que.push(v); 134 } 135 } 136 } 137 printf(" No Solution Possible\n"); 138 } 139 140 int main() 141 { 142 while(read_case()) 143 solve(); 144 return 0; 145 }