Children of the Candy Corn 深搜 广搜 很经典 对queue的认识又增加一份

Problem 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
**************************************************************************************************************************
深搜的形式很重要,广搜的先后也很重要
**************************************************************************************************************************
  1 #include<iostream>
  2 #include<string>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<cstdio>
  6 #include<queue>
  7 using namespace std;
  8 int n,w,h,k,die,de;
  9 int dx[4]={1,0,-1,0};
 10 int dy[4]={0,-1,0,1};
 11 char map[1001][1001];
 12 int step1[1001][1001];
 13 int i,j;
 14 struct  node
 15 {
 16     int x;
 17     int y;
 18 }di,st,que[10001],me;
 19 int vis[1001][1001];
 20 int dfs(bool d,int x,int y,int step)//d表示两种情况
 21   {
 22       int newx,newy,temp;
 23       if(x==di.x&&y==di.y)
 24         return step+1;
 25       if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
 26         return 0;
 27       if(d==true)
 28       {
 29           die=(die+3)%4;
 30           while(1)
 31           {
 32               int newx=x+dx[die];
 33               int newy=y+dy[die];
 34               temp=dfs(d,newx,newy,step+1);
 35               if(temp>0)
 36                 break;
 37               die=(die+1)%4;
 38           }
 39       }
 40       else
 41       {
 42           die=(die+1)%4;
 43           while(1)
 44           {
 45               int newx=x+dx[die];
 46               int newy=y+dy[die];
 47               temp=dfs(d,newx,newy,step+1);
 48               if(temp>0)
 49                 break;
 50               die=(die+3)%4;
 51           }
 52       }
 53       return temp;
 54   }
 55 int bfs()
 56 {
 57     int top,rear;
 58     que[0].x=st.x;
 59     que[0].y=st.y;
 60     top=rear=0;
 61     rear++;
 62     vis[st.x][st.y]=1;
 63     while(top<rear)
 64     {
 65         me=que[top];
 66         int xt=me.x;
 67         int yt=me.y;
 68         //cout<<"di: "<<xt<<' '<<yt<<endl;
 69         if(xt==di.x&&yt==di.y)
 70         {
 71             //cout<<"step1: "<<step1[xt][yt]<<endl;
 72             return step1[xt][yt]+1;//队列的特点,最先满足条件的为最优值
 73         }
 74 
 75         for(int it=0;it<4;it++)
 76         {
 77             int xa=xt+dx[it];
 78             int ya=yt+dy[it];
 79             if(!vis[xa][ya]&&xa>=0&&xa<h&&ya>=0&&ya<w&&map[xa][ya]!='#')//多次wa到这儿啦,注意条件
 80               {
 81                   step1[xa][ya]=step1[xt][yt]+1;
 82                   //cout<<step1[xa][ya]<<endl;
 83                   vis[xa][ya]=1;
 84                   que[rear].x=xa;
 85                   que[rear++].y=ya;
 86               }
 87         }
 88         top++;
 89     }
 90 }
 91 
 92 int main()
 93 {
 94  scanf("%d",&n);
 95  while(n--)
 96  {
 97      scanf("%d%d",&w,&h);
 98  for(i=0;i<h;i++)
 99  {
100     scanf("%s",map[i]);
101 
102     for(j=0;j<w;j++)
103     {
104 
105         if(map[i][j]=='S')
106         {
107             st.x=i;
108             st.y=j;
109         }
110         else
111         {
112             if(map[i][j]=='E')
113             {
114                 di.x=i;
115                 di.y=j;
116             }
117         }
118     }
119  }
120  //cout<<st.x<<' '<<st.y<<' '<<di.x<<' '<<di.y<<endl;
121  die=0;
122  memset(vis,0,sizeof(vis));
123  memset(step1,0,sizeof(step1));
124  int ans1=dfs(true,st.x,st.y,0);
125  int ans2=dfs(false,st.x,st.y,0);
126  int def=bfs();
127  printf("%d %d %d
",ans1,ans2,def);
128  }
129 
130  return 0;
131 }
View Code
原文地址:https://www.cnblogs.com/sdau--codeants/p/3369078.html