首页 > 代码库 > poj 3083 Children of the Candy Corn

poj 3083 Children of the Candy Corn

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9528 Accepted: 4126

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

28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########

Sample Output

37 5 517 17 9
题解:题目大意,对于迷宫来说,如果我们的方向一直向左走或者向右走,我们一定能够找到出口,本题的意思就是说,给你一个迷宫,起点是 ‘S’ ,终点是 ‘E’,
接下来一行每行输入三个数,第一个数表示方向一直向左走,走出迷宫所需要的步数,第二个表示,方向一字向右走,走出迷宫所需要的步数,第三个就是最小的步数;
对于前两个来说,我们可以用两个 DFS 来求出,走出迷宫所需要的步数,第三种情况就需要用到,BFS 了,直接求出来最小的步数即可;
以下图解:

初始化一开始的朝向都是1,当向左转时,她的第一个左边是4,第二个是1,第三个是2、、、、顺时针的
当向右时是逆时针的、、、、
以下是AC代码:
  1 #include<algorithm>  2 #include<iostream>  3 #include<cstdio>  4 #include<cstring>  5 #include<queue>  6 using namespace std;  7 const int N = 50;  8 char Map[N][N];  9 int vis[N][N]; 10 int m,n,sx,sy,ex,ey,flag; 11 int ans1,ans2; 12 int dir[4][2] = {-1,0,1,0,0,1,0,-1}; 13 struct T 14 { 15     int x,y,step; 16 }now,eed; 17 bool judge(int xx,int yy) 18 { 19     if(xx>=1 && xx<=m && yy>=1 && yy<=n && vis[xx][yy]==0 && Map[xx][yy]!=#) 20         return true; 21               return false; 22 } 23 //face表示的是当前的朝向,step表示的是当前的步数 24 void dfs1(int x,int y,int step ,int face)//向左走,进行的是顺序的,递归 25 { 26     if(flag == 1) 27       return ; 28       if(x==ex && y==ey) 29       { 30           flag=1; 31           ans1 = step; 32           return ; 33       } 34       //根据上一个的朝向判断下一个应该向那转弯 35       if(face == 1) 36       { 37           if(judge(x+1,y)) dfs1(x+1,y,step+1,4); 38           if(judge(x,y-1)) dfs1(x,y-1,step+1,1); 39           if(judge(x-1,y)) dfs1(x-1,y,step+1,2); 40           if(judge(x,y+1)) dfs1(x,y+1,step+1,3); 41       } 42       else if(face == 2) 43       { 44           if(judge(x,y-1)) dfs1(x,y-1,step+1,1); 45           if(judge(x-1,y)) dfs1(x-1,y,step+1,2); 46           if(judge(x,y+1)) dfs1(x,y+1,step+1,3); 47           if(judge(x+1,y)) dfs1(x+1,y,step+1,4); 48       } 49       else if(face == 3) 50       { 51           if(judge(x-1,y)) dfs1(x-1,y,step+1,2); 52           if(judge(x,y+1)) dfs1(x,y+1,step+1,3); 53           if(judge(x+1,y)) dfs1(x+1,y,step+1,4); 54           if(judge(x,y-1)) dfs1(x,y-1,step+1,1); 55       } 56       else 57       { 58           if(judge(x,y+1)) dfs1(x,y+1,step+1,3); 59           if(judge(x+1,y)) dfs1(x+1,y,step+1,4); 60           if(judge(x,y-1)) dfs1(x,y-1,step+1,1); 61           if(judge(x-1,y)) dfs1(x-1,y,step+1,2); 62       } 63 } 64 void dfs2(int x,int y,int step,int face)//向右走,进行的是逆序的,递归 65 { 66      if(flag == 1) 67       return ; 68       if(x==ex && y==ey) 69       { 70           flag=1; 71           ans2 = step; 72           return ; 73       } 74       if(face == 1) 75       { 76           if(judge(x-1,y)) dfs2(x-1,y,step+1,2); 77           if(judge(x,y-1)) dfs2(x,y-1,step+1,1); 78           if(judge(x+1,y)) dfs2(x+1,y,step+1,4); 79           if(judge(x,y+1)) dfs2(x,y+1,step+1,3); 80       } 81      else  if(face == 2) 82       { 83           if(judge(x,y+1)) dfs2(x,y+1,step+1,3); 84           if(judge(x-1,y)) dfs2(x-1,y,step+1,2); 85           if(judge(x,y-1)) dfs2(x,y-1,step+1,1); 86           if(judge(x+1,y)) dfs2(x+1,y,step+1,4); 87       } 88       else if(face == 3) 89       { 90           if(judge(x+1,y)) dfs2(x+1,y,step+1,4); 91           if(judge(x,y+1)) dfs2(x,y+1,step+1,3); 92           if(judge(x-1,y)) dfs2(x-1,y,step+1,2); 93           if(judge(x,y-1)) dfs2(x,y-1,step+1,1); 94       } 95       else 96       { 97           if(judge(x,y-1)) dfs2(x,y-1,step+1,1); 98           if(judge(x+1,y)) dfs2(x+1,y,step+1,4); 99           if(judge(x,y+1)) dfs2(x,y+1,step+1,3);100           if(judge(x-1,y)) dfs2(x-1,y,step+1,2);101       }102 }103 int bfs()104 {105     queue< T > ma;106     while(!ma.empty()) ma.pop();107     now.x=sx,now.y=sy,now.step=1;108     ma.push(now);109     vis[sx][sy]=1;110     while(!ma.empty())111     {112         now = ma.front();113         ma.pop();114         if(now.x == ex && now.y == ey)115              return now.step;116              for(int i=0; i<4; i++)117                 {118                     eed.x=now.x+dir[i][0];119                     eed.y=now.y+dir[i][1];120                     if(judge(eed.x,eed.y))121                     {122                         eed.step=now.step+1; vis[eed.x][eed.y]=1;123                         ma.push(eed);124                     }125                 }126     }127     return 0;128 }129 int main()130 {131     int T;132     cin>>T;133     while(T--)134     {135         memset(vis,0,sizeof(vis));136         cin>>n>>m;137         for(int i=1; i<=m; i++)138             for(int j=1; j<=n; j++)139             {140                scanf(" %c",&Map[i][j]);141                 if(Map[i][j] == S)142                 {143                     sx=i; sy=j;144                 }145                 if(Map[i][j] == E)146                 {147                     ex=i; ey=j;148                 }149             }150         flag=0;ans1=0;151         dfs1(sx,sy,1,1);//一直向左走152         flag=0;ans2=0;153         dfs2(sx,sy,1,1);//一直向右走154         int ans3=bfs();155         printf("%d %d %d\n",ans1,ans2,ans3);156     }157 return 0;158 }