POJ3083:Children of the Candy Corn(DFS、BFS)

Time Limit: 1000MS
Memory Limit: 65536K


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


解题分析

输出的三个数分别是深度-左墙优先、深度-右墙优先和广度优先搜索的答案。

深度-左墙优先搜索顺序

这里写图片描述

深度-右墙优先搜索顺序

这里写图片描述

f(nd)=(i,d)关系表    nd=((d-1+4)%4)+i)%4

这里写图片描述


AC代码

#include<iostream>
#include<queue>
using namespace std;
char Maze[50][50];
int StartX,StartY;
int EndX,EndY;
int w,h;

int DirL[][2]={{0,-1},{-1,0},{0,1},{1,0}};
int DirR[][2]={{0,1},{-1,0},{0,-1},{1,0}};

int DFS(int r,int c,int d,int Dir[][2],int Steps)
{
    //DFS(当前横、纵坐标,当前方向,采用的搜索顺序,当前步数)
    if(r==EndX&&c==EndY)
        return Steps;

    for(int i=0;i<4;i++){

        int nd=((d-1+4)%4+i)%4;
        int x=r+Dir[nd][0];
        int y=c+Dir[nd][1];

        if(x<1||x>h||y<1||y>w||Maze[x][y]=='#')
            continue;

        return DFS(x,y,nd,Dir,Steps+1);
        //因不设Visited数组,故要return,否则死循环
    }
}

int VisOfBFS[50][50];
struct Point{
    int x,y;
    int Layers;
    Point(int xx,int yy,int L):x(xx),y(yy),Layers(L){}
};
queue<Point>q;

int BFS(int r,int c)
{
    while(!q.empty())
        q.pop();

    q.push(Point(r,c,1));
    VisOfBFS[r][c]=1;

    while(!q.empty()){
        Point p=q.front();
        q.pop();

        if(p.x==EndX&&p.y==EndY)
            return p.Layers;
        else{
            for(int i=0;i<4;i++){
                int nx=p.x+DirL[i][0];
                int ny=p.y+DirL[i][1];

                if(nx<1||nx>h||ny<1||ny>w||VisOfBFS[nx][ny]||Maze[nx][ny]=='#')
                    continue;

                q.push(Point(nx,ny,p.Layers+1));
                VisOfBFS[nx][ny]=1;
            }
        }
    }
}

int main()
{
    int T;
    cin>>T;
    while(T--){

        cin>>w>>h;
        for(int i=1;i<=h;i++)
            for(int j=1;j<=w;j++){
                Maze[i][j]='#';
                VisOfBFS[i][j]=0;
            }

        for(int i=1;i<=h;i++)
            for(int j=1;j<=w;j++){
                cin>>Maze[i][j];
                if(Maze[i][j]=='S')
                    StartX=i,StartY=j;
                if(Maze[i][j]=='E')
                    EndX=i,EndY=j;
            }

        int dL,dR;
        if(StartX==1)
            dL=3,dR=3;
        else if(StartX==h)
            dL=1,dR=1;
        else if(StartY==1)
            dL=2,dR=0;
        else if(StartY==w)
            dL=0,dR=2;
        //根据'S'的位置,确定起始方向

        cout<<DFS(StartX,StartY,dL,DirL,1)<<' '<<DFS(StartX,StartY,dR,DirR,1)<<' '<<BFS(StartX,StartY)<<endl;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值