Children of the Candy Corn(图的遍历bfs最小步数)

来源:互联网 发布:linux怎样启动tomcat 编辑:程序博客网 时间:2024/05/18 01:28
Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13121 Accepted: 5673

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

Source


题目大意:

给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走

先输出左转优先时,从SE的步数

再输出右转优先时,从SE的步数

最后输出SE的最短步数

 

W为宽,列数

H为高,行数

 

解题思路:

 

1、 左转、右转优先搜索时必须标记当前位置时的方向,我定义的方向是

    


 

最初的方向由起点S确定,而下一步的方向则由前一步的走向决定

 

例如左边优先搜索:

当前位置的方向指向 1(向左),(这同时说明前一步是在第“3”的位置走过来的)

那么走下一步时,就要根据2103的顺序,先逐格确定当前位置周边的四格是否可行

若第一次确认2可行,就走到2,在位置2时的方向为2(向下)

2不可行,则再确定1,若1可行,就走到1,在位置1时的方向为1(向左)

  1也不可行,则再确定0,若0可行,就走到0,在位置0时的方向为0(向上)

  0也不可行,说明进入了迷宫的死胡同,要从原路返回,走回3

 

右边优先搜索也同

左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格

寻找最短路只能用BFS

#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;struct node{   int step;   int h,z;}s,e,v,t;int mp[45][45],vis[45][45];int ls,rs;void LF(int i,int j,int d){   ls++;    if(i==e.h&&j==e.z)        return ;    switch(d)    {   case 0:          if(mp[i][j-1])//查找按一定顺序来的,先左,左不行在前,前不行再右,最后回去            LF(i,j-1,1);          else if(mp[i-1][j])            LF(i-1,j,0);          else if(mp[i][j+1])            LF(i,j+1,3);          else if(mp[i+1][j])            LF(i+1,j,2);          break;   case 1:          if(mp[i+1][j])            LF(i+1,j,2);          else if(mp[i][j-1])            LF(i,j-1,1);          else if(mp[i-1][j])            LF(i-1,j,0);          else if(mp[i][j+1])            LF(i,j+1,3);          break;   case 2:          if(mp[i][j+1])            LF(i,j+1,3);          else if(mp[i+1][j])            LF(i+1,j,2);          else if(mp[i][j-1])            LF(i,j-1,1);          else if(mp[i-1][j])            LF(i-1,j,0);          break;   case 3:           if(mp[i-1][j])            LF(i-1,j,0);           else if(mp[i][j+1])            LF(i,j+1,3);           else if(mp[i+1][j])            LF(i+1,j,2);           else if(mp[i][j-1])            LF(i,j-1,1);           break;    }    return ;}void RF(int i,int j,int d){   rs++;    if(i==e.h&&j==e.z)        return ;    switch(d)    {   case 0:          if(mp[i][j+1])            RF(i,j+1,3);          else if(mp[i-1][j])            RF(i-1,j,0);          else if(mp[i][j-1])            RF(i,j-1,1);          else if(mp[i+1][j])            RF(i+1,j,2);          break;   case 1:          if(mp[i-1][j])            RF(i-1,j,0);          else if(mp[i][j-1])            RF(i,j-1,1);          else if(mp[i+1][j])            RF(i+1,j,2);          else if(mp[i][j+1])            RF(i,j+1,3);          break;   case 2:          if(mp[i][j-1])            RF(i,j-1,1);          else if(mp[i+1][j])            RF(i+1,j,2);          else if(mp[i][j+1])            RF(i,j+1,3);          else if(mp[i-1][j])            RF(i-1,j,0);          break;   case 3:           if(mp[i+1][j])            RF(i+1,j,2);           else if(mp[i][j+1])            RF(i,j+1,3);           else if(mp[i-1][j])            RF(i-1,j,0);           else if(mp[i][j-1])            RF(i,j-1,1);           break;    }}void bfs(){  queue<struct node>q;  v.step=1;  v.h=s.h;  v.z=s.z;  q.push(v);  while(!q.empty())  {      t=q.front();      q.pop();      int i=t.h,j=t.z;      if(t.h==e.h&&t.z==e.z)      {         cout<<t.step<<endl;         return;      }      if(mp[i-1][j]&&!vis[i-1][j])      {         vis[i-1][j]=1;         v.h=i-1;         v.z=j;         v.step=t.step+1;         q.push(v);      }      if(mp[i+1][j]&&!vis[i+1][j])      {         vis[i+1][j]=1;         v.h=i+1;         v.z=j;         v.step=t.step+1;         q.push(v);      }      if(mp[i][j+1]&&!vis[i][j+1])      {         vis[i][j+1]=1;         v.h=i;         v.z=j+1;         v.step=t.step+1;         q.push(v);      }      if(mp[i][j-1]&&!vis[i][j-1])      {         vis[i][j-1]=1;         v.h=i;         v.z=j-1;         v.step=t.step+1;         q.push(v);      }  }}int main(){    int i,j,dec;    int he,w,T;    cin>>T;    while(T--)    {  ls=1;       rs=1;       memset(mp,0,sizeof(mp));       cin>>w>>he;       for(i=1;i<=he;i++)       {           for(j=1;j<=w;j++)           {   char c;               cin>>c;               if(c=='.')                mp[i][j]=1;               if(c=='S')               {                  mp[i][j]=1;                  s.h=i;                  s.z=j;                  if(s.h==he)//确定第一步走出来的方向                   dec=0;                  else if(s.z==w)                   dec=1;                  else if(s.h==1)                    dec=2;                  else if(s.z==1)                    dec=3;               }               if(c=='E')               {                  mp[i][j]=1;                  e.h=i;                  e.z=j;               }           }       }       switch(dec)       {          case 0:LF(s.h-1,s.z,0);break;//方向不同一开始的搜索位置不同          case 1:LF(s.h,s.z-1,1);break;          case 2:LF(s.h+1,s.z,2);break;          case 3:LF(s.h,s.z+1,3);break;       }       cout<<ls<<" ";       switch(dec)       {          case 0:RF(s.h-1,s.z,0);break;          case 1:RF(s.h,s.z-1,1);break;          case 2:RF(s.h+1,s.z,2);break;          case 3:RF(s.h,s.z+1,3);break;       }       cout<<rs<<" ";       memset(vis,0,sizeof(vis));       bfs();    }    return 0;}



0 0
原创粉丝点击