poj Children of the Candy Corn(裸bfs+一点技巧)

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

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

靠着左墙走的话,想象一下,在状态中记录行走的方向,在已知方向上向左看是否能走,如果能就走那条路,抛弃其他情况;如果左走不了就向前……;前走不了就向右……;右走不了就向后(后一定能走);

仔细推敲下,就能想出来,写的时候细心点就能AC


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
char g[50][50];
bool done[50][50];
int n,m,sx,sy,ex,ey;
int ldx[]={0,-1,0,1};
int ldy[]={-1,0,1,0};
int rdx[]={0,-1,0,1};
int rdy[]={1,0,-1,0};
struct node
{
    int x,y,w,dir;
    node(int xx=0,int yy=0,int ww=0,int dd=0)
    {
        x=xx;y=yy;w=ww;dir=dd;
    }
};
bool is_ill(int x,int y)
{
    return x<0||y<0||x>=n||y>=m;
}
int bfs1(int *dx,int *dy)
{
    queue<node>q;
    int i,last=1;
    for(i=0;i<5;i++)
    {
        int curx=sx+dx[i%4];
        int cury=sy+dy[i%4];
        if(is_ill(curx,cury)||g[curx][cury]=='#')
        {
            last=0;
            continue;
        }
        if(!last)break;
    }
    q.push(node(sx+dx[i%4],sy+dy[i%4],2,i));

    while(!q.empty())
    {
        node x=q.front();q.pop();
        for(i=0;i<4;i++)
        {
            int curx=x.x+dx[(i+x.dir+3)%4];
            int cury=x.y+dy[(i+x.dir+3)%4];
            if(is_ill(curx,cury)||g[curx][cury]=='#')continue;
            if(g[curx][cury]=='E')return x.w+1;
            if(g[curx][cury]=='.')
            {
                q.push(node(curx,cury,x.w+1,(i+x.dir+3)%4));
                break;
            }
        }
    }
    return -1;
}
int bfs(int *dx,int *dy)
{
    queue<node>q;
    memset(done,0,sizeof done);
    q.push(node(sx,sy,1));
    done[sx][sy]=1;
    while(!q.empty())
    {
        node x=q.front();q.pop();
        for(int i=0;i<4;i++)
        {
            int curx=x.x+dx[i];
            int cury=x.y+dy[i];
            if(is_ill(curx,cury)||g[curx][cury]=='#'||done[curx][cury])continue;
            if(g[curx][cury]=='E')return x.w+1;
            if(g[curx][cury]=='.')
            {
                q.push(node(curx,cury,x.w+1,(i+x.dir+3)%4));
                done[curx][cury]=1;
            }
        }
    }
    return -1;
}
int main()
{
    int N,i,j,k;
    scanf("%d",&N);
    while(N--)
    {
        scanf("%d%d",&m,&n);
        for(i=0;i<n;i++)
            scanf("%s",g[i]);
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
            if(g[i][j]=='S')sx=i,sy=j;
        printf("%d %d %d\n",bfs1(ldx,ldy),bfs1(rdx,rdy),bfs(ldx,ldy));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值