POJ 3083 Children of the Candy Corn

                                                                            Children of the Candy Corn

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4063   Accepted: 1850

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

Source

South Central USA 2006

 

  1 #include <cstdlib>
  2 #include <iostream>
  3 
  4 using namespace std;
  5 
  6 char map[41][41];
  7 
  8 
  9 int movex[4]={1,0,-1,0};//图的方向的标记右(0)下(1)左(2)上(3);
 10 int movey[4]={0,1,0,-1};
 11 int mount=0;
 12 int w,h;
 13 int dfsleft(int sx,int sy,int num,int tag)//tag为此时人的正面所对应的图的标记
 14 {int i;
 15 if(map[sy][sx]=='E')
 16 {mount=num;return 1;}
 17 int x,y;
 18 
 19 for(i=(tag+3)%4;i<4;i=((++i)+4)%4)//左边优先,从左边开始顺时针
 20 {x=sx+movex[i];y=sy+movey[i];
 21   if(x<1||x>w)
 22   continue;
 23   if(y<1||y>h)
 24   continue;
 25   
 26   
 27   
 28   if(map[y][x]=='#')
 29   continue;
 30   
 31   if(dfsleft(x,y,num+1,i))
 32   return 1;
 33   
 34  
 35 }
 36 return 0;
 37 }
 38 
 39 int dfsright(int sx,int sy,int num,int tag)
 40 {
 41 int i;
 42 if(map[sy][sx]=='E')
 43 {mount=num;return 1;}
 44 int x,y;
 45 
 46 for(i=(tag+5)%4;i<4;i=((--i)+4)%4)//右边优先,从右边开始逆时针;
 47 {x=sx+movex[i];y=sy+movey[i];
 48   if(x<1||x>w)
 49   continue;
 50   if(y<1||y>h)
 51   continue;
 52   
 53   
 54   if(map[y][x]=='#')
 55   continue;
 56   
 57   if(dfsright(x,y,num+1,i))
 58   return 1;
 59   
 60 }
 61 return 0;
 62 }
 63 struct queue{
 64        int x;
 65        int y;
 66        int num;
 67        };
 68 int front,rear;
 69 
 70 int bfs(int sx,int sy)
 71 {
 72     
 73     queue q[10000];
 74     front=rear=0;
 75     queue node;
 76     
 77     if(map[sy][sx]=='E')
 78     return 1;
 79     
 80     q[rear].x=sx;
 81     q[rear].y=sy;
 82     q[rear].num=1;
 83     rear++;
 84     int i;
 85     int x,y;
 86     while(front<rear)
 87     {node.x=q[front].x;
 88      node.y=q[front].y;
 89      node.num=q[front].num;
 90      front++;
 91                      for(i=0;i<4;i++)
 92                      {x=node.x+movex[i];
 93                       y=node.y+movey[i];
 94                       if(x<1||x>w)
 95                       continue;
 96                       if(y<1||y>h)
 97                       continue;
 98                       if(map[y][x]=='#')
 99                       continue;
100                       if(map[y][x]=='E')
101                       return node.num+1;
102                       map[y][x]='#';
103                       q[rear].x=x;
104                       q[rear].y=y;
105                       q[rear].num=node.num+1;
106                       rear++;
107                      }
108      }
109 }
110 
111 
112 
113 int main(int argc, char *argv[])
114 {int n;
115 
116 cin>>n;
117 int sx,sy;
118 int tag;
119 int i,j;
120 while(n--)
121 {cin>>w>>h;
122           for(i=1;i<=h;i++)
123           for(j=1;j<=w;j++)
124           {cin>>map[i][j];
125           if(map[i][j]=='S')
126           sx=j,sy=i;
127           }
128           int x,y;
129           for(i=0;i<4;i++)
130           {x=sx+movex[i];y=sy+movey[i];
131            if(x<1||x>w)
132            continue;
133            if(y<1||y>h)
134            continue;
135            
136           if(map[y][x]=='.')
137           break;
138           
139           }
140           tag=i;
141           
142           dfsleft(sx,sy,1,tag);
143           cout<<mount<<' ';
144           dfsright(sx,sy,1,tag);
145           cout<<mount<<' ';
146           cout<<bfs(sx,sy)<<endl;
147           
148           
149           
150  }
151     system("PAUSE");
152     return EXIT_SUCCESS;
153 }
posted @ 2012-05-31 10:00  cseriscser  阅读(277)  评论(0编辑  收藏  举报