poj3083 Children of the Candy Corn

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

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

这道题的题意:给定一个迷宫,S是起点,E是终点,#是墙不可走,'.'可以走。

要向左优先搜索一遍,再向右一遍 ,再找一遍最短路,最短路要用bfs。

因为是向左右,所以要定义一个face变量,代表面向:                    2

                                                                                              1<--当前位置-->3

                                                                                                           4

还有有一点必须要注意的:

左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格

还有就是题中说了,迷宫四周是壁垒,也就是都是‘#’

#include<stdio.h>
#include<string.h>
int vis[50][50];
int a[50][50];
int flag,s;
int x1,y1;
void dfsl(int x,int y,int step,int face)
{
    if(flag==1)
        return ;
    if(x==x1&&y==y1)
    {
        flag=1;
        printf("%d ",step);
        return ;
    }
    if(face==1)
    {
        if(a[x+1][y])
            dfsl(x+1,y,step+1,4);
        if(a[x][y-1])
            dfsl(x,y-1,step+1,1);
        if(a[x-1][y])
            dfsl(x-1,y,step+1,2);
        if(a[x][y+1])
            dfsl(x,y+1,step+1,3);
    }
    else if(face==2)
    {

        if(a[x][y-1])
            dfsl(x,y-1,step+1,1);
        if(a[x-1][y])
            dfsl(x-1,y,step+1,2);
        if(a[x][y+1])
            dfsl(x,y+1,step+1,3);
        if(a[x+1][y])
            dfsl(x+1,y,step+1,4);
    }
    else if(face==3)
    {
        if(a[x-1][y])
            dfsl(x-1,y,step+1,2);
        if(a[x][y+1])
            dfsl(x,y+1,step+1,3);
        if(a[x+1][y])
            dfsl(x+1,y,step+1,4);
        if(a[x][y-1])
            dfsl(x,y-1,step+1,1);
    }
    else if(face==4)
    {
        if(a[x][y+1])
            dfsl(x,y+1,step+1,3);
        if(a[x+1][y])
            dfsl(x+1,y,step+1,4);
        if(a[x][y-1])
            dfsl(x,y-1,step+1,1);
        if(a[x-1][y])
            dfsl(x-1,y,step+1,2);
    }
}
void dfsr(int x,int y,int step,int face)
{
    if(flag==1)
        return ;
    if(x==x1&&y==y1)
    {
        flag=1;
        printf("%d ",step);
        return ;
    }
    if(face==1)
    {
        if(a[x-1][y])
            dfsr(x-1,y,step+1,2);
        if(a[x][y-1])
            dfsr(x,y-1,step+1,1);
        if(a[x+1][y])
            dfsr(x+1,y,step+1,4);
        if(a[x][y+1])
            dfsr(x,y+1,step+1,3);
    }
    else if(face==2)
    {
        if(a[x][y+1])
            dfsr(x,y+1,step+1,3);
        if(a[x-1][y])
            dfsr(x-1,y,step+1,2);
        if(a[x][y-1])
            dfsr(x,y-1,step+1,1);
        if(a[x+1][y])
            dfsr(x+1,y,step+1,4);
    }
    else if(face==3)
    {
        if(a[x+1][y])
            dfsr(x+1,y,step+1,4);
        if(a[x][y+1])
            dfsr(x,y+1,step+1,3);
        if(a[x-1][y])
            dfsr(x-1,y,step+1,2);
        if(a[x][y-1])
            dfsr(x,y-1,step+1,1);
    }
    else
    {

        if(a[x][y-1])
            dfsr(x,y-1,step+1,1);
        if(a[x+1][y])
            dfsr(x+1,y,step+1,4);
        if(a[x][y+1])
            dfsr(x,y+1,step+1,3);
        if(a[x-1][y])
            dfsr(x-1,y,step+1,2);
    }
}
struct node
{
    int x,y,step;
} q[2000];
void bfs(int xx,int yy)
{
    memset(vis,0,sizeof(vis));
    vis[xx][yy]=1;
    int l,r;
    l=r=0;
    q[r].x=xx;
    q[r].y=yy;
    q[r++].step=1;
    while(l<r)
    {
        struct node temp;
        temp=q[l++];
        if(temp.x==x1&&temp.y==y1)
        {
            printf("%d\n",temp.step);
            break;
        }
        if(vis[temp.x-1][temp.y]==0&&a[temp.x-1][temp.y])
        {
            vis[temp.x-1][temp.y]=1;
            q[r].x=temp.x-1;
            q[r].y=temp.y;
            q[r++].step=temp.step+1;
        }
        if(vis[temp.x+1][temp.y]==0&&a[temp.x+1][temp.y])
        {
            vis[temp.x+1][temp.y]=1;
            q[r].x=temp.x+1;
            q[r].y=temp.y;
            q[r++].step=temp.step+1;
        }
        if(vis[temp.x][temp.y-1]==0&&a[temp.x][temp.y-1])
        {
            vis[temp.x][temp.y-1]=1;
            q[r].x=temp.x;
            q[r].y=temp.y-1;
            q[r++].step=temp.step+1;
        }
        if(vis[temp.x][temp.y+1]==0&&a[temp.x][temp.y+1])
        {
            vis[temp.x][temp.y+1]=1;
            q[r].x=temp.x;
            q[r].y=temp.y+1;
            q[r++].step=temp.step+1;
        }
    }
    return ;
}
int main()
{
    int T,n,m,i,j;
    char st[50];
    scanf("%d",&T);
    while(T--)
    {
        int xx,yy;
        scanf("%d%d",&n,&m);
        memset(a,0,sizeof(a));
        for(i=1; i<=m; i++)
        {
            scanf("%s",st);
            for(j=0; j<n; j++)
            {
                if(st[j]=='.')
                    a[i][j+1]=1;
                if(st[j]=='S')
                {
                    xx=i;
                    yy=j+1;
                    a[i][j+1]=1;
                }
                if(st[j]=='E')
                {
                    x1=i;
                    y1=j+1;
                    a[i][j+1]=1;
                }
            }
        }
        flag=0;
        dfsl(xx,yy,1,1);
        flag=0;
        dfsr(xx,yy,1,1);
        bfs(xx,yy);
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值