POJ 3083 Children of the Candy Corn (BFS+DFS)

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. 沿着左墙走,输出左转优先的步数;

2. 沿着右墙走,输出右转优先的步数;

3. 随意走,要求步数最少。


解题思路:

  当左转有限和右转优先的的时候可以采用dfs(深搜),因为深搜可以沿着一个方向一直搜下去,直到不能搜为止,求最小步数通常是采用bfs(广搜),这个题结合这两种方法就可以做出来。

最短步数挺简单就不说了,重点说一下怎么深搜。

我是定义了四个数0123分别表示北东南西,

  0(北) 
3(西) 当前位置 1(东)
  2(南) 
这个题麻烦就麻烦在每走一步就要判断你在当前位置往哪里走,比如当前是左优先,此时在当前位置,你需要先判断下一步你能不能往左走(左也是不固定的,当你向北走时,西就是左,当你向南走的时,东就是左),不能往左走,那就判断是否能往前走,不能那就判断能否往右走,全都不能,那只能往后走(没有路了)。

转化为上面的图形语言就是,你在当前位置向上走呢,先判断是否可以先往3,能走就走,不行就判断能否往0走,接着就是1,2,就这样每到一个点先判断,但是一定要有先后顺序。


这里有一点必须要注意的:

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


#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,startx,starty,endx,endy;
int dir[4][2]= {-1,0,0,1,1,0,0,-1};
int Lstep,Rstep,Qstep;
int mark[42][42],vis[42][42];
char map[42][42];
struct node
{
    int x,y,step;
};
void Init()
{
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<m; j++)
        {
            scanf("%c",&map[i][j]);
            if(map[i][j]=='.')
                mark[i][j]=1;
            if(map[i][j]=='S')
            {
                startx=i;
                starty=j;
                mark[i][j]=1;
            }
            if(map[i][j]=='E')
            {
                endx=i;
                endy=j;
                mark[i][j]=1;
                map[i][j]='.';
            }
        }
        getchar();
    }
}
void Ldfs(int x,int y,int d)
{
    Lstep++;
    if(x==endx&&y==endy) return ;
    if(d==0)
    {
        if(mark[x][y-1]) Ldfs(x,y-1,3);
        else if(mark[x-1][y]) Ldfs(x-1,y,0);
        else if(mark[x][y+1]) Ldfs(x,y+1,1);
        else if(mark[x+1][y]) Ldfs(x+1,y,2);
    }
    else if(d==1)
    {
        if(mark[x-1][y]) Ldfs(x-1,y,0);
        else if(mark[x][y+1]) Ldfs(x,y+1,1);
        else if(mark[x+1][y]) Ldfs(x+1,y,2);
        else if(mark[x][y-1]) Ldfs(x,y-1,3);
    }
    else if(d==2)
    {
        if(mark[x][y+1]) Ldfs(x,y+1,1);
        else if(mark[x+1][y]) Ldfs(x+1,y,2);
        else if(mark[x][y-1]) Ldfs(x,y-1,3);
        else if(mark[x-1][y]) Ldfs(x-1,y,0);
    }
    else if(d==3)
    {
        if(mark[x+1][y]) Ldfs(x+1,y,2);
        else if(mark[x][y-1]) Ldfs(x,y-1,3);
        else if(mark[x-1][y]) Ldfs(x-1,y,0);
        else if(mark[x][y+1]) Ldfs(x,y+1,1);
    }
}
void Rdfs(int x,int y,int d)
{
    Rstep++;
    if(x==endx&&y==endy) return ;
    if(d==0)
    {
        if(mark[x][y+1])  Rdfs(x,y+1,1);
        else if(mark[x-1][y])Rdfs(x-1,y,0);
        else if(mark[x][y-1]) Rdfs(x,y-1,3);
        else if(mark[x+1][y]) Rdfs(x+1,y,2);
    }
    else if(d==1)
    {
        if(mark[x+1][y]) Rdfs(x+1,y,2);
        else if(mark[x][y+1]) Rdfs(x,y+1,1);
        else if(mark[x-1][y]) Rdfs(x-1,y,0);
        else if(mark[x][y-1]) Rdfs(x,y-1,3);
    }
    else if(d==2)
    {
        if(mark[x][y-1]) Rdfs(x,y-1,3);
        else if(mark[x+1][y]) Rdfs(x+1,y,2);
        else if(mark[x][y+1]) Rdfs(x,y+1,1);
        else if(mark[x-1][y]) Rdfs(x-1,y,0);
    }
    else if(d==3)
    {
        if(mark[x-1][y]) Rdfs(x-1,y,0);
        else if(mark[x][y-1]) Rdfs(x,y-1,3);
        else if(mark[x+1][y]) Rdfs(x+1,y,2);
        else if(mark[x][y+1]) Rdfs(x,y+1,1);
    }
}
int bfs(int x,int y)
{
    queue<node>Q;
    node p,q;
    p.x=x;
    p.y=y;
    vis[x][y]=1;
    p.step=1;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(p.x==endx&&p.y==endy)
        {
            return p.step;
        }
        for(int i=0; i<4; i++)
        {
            q.x=p.x+dir[i][0];
            q.y=p.y+dir[i][1];
            if(q.x>=0&&q.x<n&&q.y>=0&&q.y<m&&vis[q.x][q.y]==0&&map[q.x][q.y]=='.')
            {
                q.step=p.step+1;
                Q.push(q);
                vis[q.x][q.y]=1;
            }
        }
    }
    while(!Q.empty())
        Q.pop();
}
int main()
{
    int i,j,t,d;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        memset(mark,0,sizeof(mark));
        scanf("%d%d",&m,&n);
        getchar();
        Init();
        Lstep=Rstep=Qstep=0;
        if(startx==0) d=2;
        else if(startx==n-1) d=0;
        else if(starty==0) d=1;
        else if(starty==m-1) d=3;
        Ldfs(startx, starty,d);
        Rdfs(startx, starty,d);
        Qstep=bfs(startx,starty);
        printf("%d %d %d\n",Lstep,Rstep,Qstep);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值