加载中...

POJ 3083 - Children of the Candy Corn


0x00 POJ 原题

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

Source

South Central USA 2006

0x10 题目概述

给定一个迷宫,S 是起点,E 是终点,# 是墙不可走,. 可以走

先输出左转优先时,从 S 到 E 的步数

再输出右转优先时,从 S 到 E 的步数

最后输出 S 到 E 的最短步数

W 为宽,列数

H 为高,行数

0x20 解题思路

DFS 和 BFS 的综合题水题,难度不大,但是写代码时要注意几方面:

0x21 1. 左转、右转优先搜索

搜索时必须标记当前位置的方向,我定义的方向是

最初的方向由起点 S 确定,而下一步的方向则由前一步的走向决定。

例如 左边优先搜索:

当前位置的方向指向 1(向左),(这同时说明前一步是在第“3”的位置走过来的)

那么走下一步时,就要根据 2103 的顺序,先逐格确定当前位置周边的四格是否可行

若第一次确认 2 可行,就走到 2,在位置 2 时的方向为 2(向下)

若 2 不可行,则再确定 1,若 1 可行,就走到 1,在位置 1 时的方向为 1(向左)

若 1 也不可行,则再确定 0,若 0 可行,就走到 0,在位置 0 时的方向为 0(向上)

若 0 也不可行,说明进入了迷宫的死胡同,要从原路返回,走回 3

右边优先搜索也同理。

根据我定义的方向,设当前位置为 d,那么:

  • 左转,用数学式子表达就是 d=(d+1)%4
  • 右转,用数学式子表达就是 d=(d+3)%4

我比较懒,在我的程序中,DFS 和 BFS 都用了多入口的做法,有兴趣的同学可以利用我给出的这两个式子对代码进行优化。

这里还有一点必须要注意的: 左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格

0x22 2. 寻找最短路只能用 BFS

因此在做第 3 问时别傻乎乎的又用 DFS,DFS 对于样例的输入确实和 BFS 得到的结果一样的,别以为样例 PASS 就提交了。。。所以我就说样例没代表性,学会测试数据很重要= =

需要注意的还有:

  • 要求 E 的最短路,必须把迷宫模拟为树,S 为根,找到 E 所在的层(树深),该层就是 S 到 E 的最短路,处理技巧就是在 BFS 时,令 queue[tail] 的 depth 等于对应的 queue[head] 的 depth+1,详细见我的程序
  • 把循环的次数作为深度就铁定错的

0x30 AC 源码

//Memory Time 
// 212K   0MS 

#include<iostream>
using namespace std;

typedef class
{
    public:
        int r,c;
        int depth;
}SE;

SE s,e; //起止点
int Lstep;  //左边优先搜索 时从S到E的总步数
int Rstep;  //右边优先搜索 时从S到E的总步数
int shortstep;  //S到E的最少总步数

bool maze[41][41]; //记录迷宫的“可行域”与“墙”

void DFS_LF(int i,int j,int d)    //左边优先搜索,i,j为当前点坐标,d为当前位置方向
{
    Lstep++;
    if(i==e.r && j==e.c)
        return;

    switch(d)
    {
        case 0:
            {
                if(maze[i][j-1])
                    DFS_LF(i,j-1,1);
                else if(maze[i-1][j])
                    DFS_LF(i-1,j,0);
                else if(maze[i][j+1])
                    DFS_LF(i,j+1,3);
                else if(maze[i+1][j])
                    DFS_LF(i+1,j,2);
                break;
            }
        case 1:
            {
                if(maze[i+1][j])
                    DFS_LF(i+1,j,2);
                else if(maze[i][j-1])
                    DFS_LF(i,j-1,1);
                else if(maze[i-1][j])
                    DFS_LF(i-1,j,0);
                else if(maze[i][j+1])
                    DFS_LF(i,j+1,3);
                break;
            }
        case 2:
            {
                if(maze[i][j+1])
                    DFS_LF(i,j+1,3);
                else if(maze[i+1][j])
                    DFS_LF(i+1,j,2);
                else if(maze[i][j-1])
                    DFS_LF(i,j-1,1);
                else if(maze[i-1][j])
                    DFS_LF(i-1,j,0);
                break;
            }
        case 3:
            {
                if(maze[i-1][j])
                    DFS_LF(i-1,j,0);
                else if(maze[i][j+1])
                    DFS_LF(i,j+1,3);
                else if(maze[i+1][j])
                    DFS_LF(i+1,j,2);
                else if(maze[i][j-1])
                    DFS_LF(i,j-1,1);
                break;
            }
    }

    return;
}

void DFS_RF(int i,int j,int d)    //右边优先搜索,i,j为当前点坐标,d为当前位置方向
{
    Rstep++;
    if(i==e.r && j==e.c)
        return;

    switch(d)
    {
        case 0:
            {
                if(maze[i][j+1])
                    DFS_RF(i,j+1,3);
                else if(maze[i-1][j])
                    DFS_RF(i-1,j,0);
                else if(maze[i][j-1])
                    DFS_RF(i,j-1,1);
                else if(maze[i+1][j])
                    DFS_RF(i+1,j,2);
                break;
            }
        case 1:
            {
                if(maze[i-1][j])
                    DFS_RF(i-1,j,0);
                else if(maze[i][j-1])
                    DFS_RF(i,j-1,1);
                else if(maze[i+1][j])
                    DFS_RF(i+1,j,2);
                else if(maze[i][j+1])
                    DFS_RF(i,j+1,3);
                break;
            }
        case 2:
            {
                if(maze[i][j-1])
                    DFS_RF(i,j-1,1);
                else if(maze[i+1][j])
                    DFS_RF(i+1,j,2);
                else if(maze[i][j+1])
                    DFS_RF(i,j+1,3);
                else if(maze[i-1][j])
                    DFS_RF(i-1,j,0);
                break;
            }
        case 3:
            {
                if(maze[i+1][j])
                    DFS_RF(i+1,j,2);
                else if(maze[i][j+1])
                    DFS_RF(i,j+1,3);
                else if(maze[i-1][j])
                    DFS_RF(i-1,j,0);
                else if(maze[i][j-1])
                    DFS_RF(i,j-1,1);
                break;
            }
    }
    return;
}

void BFS_MSS(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;  //size of maze
        cin>>w>>h;

        /*Initial*/

        Lstep=1;
        Rstep=1;
        memset(maze,false,sizeof(maze));

        /*Structure the 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;
                }
            }

        /*Left First Search*/

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

        /*Right First Search*/

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

        /*Most Short Step Search*/

        BFS_MSS(s.r,s.c);

    }
    return 0;
}

相关资料


文章作者: EXP
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 EXP !
赏
  目录