POJ 3083图搜 bfs+dfs

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11692 Accepted: 5020

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
作为一个广搜痴迷者,这次尝试了一下深搜,默默的感受到了深搜的魅力,深搜真的是比广搜好建立思路啊。

题目大意:

从S走到E,第一个输出:左转优先到E的步数。第二个输出:右转优先到E的步数。第三个最短到E的步数。起点算一步;

这里提示英语不好的小伙伴们:这个题目保证从起点走到下一步的方向是唯一的,而且S和E都在边缘(i==0||i==n-1||j==0||j==m-1)

这里我们很容易想到广搜解第三个输出,这里不啰嗦直接上代码:

int bfs(int x,int y)
{
    memset(output,0,sizeof(output));
    memset(vis,0,sizeof(vis));
    vis[x][y]=1;
    queue<zuobiao >s;
    now.x=x;
    now.y=y;
    output[x][y]=1;
    s.push(now);
    while(!s.empty())
    {
        now=s.front();
        if(a[now.x][now.y]=='E')
        {
            return output[now.x][now.y];
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&a[nex.x][nex.y]!='#')
            {
                s.push(nex);
                output[nex.x][nex.y]=output[now.x][now.y]+1;
                vis[nex.x][nex.y]=1;
            }
        }
    }
    return -1;
}
这里着重详解一下转向优先dfs的部分,这里我采用深搜的思维,比较好理解,也比较容易建立:

首先我们要有一个方向标记,标记上一步是向哪个方向走的,然后根据上一步的走向,进行走向优先的操作,这里我是这样定义的方向:

 0 
1now3
 2 
这里的now表示当前格子,0 1 2 3分别表示为向北走,向西走,向东走,向南走。这里我为什么不是说向前向左向右向后呢,因为我们来的方向不同,所对应的正面也是不同的。所以我们这里只能说是东南西北。

如果来向是0,那么表明上一步是向北走的。(这里我们拿左向优先来举例说明)这个时候我们想优先左转,对应的就是1,然后如果左边走不了,那么我们就向前走,如果前边也不能走,那就走左边,再不行就走后边,所以优先的方式是:1 0 3 2.

相应的,如果来向是1 那么我们的优先方向是2 1 0 3,如果来向是2,那么我们的优先方向是3 2 1 0.。

这里,我们左转向的优先是:左,前,右,后。右转向的优先是:右,前,左,后。

这里思维很好建立,之后就是代码的实现了:

void dfs(int i,int j,int d)//左优先
{
    step++;
    if(i==ex&&j==ey)return ;
    if(d==0)//如果来向是0 优先方式是 1 0 3 2
    {
        if(mark[i][j-1])dfs(i,j-1,1);
        else if(mark[i-1][j])dfs(i-1,j,0);
        else if(mark[i][j+1])dfs(i,j+1,3);
        else dfs(i+1,j,2);
    }
    if(d==1)
    {
        if(mark[i+1][j])dfs(i+1,j,2);
        else if(mark[i][j-1])dfs(i,j-1,1);
        else if(mark[i-1][j])dfs(i-1,j,0);
        else dfs(i,j+1,3);
    }
    if(d==2)
    {
        if(mark[i][j+1])dfs(i,j+1,3);
        else if(mark[i+1][j])dfs(i+1,j,2);
        else if(mark[i][j-1])dfs(i,j-1,1);
        else dfs(i-1,j,0);
    }
    if(d==3)
    {
        if(mark[i-1][j])dfs(i-1,j,0);
        else if(mark[i][j+1])dfs(i,j+1,3);
        else if(mark[i+1][j])dfs(i+1,j,2);
        else dfs(i,j-1,1);
    }
}
void dfs2(int i,int j,int d)//右优先。
{
    step2++;
    if(i==ex&&j==ey)return ;
    if(d==0)
    {
        if(mark[i][j+1])dfs2(i,j+1,3);
        else if(mark[i-1][j])dfs2(i-1,j,0);
        else if(mark[i][j-1])dfs2(i,j-1,1);
        else dfs2(i+1,j,2);
    }
    if(d==1)
    {
        if(mark[i-1][j])dfs2(i-1,j,0);
        else if(mark[i][j-1])dfs2(i,j-1,1);
        else if(mark[i+1][j])dfs2(i+1,j,2);
        else dfs2(i,j+1,3);
    }
    if(d==2)
    {
        if(mark[i][j-1])dfs2(i,j-1,1);
        else if(mark[i+1][j])dfs2(i+1,j,2);
        else if(mark[i][j+1])dfs2(i,j+1,3);
        else dfs2(i-1,j,0);
    }
    if(d==3)
    {
        if(mark[i+1][j])dfs2(i+1,j,2);
        else if(mark[i][j+1])dfs2(i,j+1,3);
        else if(mark[i-1][j])dfs2(i-1,j,0);
        else dfs2(i,j-1,1);
    }
}

