poj3083Children of the Candy Corn(bfs)

浏览: 102 发布日期: 2017-07-01 分类: c++

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

走迷宫的时候一直靠着左边或右边的墙就一定能走出去,题目就是求这两种方法下走出迷宫需要的步数和最小的步数
靠左走和靠右走需要判断方向,可以由上一步的位置推导出来,只不过有点麻烦,最短步数不用多说了,另外数组要开大点。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define INF 0x3f3f3f3f
#define MAXN 2005
#define Mod 10001
using namespace std;
struct Node
{
    int x,y,step;
    char p;
};
char map[500][500];
int si,sj,ei,ej;
int h,w;
int dfs1(int x,int y)
{
    Node start,tmp;
    start.x=x,start.y=y;
    start.step=1;
    queue q;
    if(y==1)
    {
        start.step++;
        start.y++;
        start.p='e';
    }
    else if(x==1)
    {
        start.step++;
        start.x++;
        start.p='s';
    }
    else if(y==w)
    {
        start.step++;
        start.y--;
        start.p='w';
    }
    else if(x==h)
    {
        start.step++;
        start.x--;
        start.p='n';
    }
    q.push(start);
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp.x==ei&&tmp.y==ej)
            return tmp.step;
        if(tmp.p=='e')
        {
            if(map[tmp.x-1][tmp.y]!='#')
            {
                tmp.x--;
                tmp.step++;
                tmp.p='n';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y+1]!='#')
            {
                tmp.y++;
                tmp.step++;
                tmp.p='e';
                q.push(tmp);
            }
            else if(map[tmp.x+1][tmp.y]!='#')
            {
                tmp.x++;
                tmp.step++;
                tmp.p='s';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y-1]!='#')
            {
                tmp.y--;
                tmp.step++;
                tmp.p='w';
                q.push(tmp);
            }
        }
        else if(tmp.p=='s')
        {
            if(map[tmp.x][tmp.y+1]!='#')
            {
                tmp.y++;
                tmp.step++;
                tmp.p='e';
                q.push(tmp);
            }
            else if(map[tmp.x+1][tmp.y]!='#')
            {
                tmp.x++;
                tmp.step++;
                tmp.p='s';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y-1]!='#')
            {
                tmp.y--;
                tmp.step++;
                tmp.p='w';
                q.push(tmp);
            }
            else if(map[tmp.x-1][tmp.y]!='#')
            {
                tmp.x--;
                tmp.step++;
                tmp.p='n';
                q.push(tmp);
            }
        }
        else if(tmp.p=='w')
        {
            if(map[tmp.x+1][tmp.y]!='#')
            {
                tmp.x++;
                tmp.step++;
                tmp.p='s';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y-1]!='#')
            {
                tmp.y--;
                tmp.step++;
                tmp.p='w';
                q.push(tmp);
            }
            else if(map[tmp.x-1][tmp.y]!='#')
            {
                tmp.x--;
                tmp.step++;
                tmp.p='n';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y+1]!='#')
            {
                tmp.y++;
                tmp.step++;
                tmp.p='e';
                q.push(tmp);
            }
        }
        else if(tmp.p=='n')
        {
            if(map[tmp.x][tmp.y-1]!='#')
            {
                tmp.y--;
                tmp.step++;
                tmp.p='w';
                q.push(tmp);
            }
            else if(map[tmp.x-1][tmp.y]!='#')
            {
                tmp.x--;
                tmp.step++;
                tmp.p='n';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y+1]!='#')
            {
                tmp.y++;
                tmp.step++;
                tmp.p='e';
                q.push(tmp);
            }
            else if(map[tmp.x+1][tmp.y]!='#')
            {
                tmp.x++;
                tmp.step++;
                tmp.p='s';
                q.push(tmp);
            }
        }
    }
}
int dfs2(int x,int y)
{
    Node start,tmp;
    start.x=x,start.y=y;
    start.step=1;
    queue q;
    if(y==1)
    {
        start.step++;
        start.y++;
        start.p='e';
    }
    else if(x==1)
    {
        start.step++;
        start.x++;
        start.p='s';
    }
    else if(y==w)
    {
        start.step++;
        start.y--;
        start.p='w';
    }
    else if(x==h)
    {
        start.step++;
        start.x--;
        start.p='n';
    }
    q.push(start);
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp.x==ei&&tmp.y==ej)
            return tmp.step;
        if(tmp.p=='e')
        {
            if(map[tmp.x+1][tmp.y]!='#')
            {
                tmp.x++;
                tmp.step++;
                tmp.p='s';
                q.push(tmp);
            }

            else if(map[tmp.x][tmp.y+1]!='#')
            {
                tmp.y++;
                tmp.step++;
                tmp.p='e';
                q.push(tmp);
            }
            else if(map[tmp.x-1][tmp.y]!='#')
            {
                tmp.x--;
                tmp.step++;
                tmp.p='n';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y-1]!='#')
            {
                tmp.y--;
                tmp.step++;
                tmp.p='w';
                q.push(tmp);
            }
        }
        else if(tmp.p=='s')
        {
            if(map[tmp.x][tmp.y-1]!='#')
            {
                tmp.y--;
                tmp.step++;
                tmp.p='w';
                q.push(tmp);
            }

            else if(map[tmp.x+1][tmp.y]!='#')
            {
                tmp.x++;
                tmp.step++;
                tmp.p='s';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y+1]!='#')
            {
                tmp.y++;
                tmp.step++;
                tmp.p='e';
                q.push(tmp);
            }
            else if(map[tmp.x-1][tmp.y]!='#')
            {
                tmp.x--;
                tmp.step++;
                tmp.p='n';
                q.push(tmp);
            }
        }
        else if(tmp.p=='w')
        {
            if(map[tmp.x-1][tmp.y]!='#')
            {
                tmp.x--;
                tmp.step++;
                tmp.p='n';
                q.push(tmp);
            }

            else if(map[tmp.x][tmp.y-1]!='#')
            {
                tmp.y--;
                tmp.step++;
                tmp.p='w';
                q.push(tmp);
            }
            else if(map[tmp.x+1][tmp.y]!='#')
            {
                tmp.x++;
                tmp.step++;
                tmp.p='s';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y+1]!='#')
            {
                tmp.y++;
                tmp.step++;
                tmp.p='e';
                q.push(tmp);
            }
        }
        else if(tmp.p=='n')
        {
            if(map[tmp.x][tmp.y+1]!='#')
            {
                tmp.y++;
                tmp.step++;
                tmp.p='e';
                q.push(tmp);
            }

            else if(map[tmp.x-1][tmp.y]!='#')
            {
                tmp.x--;
                tmp.step++;
                tmp.p='n';
                q.push(tmp);
            }
            else if(map[tmp.x][tmp.y-1]!='#')
            {
                tmp.y--;
                tmp.step++;
                tmp.p='w';
                q.push(tmp);
            }
            else if(map[tmp.x+1][tmp.y]!='#')
            {
                tmp.x++;
                tmp.step++;
                tmp.p='s';
                q.push(tmp);
            }
        }
    }
}
int vis[500][500];
int dfs3(int x,int y)
{
    vis[x][y]=1;
    Node start,tmp,tmp1;
    start.x=x,start.y=y;
    start.step=1;
    queue q;
    q.push(start);
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        if(tmp.x==ei&&tmp.y==ej)
            return tmp.step;
        if(map[tmp.x+1][tmp.y]!='#'&&!vis[tmp.x+1][tmp.y])
        {
            tmp1.x=tmp.x+1;
            tmp1.y=tmp.y;
            tmp1.step=tmp.step+1;
            vis[tmp.x+1][tmp.y]=1;
            q.push(tmp1);
        }
        if(map[tmp.x-1][tmp.y]!='#'&&!vis[tmp.x-1][tmp.y])
        {
            tmp1.x=tmp.x-1;
            tmp1.y=tmp.y;
            tmp1.step=tmp.step+1;
            vis[tmp.x-1][tmp.y]=1;
            q.push(tmp1);
        }
        if(map[tmp.x][tmp.y+1]!='#'&&!vis[tmp.x][tmp.y+1])
        {
            tmp1.x=tmp.x;
            tmp1.y=tmp.y+1;
            tmp1.step=tmp.step+1;
            vis[tmp.x][tmp.y+1]=1;
            q.push(tmp1);
        }
        if(map[tmp.x][tmp.y-1]!='#'&&!vis[tmp.x][tmp.y-1])
        {
            tmp1.x=tmp.x;
            tmp1.y=tmp.y-1;
            tmp1.step=tmp.step+1;
            vis[tmp.x][tmp.y-1]=1;
            q.push(tmp1);
        }
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&w,&h);
        for(int i=1; i<=h; ++i)
            for(int j=1; j<=w; ++j)
            {
                cin>>map[i][j];
                if(map[i][j]=='S')
                {
                    si=i;
                    sj=j;
                }
                if(map[i][j]=='E')
                {
                    ei=i;
                    ej=j;
                }
            }
        int ans1=dfs1(si,sj);
        int ans2=dfs2(si,sj);
        int ans3=dfs3(si,sj);
        cout<
以上就是poj3083Children of the Candy Corn(bfs)的全文介绍,希望对您学习和使用c++编程开发有所帮助.
返回顶部