UVa816 Abbott's Revenge

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 Input

Output for the 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

  (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。

  一道BFS搜索最短路径的问题,与其他题目不同的是结点的转向有了限制,但也不算麻烦。

  首先根据输入构造has_edge[x][y][dir][turn]数组,表示位于xy朝向dir能否向turn转向。然后以位置(x,y)方向dir为状态宽搜即可。

  需要注意的是:

1、  以将sx sy向sdir方向移动一格为初始状态

2、  转向+移动算作一步。

【代码】

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<queue>
 4 #include<vector>
 5 using namespace std;
 6 
 7 const int maxn = 10;
 8 const int maxm=1000+10;
 9 const char* dirs="NESW";
10 const char* turns="FLR";
11 const int dx[]={-1,0,1,0};
12 const int dy[]={0,1,0,-1};
13 struct Node{
14     int x,y,dir;
15 };
16 int id_dirs(char c) {
17     return strchr(dirs,c)-dirs;
18 }
19 int id_turns(char c) {
20     return strchr(turns,c)-turns;
21 }
22 
23 bool has_edge[maxn][maxn][5][4];
24 int n,m,sx,sy,sdir,ex,ey;
25 
26 bool vis[maxn][maxn][5];
27 Node p[maxn][maxn][5];
28 void print(Node u) {
29     vector<Node> ans;
30     for(;;) {
31         ans.push_back(u);
32         if(u.x==sx && u.y==sy && u.dir==sdir) break;
33         u=p[u.x][u.y][u.dir];
34     }
35     ans.push_back((Node){sx-dx[sdir],sy-dy[sdir],sdir});
36     int cnt=0;
37     for(int i=ans.size()-1;i>=0;i--) {
38         if(cnt%10==0) putchar(' ');
39         printf(" (%d,%d)",ans[i].x,ans[i].y);
40         if(++cnt%10==0) putchar('
');
41     }
42     if(ans.size()%10!=0) putchar('
');
43 }
44 Node walk(Node u,int i) {
45     if(i==1) { u.dir=(u.dir+3)%4; }
46     if(i==2) { u.dir=(u.dir+1)%4; }
47     return (Node){u.x+dx[u.dir],u.y+dy[u.dir],u.dir} ;
48 }
49 bool inside(int x,int y) {
50     return x>0 && x<=9 && y>0 && y<=9;
51 }
52 void BFS() {
53     memset(vis,0,sizeof(vis));
54     queue<Node> q;
55     q.push((Node){sx,sy,sdir});
56     vis[sx][sy][sdir]=1;
57     while(!q.empty()) {
58         Node u=q.front(); q.pop();
59         if(u.x==ex && u.y==ey) { print(u); return ; }
60         for(int i=0;i<3;i++) {
61             Node v=walk(u,i);
62             if(has_edge[u.x][u.y][u.dir][i] && inside(v.x,v.y) && !vis[v.x][v.y][v.dir]) {
63                 vis[v.x][v.y][v.dir]=1;
64                 p[v.x][v.y][v.dir]=u;
65                 q.push(v);
66             }
67         }
68     }
69     printf("  No Solution Possible
");
70 }
71 
72 int main() {
73     char T[maxm];
74     while(scanf("%s",&T))
75     {
76         char s[maxm];
77         if (scanf("%d%d%s%d%d",&sx,&sy,&s,&ex,&ey) !=5 ) break;
78         sdir=id_dirs(s[0]);
79         sx += dx[sdir] , sy += dy[sdir];
80         memset(has_edge,0,sizeof(has_edge));
81         int x,y;
82         while(scanf("%d",&x) && x) {
83             scanf("%d",&y);
84             while(scanf("%s",&s) && s[0]!='*') {
85                 int dir=id_dirs(s[0]);
86                 for(int i=1;i<strlen(s);i++) has_edge[x][y][dir][id_turns(s[i])]=1;
87             }
88         }
89         printf("%s
",T);
90         BFS();
91     }
92     return 0;
93 }
原文地址:https://www.cnblogs.com/lidaxin/p/4919306.html