POJ 3083 Children of the Candy Corn(顺时针DFS+逆时针DFS+BFS)

题目链接:POJ 3083 Children of the Candy Corn

【题意】给出一个迷宫,不超过40*40,‘#’代表墙,‘.’代表能走,‘S’是起点,‘E’是终点。分别求出从起点一直沿左走,一直沿右走,走到终点所需要的步数。以及走出迷宫的最小步数。

【思路】首先最小步数很简单,一个普通BFS搞定,这道题重点是一直向左走和一直向右走的DFS的方向问题,方向还和游客当时朝向有关。开始一直认为是每次都向左(右)转,直到可以走,然后就一直不对,在google了之后才知道向左走要遵循左上右下的顺时针DFS搜索顺序,向右走要遵循右上左下的逆时针DFS搜索顺序。

具体思路是这样的,这是我画的方向草图:

举个例来说,一直向左,最初的起点由S决定,如果当前位置在1,那么面向的就是3,那么就要先去试探2方向可行否,不行的话就试探3,然后0,最后是1。遵循先向左,不行则顺时针搜索的规则。一直向右边的方法同理,遵循先向右,不行则逆时针搜索的规则。方向的更新按照我定义的是:

nowface = (face + to[i]) % 4;

face是当前朝向,to[i]具体看代码,每回都需要%4,保证数字不超出范围。

还有一点必须要注意的:

左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的路,并且一旦哪一步确定了走的方向,就不必去搜索其它方向了,按照规则一直搜索下去。


下面贴代码:

/*
** POJ 3083 Children of the Candy Corn
** Created by Rayn @@ 2014/05/09
** 这道题重点是left和right的DFS的方向问题,方向还和游客
** 当时朝向有关,BFS就是很普通的最短路搜索
** left搜索要遵循左上右下的顺时针搜索顺序
** right搜索要遵循右上左下的逆时针搜索顺序
** 朝向标记 :左0 上1 右2 下3
*/
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAX = 50;
struct pos{
    int x, y;
    int step;
};
int left, right;
int w, h, sign[MAX][MAX];
char maze[MAX][MAX];
int dir[][2] = {{-1,0},{0,1},{1,0},{0,-1}}; /*0-上,1-右,2-下,3-左*/

void leftDFS(int x, int y, int face)
{
    int to[4] = {3,0,1,2};

    if(maze[x][y] == 'E')
    {
        printf("%d ", left);
        return ;
    }
    left++;
    for(int i=0; i<4; ++i)
    {
        int nowface = (face + to[i]) % 4;
        int tx = x + dir[nowface][0];
        int ty = y + dir[nowface][1];
        if(tx>=0 && ty>=0 && tx<h && ty<w && maze[tx][ty] != '#')
        {
            leftDFS(tx, ty, nowface);
            return ;
        }
    }
}
void rightDFS(int x, int y, int face)
{
    int to[4] = {1,0,3,2};

    if(maze[x][y] == 'E')
    {
        printf("%d ", right);
        return ;
    }
    right++;
    for(int i=0; i<4; ++i)
    {
        int nowface = (face + to[i]) % 4;
        int tx = x + dir[nowface][0];
        int ty = y + dir[nowface][1];
        if(tx>=0 && ty>=0 && tx<h && ty<w && maze[tx][ty] != '#')
        {
            rightDFS(tx, ty, nowface);
            return ;
        }
    }
}
void BFS(int x, int y)
{
    queue<pos> q;
    while(!q.empty())
        q.pop();

    pos start;
    start.x = x; start.y = y; start.step = 1;
    q.push(start);
    sign[x][y] = 1;

    while(!q.empty())
    {
        pos now = q.front();
        if(maze[now.x][now.y] == 'E')
        {
            printf("%d\n", now.step);
            return ;
        }
        q.pop();
        for(int i=0; i<4; ++i)
        {
            pos tmp = now;
            tmp.x = now.x + dir[i][0];
            tmp.y = now.y + dir[i][1];
            tmp.step = now.step + 1;
            if(tmp.x>=0 && tmp.y>=0 && tmp.x<h && tmp.y<w && maze[tmp.x][tmp.y] != '#' && !sign[tmp.x][tmp.y])
            {
                sign[tmp.x][tmp.y] = 1;
                q.push(tmp);
            }
        }
    }
}
int main()
{
    int cases;
    int sx, sy, face;

    scanf("%d", &cases);
    while(cases--)
    {
        memset(sign, 0, sizeof(sign));

        scanf("%d%d%*c", &w, &h);
        for(int i=0; i<h; ++i)
        {
            scanf("%s", maze[i]);
            for(int j=0; j<w; ++j)
            {
                if(maze[i][j] == 'S')
                {
                    sx = i;
                    sy = j;
                }
            }
        }
        //确定初始方位
        if(sx == 0)
            face = 2;
        else if(sx == h-1)
            face = 0;
        if(sy == 0)
            face = 1;
        else if(sy == w-1)
            face = 3;

        left = 1;
        leftDFS(sx, sy, face);
        right = 1;
        rightDFS(sx, sy, face);
        BFS(sx, sy);
    }
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值