POJ-3083 Children of the Candy Corn (BFS+DFS)

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


题目大意:从起点到终点,求三条路的长度。第一条是以总以左边优先,并且四个方位按顺时针遍历,直到终点的长度;第二条是总以右边优先,并且四个方位按逆时针遍历,直到终点的长度;第三条是最短路。
题目分析:显然,前两条路是DFS出来的,第三条是BFS出来的。

代码如下:
 1 # include<iostream>
 2 # include<cstdio>
 3 # include<queue>
 4 # include<cstring>
 5 # include<algorithm>
 6 using namespace std;
 7 struct node
 8 {
 9     int x,y,t;
10     node(int _x,int _y,int _t):x(_x),y(_y),t(_t){}
11 };
12 char p[50][50];
13 bool flag;
14 int w,h,ans[2],mark[50][50];
15 int d[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
16 bool ok(int x,int y)
17 {
18     if(x>=0&&x<h&&y>=0&&y<w)
19         return true;
20     return false;
21 }
22 void dfs(int x,int y,int pos,int t,int step)
23 {
24     if(flag)
25         return ;
26     if(p[x][y]=='E'){
27         flag=true;
28         ans[(t==1)]=step;
29         return ;
30     }
31     int nx,ny,np;
32     for(int i=1,pp=(pos+t+4)%4;i<=4;++i,pp=(pp-t+4)%4){
33         nx=x+d[pp][0],ny=y+d[pp][1];
34         if(ok(nx,ny)&&p[nx][ny]!='#'){
35             dfs(nx,ny,pp,t,step+1);
36             if(flag)
37                 return ;
38         }
39     }
40 }
41 void bfs(int sx,int sy)
42 {
43     queue<node>q;
44     memset(mark,0,sizeof(mark));
45     mark[sx][sy]=1;
46     q.push(node(sx,sy,1));
47     while(!q.empty())
48     {
49         node u=q.front();
50         q.pop();
51         if(p[u.x][u.y]=='E'){
52             printf("%d\n",u.t);
53             return ;
54         }
55         for(int i=0;i<4;++i){
56             int nx=u.x+d[i][0],ny=u.y+d[i][1];
57             if(ok(nx,ny)&&!mark[nx][ny]&&p[nx][ny]!='#'){
58                 mark[nx][ny]=1;
59                 q.push(node(nx,ny,u.t+1));
60             }
61         }
62     }
63 }
64 int main()
65 {
66     int T,sx,sy,pos;
67     scanf("%d",&T);
68     while(T--)
69     {
70         scanf("%d%d",&w,&h);
71         for(int i=0;i<h;++i){
72             scanf("%s",p[i]);
73             for(int j=0;j<w;++j)
74                 if(p[i][j]=='S')
75                     sx=i,sy=j;
76         }
77         if(sx==0)
78             pos=2;
79         if(sx==h-1)
80             pos=0;
81         if(sy==0)
82             pos=1;
83         if(sy==w-1)
84             pos=3;
85         ans[0]=ans[2]=0;
86         flag=false;
87         dfs(sx,sy,pos,-1,1);
88         flag=false;
89         dfs(sx,sy,pos,1,1);
90         printf("%d %d ",ans[0],ans[1]);
91         bfs(sx,sy);
92     }
93     return 0;
94 }
View Code

做后感:这道题既恶心又水!!!

posted @ 2015-08-16 15:23  20143605  阅读(177)  评论(0编辑  收藏  举报