然后是完整的代码:

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int x,y,ex,ey;
struct zuobiao
{
    int x,y;
}now,nex;
int m,n;
int step,step2;
char a[50][50];
int mark[50][50];
int output[50][50];
int vis[50][50];
int fx[4]={0,0,-1,1};
int fy[4]={1,-1,0,0};
int bfs(int x,int y)
{
    memset(output,0,sizeof(output));
    memset(vis,0,sizeof(vis));
    vis[x][y]=1;
    queue<zuobiao >s;
    now.x=x;
    now.y=y;
    output[x][y]=1;
    s.push(now);
    while(!s.empty())
    {
        now=s.front();
        if(a[now.x][now.y]=='E')
        {
            return output[now.x][now.y];
        }
        s.pop();
        for(int i=0;i<4;i++)
        {
            nex.x=now.x+fx[i];
            nex.y=now.y+fy[i];
            if(nex.x>=0&&nex.x<n&&nex.y>=0&&nex.y<m&&vis[nex.x][nex.y]==0&&a[nex.x][nex.y]!='#')
            {
                s.push(nex);
                output[nex.x][nex.y]=output[now.x][now.y]+1;
                vis[nex.x][nex.y]=1;
            }
        }
    }
    return -1;
}
void dfs(int i,int j,int d)
{
    step++;
    if(i==ex&&j==ey)return ;
    if(d==0)
    {
        if(mark[i][j-1])dfs(i,j-1,1);
        else if(mark[i-1][j])dfs(i-1,j,0);
        else if(mark[i][j+1])dfs(i,j+1,3);
        else dfs(i+1,j,2);
    }
    if(d==1)
    {
        if(mark[i+1][j])dfs(i+1,j,2);
        else if(mark[i][j-1])dfs(i,j-1,1);
        else if(mark[i-1][j])dfs(i-1,j,0);
        else dfs(i,j+1,3);
    }
    if(d==2)
    {
        if(mark[i][j+1])dfs(i,j+1,3);
        else if(mark[i+1][j])dfs(i+1,j,2);
        else if(mark[i][j-1])dfs(i,j-1,1);
        else dfs(i-1,j,0);
    }
    if(d==3)
    {
        if(mark[i-1][j])dfs(i-1,j,0);
        else if(mark[i][j+1])dfs(i,j+1,3);
        else if(mark[i+1][j])dfs(i+1,j,2);
        else dfs(i,j-1,1);
    }
}
void dfs2(int i,int j,int d)
{
    step2++;
    if(i==ex&&j==ey)return ;
    if(d==0)
    {
        if(mark[i][j+1])dfs2(i,j+1,3);
        else if(mark[i-1][j])dfs2(i-1,j,0);
        else if(mark[i][j-1])dfs2(i,j-1,1);
        else dfs2(i+1,j,2);
    }
    if(d==1)
    {
        if(mark[i-1][j])dfs2(i-1,j,0);
        else if(mark[i][j-1])dfs2(i,j-1,1);
        else if(mark[i+1][j])dfs2(i+1,j,2);
        else dfs2(i,j+1,3);
    }
    if(d==2)
    {
        if(mark[i][j-1])dfs2(i,j-1,1);
        else if(mark[i+1][j])dfs2(i+1,j,2);
        else if(mark[i][j+1])dfs2(i,j+1,3);
        else dfs2(i-1,j,0);
    }
    if(d==3)
    {
        if(mark[i+1][j])dfs2(i+1,j,2);
        else if(mark[i][j+1])dfs2(i,j+1,3);
        else if(mark[i-1][j])dfs2(i-1,j,0);
        else dfs2(i,j-1,1);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(mark,0,sizeof(mark));
        int dfscur;
        scanf("%d%d",&m,&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            for(int j=0;j<m;j++)
            {
                if(a[i][j]=='.')
                {
                    mark[i][j]=1;
                }
                if(a[i][j]=='S')
                {
                   x=i;
                   y=j;
                   if(i==n-1)
                   dfscur=0;
                   else if(j==m-1)
                   dfscur=1;
                   else if(i==0)
                   dfscur=2;
                   else if(j==0)
                   dfscur=3;
                }
                if(a[i][j]=='E')
                {
                    mark[i][j]=1;
                    ex=i;
                    ey=j;
                }
            }
        }
        step=1,step2=1;
        switch(dfscur)
        {
            case 0:dfs(x-1,y,0);break;
            case 1:dfs(x,y-1,1);break;
            case 2:dfs(x+1,y,2);break;
            case 3:dfs(x,y+1,3);break;
        }
        printf("%d ",step);
        switch(dfscur)
        {
            case 0:dfs2(x-1,y,0);break;
            case 1:dfs2(x,y-1,1);break;
            case 2:dfs2(x+1,y,2);break;
            case 3:dfs2(x,y+1,3);break;
        }
        printf("%d ",step2);
        printf("%d\n",bfs(x,y));
    }
}
























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值