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.
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.
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
题意:S为起点,E为终点,#为障碍,求从起点到终点的左优先策略的步数,右优先策略的步数,和最小的步数
代码:
#include<iostream>
#include<cstdio>
#include<queue>
char map[105][105];
int vis[105][105];
int sx,sy,ex,ey; //标记start end
int a,b;
int fx; //判断下一步方向
int left1[4][2]={0,-1,-1,0,0,1,1,0};
int right1[4][2]={0,1,-1,0,0,-1,1,0}; //不能直接用left ,right作数组名
struct node{
int xx,yy,num;
};
using namespace std;
int dfs(int x,int y,int s,int d) //s为步数 d判断是左优先还是右优先
{
if(x==ex&&y==ey)
{
return s+1; //要算上起点
}
if(x<1||x>a||y<1||y>b||map[x][y]=='#')
return 0;
int t=0;
fx=(fx+3)%4; //将方向定为当前方向的左/右
while(1)
{
if(d==1)
{
t=dfs(x+left1[fx][0],y+left1[fx][1],s+1,d);
}
else
{
t=dfs(x+right1[fx][0],y+right1[fx][1],s+1,d);
}
if(t>0)
break;
fx=(fx+1)%4; //按左/右优先依次搜索
}
return t;
}
int bfs() //广搜求最小距离
{
node now,next;
queue<node>q;
now.xx=sx;now.yy=sy; now.num=0;
vis[sx][sy]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.xx==ex&&now.yy==ey)
{
return now.num+1; //要算上起点
}
int tx,ty;
for(int i=0;i<4;i++)
{
tx=now.xx+left1[i][0];
ty=now.yy+left1[i][1];
if(tx<1||tx>a||ty<1||ty>b||vis[tx][ty])
continue;
else
vis[tx][ty]=1;
next.num=now.num+1;
next.xx=tx;
next.yy=ty;
q.push(next);
}
}
}
int main()
{
int m,i,j;
cin>>m;
while(m--)
{
fx=0;
cin>>b>>a;
memset(vis,0,sizeof(vis));
for(i=1;i<=a;i++)
for(j=1;j<=b;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{sx=i;sy=j;}
if(map[i][j]=='E')
{ ex=i;ey=j;}
if(map[i][j]=='#')
vis[i][j]=1;
}
printf("%d ",dfs(sx,sy,0,1));
fx=0; //左优先搜索完 要初始化方向
printf("%d ",dfs(sx,sy,0,2));
printf("%d\n",bfs());
}
return 0;
}
代码很长 但除了标识符不太明确外 还是比较简洁的= =