您的位置:首页 > 其它

POJ_3083——贴左右墙DFS,最短路径BFS

2013-07-13 12:15 519 查看
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


#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

struct node
{
int x, y;
int step;
}start,end;

int T, w, h, L_step, R_step, step;

int dir[4][2]={-1,0,0,-1,1,0,0,1};

char map[50][50], *p;

//左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格
bool DFS_Left(int x,int y,int d)
{
int _d, _dx, _dy;
if(map[x][y]=='E')
{
return true;
}

L_step++;

//根据上一次走的方向,用公式推出下一次要走的方向

//往左走
_d = (d+1)%4;
_dx = x + dir[_d][0];
_dy = y + dir[_d][1];
if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w)
{
if(DFS_Left(_dx,_dy,_d))
return true;
}

//往原始方向走
_d = d;
_dx = x + dir[_d][0];
_dy = y + dir[_d][1];
if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w)
{
if(DFS_Left(_dx,_dy,_d))
return true;
}

//往右边走
_d = (d+3)%4;
_dx = x + dir[_d][0];
_dy = y + dir[_d][1];
if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w)
{
if(DFS_Left(_dx,_dy,_d))
return true;
}

//往回走
_d = (d+2)%4;
_dx = x + dir[_d][0];
_dy = y + dir[_d][1];
if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w)
{
if(DFS_Left(_dx,_dy,_d))
return true;
}

L_step--;
return false;
}

bool DFS_Right(int x,int y,int d)
{
int _d, _dx, _dy;
if(map[x][y]=='E')
{
return true;
}

R_step++;

//根据上一次走的方向,用公式推出下一次要走的方向

//往右走
_d = (d+3)%4;
_dx = x + dir[_d][0];
_dy = y + dir[_d][1];
if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w)
{
if(DFS_Right(_dx,_dy,_d))
return true;
}

//往原始方向走
_d = d;
_dx = x + dir[_d][0];
_dy = y + dir[_d][1];
if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w)
{
if(DFS_Right(_dx,_dy,_d))
return true;
}

//往左边走
_d = (d+1)%4;
_dx = x + dir[_d][0];
_dy = y + dir[_d][1];
if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w)
{
if(DFS_Right(_dx,_dy,_d))
return true;
}

//往回走
_d = (d+2)%4;
_dx = x + dir[_d][0];
_dy = y + dir[_d][1];
if(map[_dx][_dy]!='#' && _dx>=0 && _dx<h && _dy>=0 && _dy<w)
{
if(DFS_Right(_dx,_dy,_d))
return true;
}

R_step--;
return false;
}

//寻找最短路只能用BFS
void BFS()
{
node temp,next;
queue<node>p;
p.push(start);
while(!p.empty())
{
temp=p.front();
p.pop();

if(temp.x==end.x && temp.y==end.y)
{
step = temp.step;
break;
}

next.step = temp.step + 1;
for(int i=0;i<4;i++)
{
next.x = temp.x + dir[i][0];
next.y = temp.y + dir[i][1];
if(map[next.x][next.y]!='#' && next.x>=0 && next.x<h && next.y>=0 && next.y<w)
{
//BFS中在原始地图中记录走过的路径,会把结束标记给覆盖掉,所以要预先存储
//终点坐标,或者另开一个数组记录路径
map[next.x][next.y]='#';
p.push(next);
}
}
}
return;
}

int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&w,&h);
for(int i=0;i<h;i++)
{
scanf("%s",map[i]);

//获取起点坐标
p=strchr(map[i],'S');
if(p!=NULL)
{
start.x=i;
start.y=p-map[i];
}

p=strchr(map[i],'E');

if(p!=NULL)
{
end.x=i;
end.y=p-map[i];
}
}

L_step=R_step=1;    start.step=1;

DFS_Left(start.x,start.y,0);
DFS_Right(start.x,start.y,0);
BFS();
printf("%d %d %d\n",L_step,R_step,step);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: