POJ 3083 Children of the Candy Corn( BFS + DFS )有意思的题目

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

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


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


【题意】   

   输入:

        输入T,表示T组测试数据。

       每组测试数据输入M,N,表示N行M列,其中‘#’表示不能走,‘ . ’表示能走,‘S’表示开始点,‘E’表示终点。

   输出:

        沿着左墙走的步数、沿着右墙走的步数和最短的步数。


【分析】

       其实就是输出分别以优先往左拐优先往右拐最短路的步数的步数。

       其中优先拐问题:

       优先往左拐:自己面向的方向往左拐,如果左边不能走,自己顺时针旋转90度,继续左拐,所以就是遍历顺序以顺时针进行遍历;

       优先往右拐:自己面向的方向往右拐,如果右边不能走,自己逆时针旋转90度,继续右拐,所以就是遍历顺序以逆时针进行遍历。

       其中优先拐问题需要用DFS遍历,最短路需要用BFS,因为沿着墙走可以走走过的路,所以不能标记,难点就在怎样实现按顺时针(逆时针)走,这里我用的是记录面向的方向,方向数组用顺时针和逆时针两种,其中面向的方向 - 1 就是需要走的方向,更新面向的方向就可以实现顺时针(逆时针)走。


       BFS就是简单的BFS,这里不介绍了。


【代码】

#include<stdio.h>
#include<string.h>
#define MAXN 40+5
#define MAXM 40+5
char e[MAXN][MAXM];
bool book[MAXN][MAXM];
int n,m,step,flag,cnt;
int fang_left[4][2]= {0,-1,-1,0,0,1,1,0};   // 优先向左拐,左边不能走,身体向右转
int fang_right[4][2]= {0,-1,1,0,0,1,-1,0};  // 优先向右拐,右边不能走,身体向左转
struct node
{
    int x,y,z;
}s[10000];
// f表示上一步走到这点的方向
void dfs(int x,int y,int f)
{
    int tx,ty,k=f,l=4; // k表示当前的面向
    while(l--)
    {
        int h=k;    // h表示左拐(右拐)的方向
        if(k==0)
            h=3;
        else h=k-1;
        if(cnt==1)
        {
            tx=x+fang_left[h][0];
            ty=y+fang_left[h][1];
        }
        else if(cnt==2)
        {
            tx=x+fang_right[h][0];
            ty=y+fang_right[h][1];
        }
        k++; // 能不能走都要向右转向,如果这条路能走但是到不了终点回溯到这点时就会死循环
        if(k==5)
            k=1;
        if(tx<0||tx>=n||ty<0||ty>=m||e[tx][ty]=='#')
        {
            continue;
        }
        if(flag) return ;
        step++;
        if(e[tx][ty]=='E')
        {
            flag=1;
            return ;
        }
        dfs(tx,ty,h);
        break;
    }
    return ;
}

int bfs(int x,int y)
{
    memset(book,false,sizeof(book));
    int head=1,tail=1,flag=0;
    s[tail].x=x;
    s[tail].y=y;
    s[tail].z=1;
    book[x][y]=true;
    tail++;
    while(head<tail)
    {
        int tx,ty;
        for(int k=0;k<4;k++)
        {
            tx=s[head].x+fang_left[k][0];
            ty=s[head].y+fang_left[k][1];
            if(tx<0||tx>=n||ty<0||ty>=m||book[tx][ty]||e[tx][ty]=='#')
                continue;
            if(e[tx][ty]=='E')
            {
                flag=1;
                break;
            }
            s[tail].x=tx;
            s[tail].y=ty;
            s[tail].z=s[head].z+1;
            tail++;
            book[tx][ty]=1;
        }
        if(flag)
            break;
        head++;
    }
    return s[head].z+1;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int start_x,start_y;
        scanf("%d %d",&m,&n);
        for(int i=0; i<n; i++)
        {
            scanf("%s",e[i]);
            for(int j=0; j<m; j++)
            {
                if(e[i][j]=='S')
                {
                    start_x=i;
                    start_y=j;
                }
            }
        }
        step=1;flag=0;cnt=1;
        dfs(start_x,start_y,1);
        printf("%d ",step);
        step=1;flag=0;cnt=2;
        dfs(start_x,start_y,1);
        printf("%d ",step);
        step=bfs(start_x,start_y);
        printf("%d\n",step);
    }
    return 0;
}


如果博客有什么请留言奋斗~~










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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值