poj-3083 Children of the Candy Corn-DFS+BFS

来源:互联网 发布:js 特殊字符校验 编辑:程序博客网 时间:2024/06/11 05:22

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和直接找到E,输出沿左走沿右走和直接以最短路径到达E所需的步数。该题需要用DFS和BFS相结合,BFS可以方便快速的直接找到E,DFS可以遍历所有沿墙走的情况并最后到达E。

    注意:DFS时需要控制走的方向,以向左走为例,每个点处优先查看这个点所朝方向的左边(例如点朝向上,那么就先看左边的点。点朝向右,就先看上边的点。因为沿左边走向左优先)然后沿顺时针查看其他方向。

    沿右走本人是以E为起点沿左走找S,这样可以避免写两个DFS。

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#define INF 0x3f3f3f3fusing namespace std;char map[255][255];int d[][2]={{-1,0},{0,1},{1,0},{0,-1}};int ans,ansl,ansr,anss;int xs,ys,xe,ye,w,h;int flag;int way;struct node{    int x,y,step;}next,now,que[10000];int vis[255][255];void DFS(int x,int y,int step){    int fx,fy,i;    for (i=1;i<=4;i++)    {        if(flag) return;        if(i==1) way+=3;        else way++;        if(way>4) way %= 4;         fx = x+d[way-1][0];         fy = y+d[way-1][1];         if(fx<0||fy<0||fx>=h||fy>=w) continue;        if(fx==xe&&fy==ye)            {ans=step+1;flag=1;return;}        if(map[fx][fy]=='.')            DFS(fx,fy,step+1);    }}void BFS(int x,int y,int step){    memset(vis,0,sizeof(vis));    int s=0,e=1,i;    vis[x][y]=1;    que[0].x=x;    que[0].y=y;    que[0].step=step;    while(s<e)    {        now=que[s++];        for (i=1;i<=4;i++)        {            if(i==1) {next.x=now.x+1;next.y=now.y;}            if(i==2) {next.x=now.x-1;next.y=now.y;}            if(i==3) {next.x=now.x;next.y=now.y+1;}            if(i==4) {next.x=now.x;next.y=now.y-1;}            next.step=now.step+1;            if(map[next.x][next.y]=='#'||next.x<0||next.y<0||next.x>=h||next.y>=w) continue;            if(next.x==xe&&next.y==ye)            {                anss=next.step;                flag=1;break;            }            if(!vis[next.x][next.y])            {                vis[next.x][next.y]=1;                que[e++]=next;            }        }        if(flag) break;    }}int main(){    int t,i,j;    int ch;    scanf("%d",&t);    while(t--)    {        memset(map,0,sizeof(map));        scanf("%d%d",&w,&h);        for (i=0;i<h;i++)        {            scanf("%s",map[i]);        }        for (i=0;i<h;i++)        {            for (j=0;j<w;j++)            {                if(map[i][j]=='S')                {                    xs=i;                    ys=j;                }                else if(map[i][j]=='E')                {                    xe=i;                    ye=j;                }            }        }                flag=0;        BFS(xs,ys,1);        way=1;        flag=0;        DFS(xs,ys,1);        ansl=ans;        way=1;        flag=0;        ch=xs,xs=xe,xe=ch;                                 //交换S和E的坐标;        ch=ys,ys=ye,ye=ch;        DFS(xs,ys,1);        ansr=ans;        printf("%d %d %d\n",ansl,ansr,anss);    }    return 0;}



0 0
原创粉丝点击