poj 3083Children of the Candy Corn

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. 

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.) 

As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'. 

Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#'). 

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9

这题想了好久,要求三个数,一个是靠左先 一个靠右先,一个最短到终点的距离,其中最短距离很好求,简单bfs就行了
就是其他两个不太好想。本想着有什么简单的方法的,可结果发了两三天都没想出来,最后就在纸上画它行走的方向图,
发现每次走完之后所改变的方向都是对应的,即一个方向图向某一方向同时移动一位,构成对应的方向关系- -虽然想到
这里但是还是没什么简单的方法。。DT时。。决定用枚举写了- -结果成了现在的冗长的代码。。
View Code
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <queue>
  6 using namespace std;
  7 const int N=45;
  8 const int east=0;
  9 const int north=1;
 10 const int west=2;
 11 const int south=3;
 12 int dx[]={-1,0,1,0};
 13 int dy[]={0,1,0,-1};
 14 struct road
 15 {
 16     int x,y;
 17     int step;
 18 }start,end,mid,t;
 19 int vis[N][N];
 20 int map[N][N];
 21 int w,h;
 22 int bfs()
 23 {
 24     int i;
 25     memset(vis,0,sizeof(vis));
 26     queue<road>q;
 27     q.push(start);
 28     vis[start.x][start.y]=1;
 29     while(!q.empty())
 30     {
 31         mid=q.front();
 32         q.pop();
 33         if(mid.x==end.x&&mid.y==end.y)
 34         {
 35             end.step=mid.step;
 36             return end.step;
 37         }
 38         for(i=0;i<4;i++)
 39         {
 40             t.x=mid.x+dx[i];
 41             t.y=mid.y+dy[i];
 42             if(t.x>=0&&t.x<h&&t.y>=0&&t.y<w&&map[t.x][t.y]&&!vis[t.x][t.y])
 43             {
 44                 vis[t.x][t.y]=1;
 45                 t.step=mid.step+1;
 46                 if(t.x==end.x&&t.y==end.y)
 47                     return t.step;
 48                 q.push(t);
 49             }
 50         }
 51     }
 52 }
 53 int left(int x,int y,int d)
 54 {
 55     int cnt=1;
 56     while(map[x][y]!=2)
 57     {
 58         switch(d)
 59         {
 60             case north:
 61                 if(y-1>=0&&map[x][y-1])
 62                     y--,d=west;
 63                 else if(x-1>=0&&map[x-1][y])
 64                     x--,d=north;
 65                 else if(y+1<w&&map[x][y+1])
 66                     y++,d=east;
 67                 else if(x+1<h&&map[x+1][y])
 68                     x++,d=south;
 69                 cnt++;
 70                 break;
 71             case west:
 72                 if(x+1<h&&map[x+1][y])
 73                     x++,d=south;
 74                 else if(y-1>=0&&map[x][y-1])
 75                     y--,d=west;
 76                 else if(x-1>=0&&map[x-1][y])
 77                     x--,d=north;
 78                 else if(y+1<w&&map[x][y+1])
 79                     y++,d=east;
 80                 cnt++;
 81                 break;
 82             case south:
 83                 if(y+1<w&&map[x][y+1])
 84                     y++,d=east;
 85                 else if(x+1<h&&map[x+1][y])
 86                     x++,d=south;
 87                 else if(y-1>=0&&map[x][y-1])
 88                     y--,d=west;
 89                 else if(x-1>=0&&map[x-1][y])
 90                     x--,d=north;
 91                 cnt++;
 92                 break;
 93             case east:
 94                 if(x-1>=0&&map[x-1][y])
 95                     x--,d=north;
 96                 else if(y+1<w&&map[x][y+1])
 97                     y++,d=east;
 98                 else if(x+1<h&&map[x+1][y])
 99                     x++,d=south;
100                 else if(y-1>=0&&map[x][y-1])
101                     y--,d=west;
102                 cnt++;
103                 break;
104         }
105     }
106     return cnt;
107 }
108 int right(int x,int y,int d)
109 {
110     int cnt=1;
111     while(map[x][y]!=2)
112     {
113         switch(d)
114         {
115             case north:
116                 if(y+1<w&&map[x][y+1])
117                     y++,d=east;
118                 else if(x-1>=0&&map[x-1][y])
119                     x--,d=north;
120                 else if(y-1>=0&&map[x][y-1])
121                     y--,d=west;
122                 else if(x+1<h&&map[x+1][y])
123                     x++,d=south;
124                 cnt++;
125                 break;
126             case west:
127                 if(x-1>=0&&map[x-1][y])
128                     x--,d=north;
129                 else if(y-1>=0&&map[x][y-1])
130                     y--,d=west;
131                 else if(x+1<h&&map[x+1][y])
132                     x++,d=south;
133                 else if(y+1<w&&map[x][y+1])
134                     y++,d=east;
135                 cnt++;
136                 break;
137             case south:
138                 if(y-1>=0&&map[x][y-1])
139                     y--,d=west;
140                 else if(x+1<h&&map[x+1][y])
141                     x++,d=south;
142                 else if(y+1<w&&map[x][y+1])
143                     y++,d=east;
144                 else if(x-1>=0&&map[x-1][y])
145                     x--,d=north;
146                 cnt++;
147                 break;
148             case east:
149                 if(x+1<h&&map[x+1][y])
150                     x++,d=south;
151                 else if(y+1<w&&map[x][y+1])
152                     y++,d=east;
153                 else if(x-1>=0&&map[x-1][y])
154                     x--,d=north;
155                 else if(y-1>=0&&map[x][y-1])
156                     y--,d=west;
157                 cnt++;
158                 break;
159         }    
160     }
161     return cnt;
162 }
163 int main()
164 {
165     int t,i,j,dir;
166     char s[50];
167     scanf("%d",&t);
168     while(t--)
169     {
170         scanf("%d%d",&w,&h);
171         for(i=0;i<h;i++)
172         {
173             scanf("%s",s);
174             for(j=0;s[j];j++)
175                {
176                 if(s[j]=='.'||s[j]=='E')
177                 {
178                     if(s[j]=='E')
179                         map[i][j]=2;
180                     else
181                         map[i][j]=1;
182                 }
183                 if(s[j]=='#'||s[j]=='S')
184                     map[i][j]=0;
185                 if(s[j]=='S')
186                 {
187                     start.x=i;
188                     start.y=j;
189                     start.step=1;
190                 }
191                 if(s[j]=='E')
192                 {
193                     end.x=i;
194                     end.y=j;
195                 }
196             }
197         }
198         if(start.x==0) dir=south;
199         else if(start.x==h-1) dir=north;
200         else if(start.y==0) dir=east;
201         else if(start.y==w-1) dir=west;
202         printf("%d %d %d\n",left(start.x,start.y,dir),right(start.x,start.y,dir),bfs());
203     }
204     return 0;
205 }
原文地址:https://www.cnblogs.com/wilsonjuxta/p/2963714.html