POJ3083-Children of the Candy Corn(bfs+dfs)

本文解析了POJ3083迷宫问题,介绍了如何使用DFS和BFS算法解决迷宫中最短路径的问题。文章通过具体实例详细阐述了左转优先、右转优先以及最短路径的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ3083 原题链接:http://poj.org/problem?id=3083

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

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



题目大意:输入列为w,行为h的迷宫,'#'代表墙不能走, '.'代表路可以走,S为起点,E为终点,所求为:1.以左转优先时需要的步数,2.右转优先时需要的步数,3.最短的步数。

题目这里的意思我弄错了很多次,最后看大佬们的博客才看懂什么意思:

1.左转优先:即以当前方向为正方向,能左转就左转,不能的话往正方向走,还是不能往反方向走,如果都不能最后往右转,比如说当前方向为←,那么此时的优先顺序是↓,←,→,↑;

2.右转优先:同上,比如说当前方向为↑,那么此时的优先顺序是→,↑,↓,←;

3.最短路:即求从S到E的最短路径(bfs);



思路:理解题意之后就感觉不是很难了,两个dfs+一个bfs即可,添加一个标记用来记录当前的正方向,在以当前的正方向来决定优先顺序(超麻烦啊。

代码如下:

#include<cstring>
using namespace std;  
  
typedef class  
{  
    public:  
        int r,c;  
        int depth;  
}SE;  
  
SE s,e; //起止点  
int Lstep;  //左边优先搜索 时从S到E的总步数  
int Rstep;  //右边优先搜索 时从S到E的总步数  
int Zstep;  //最短步数
  
bool maze[41][41]; //记录迷宫的“可行域”与“墙”  
  
void zuo(int i,int j,int d)    //左边优先
{  
    Lstep++;  
    if(i==e.r && j==e.c)  
        return;  
  
    switch(d)  
    {  
        case 0:  
            {  
                if(maze[i][j-1])  
                    zuo(i,j-1,1);  
                else if(maze[i-1][j])  
                    zuo(i-1,j,0);  
                else if(maze[i][j+1])  
                    zuo(i,j+1,3);  
                else if(maze[i+1][j])  
                    zuo(i+1,j,2);  
                break;  
            }  
        case 1:  
            {  
                if(maze[i+1][j])  
                    zuo(i+1,j,2);  
                else if(maze[i][j-1])  
                    zuo(i,j-1,1);  
                else if(maze[i-1][j])  
                    zuo(i-1,j,0);  
                else if(maze[i][j+1])  
                    zuo(i,j+1,3);  
                break;  
            }  
        case 2:  
            {  
                if(maze[i][j+1])  
                    zuo(i,j+1,3);  
                else if(maze[i+1][j])  
                    zuo(i+1,j,2);  
                else if(maze[i][j-1])  
                    zuo(i,j-1,1);  
                else if(maze[i-1][j])  
                    zuo(i-1,j,0);  
                break;  
            }  
        case 3:  
            {  
                if(maze[i-1][j])  
                    zuo(i-1,j,0);  
                else if(maze[i][j+1])  
                    zuo(i,j+1,3);  
                else if(maze[i+1][j])  
                    zuo(i+1,j,2);  
                else if(maze[i][j-1])  
                    zuo(i,j-1,1);  
                break;  
            }  
    }  
  
    return;  
}  
  
void you(int i,int j,int d)    //右边优先
{  
    Rstep++;  
    if(i==e.r && j==e.c)  
        return;  
  
    switch(d)  
    {  
        case 0:  
            {  
                if(maze[i][j+1])  
                    you(i,j+1,3);  
                else if(maze[i-1][j])  
                    you(i-1,j,0);  
                else if(maze[i][j-1])  
                    you(i,j-1,1);  
                else if(maze[i+1][j])  
                    you(i+1,j,2);  
                break;  
            }  
        case 1:  
            {  
                if(maze[i-1][j])  
                    you(i-1,j,0);  
                else if(maze[i][j-1])  
                    you(i,j-1,1);  
                else if(maze[i+1][j])  
                    you(i+1,j,2);  
                else if(maze[i][j+1])  
                    you(i,j+1,3);  
                break;  
            }  
        case 2:  
            {  
                if(maze[i][j-1])  
                    you(i,j-1,1);  
                else if(maze[i+1][j])  
                    you(i+1,j,2);  
                else if(maze[i][j+1])  
                    you(i,j+1,3);  
                else if(maze[i-1][j])  
                    you(i-1,j,0);  
                break;  
            }  
        case 3:  
            {  
                if(maze[i+1][j])  
                    you(i+1,j,2);  
                else if(maze[i][j+1])  
                    you(i,j+1,3);  
                else if(maze[i-1][j])  
                    you(i-1,j,0);  
                else if(maze[i][j-1])  
                    you(i,j-1,1);  
                break;  
            }  
    }  
    return;  
}  
  
void zuiduanbfs(int i,int j)    //最短路搜索  
{  
    bool vist[41][41]={false};  
    SE queue[1600];  
    int head,tail;  
  
    queue[head=0].r=i;  
    queue[tail=0].c=j;  
    queue[tail++].depth=1;  //当前树深标记,这是寻找最短路的关键点  
  
    vist[i][j]=true;  
  
    while(head<tail)  
    {  
        SE x=queue[head++];  
  
        if(x.r==e.r && x.c==e.c)  
        {  
            cout<<x.depth<<endl;  
            return;  
        }  
  
        if(maze[x.r][x.c-1] && !vist[x.r][x.c-1])  
        {  
            vist[x.r][x.c-1]=true;  
            queue[tail].r=x.r;  
            queue[tail].c=x.c-1;  
            queue[tail++].depth=x.depth+1;  
        }  
        if(maze[x.r-1][x.c] && !vist[x.r-1][x.c])  
        {  
            vist[x.r-1][x.c]=true;  
            queue[tail].r=x.r-1;  
            queue[tail].c=x.c;  
            queue[tail++].depth=x.depth+1;  
        }  
        if(maze[x.r][x.c+1] && !vist[x.r][x.c+1])  
        {  
            vist[x.r][x.c+1]=true;  
            queue[tail].r=x.r;  
            queue[tail].c=x.c+1;  
            queue[tail++].depth=x.depth+1;  
        }  
        if(maze[x.r+1][x.c] && !vist[x.r+1][x.c])  
        {  
            vist[x.r+1][x.c]=true;  
            queue[tail].r=x.r+1;  
            queue[tail].c=x.c;  
            queue[tail++].depth=x.depth+1;  
        }  
    }  
    return;  
}  
  
int main(int i,int j)  
{  
    int test;  
    cin>>test;  
    while(test--)  
    {  
        int direction;  //起点S的初始方向  
        int w,h; 
        cin>>w>>h;  

  
        Lstep=1;  
        Rstep=1;  
        memset(maze,false,sizeof(maze));  
  
        for(i=1;i<=h;i++)  
            for(j=1;j<=w;j++)  
            {  
                char temp;  
                cin>>temp;  
                if(temp=='.')  
                    maze[i][j]=true;  
                if(temp=='S')  
                {  
                    maze[i][j]=true;  
                    s.r=i;  
                    s.c=j;  
  
                    if(i==h)  
                        direction=0;  
                    else if(j==w)  
                        direction=1;  
                    else if(i==1)  
                        direction=2;  
                    else if(j==1)  
                        direction=3;  
                }  
                if(temp=='E')  
                {  
                    maze[i][j]=true;  
                    e.r=i;  
                    e.c=j;  
                }  
            }  

  
        switch(direction)  
        {  
            case 0: {zuo(s.r-1,s.c,0); break;}  
            case 1: {zuo(s.r,s.c-1,1); break;}  
            case 2: {zuo(s.r+1,s.c,2); break;}  
            case 3: {zuo(s.r,s.c+1,3); break;}  
        }  
        cout<<Lstep<<' ';  
 
  
        switch(direction)  
        {  
            case 0: {you(s.r-1,s.c,0); break;}  
            case 1: {you(s.r,s.c-1,1); break;}  
            case 2: {you(s.r+1,s.c,2); break;}  
            case 3: {you(s.r,s.c+1,3); break;}  
        }  
        cout<<Rstep<<' ';  
        zuiduanbfs(s.r,s.c);  
          
    }  
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值