Children of the Candy Corn(POJ--3083

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.
题意:总共有T组数据,每组输入w和h,分别代表迷宫的列数和行数,迷宫的出口只会在边界,求从入口到出口沿着左墙走的步数,从入口到出口沿着右墙的步数,从入口到出口最短的步数。
思路:求沿着左墙和右墙走的步数是用dfs,求最短步数时用bfs。

Sample Input

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9

<span style="font-size:18px;">#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int w,h;
bool isok;
char map[50][50];
int dx[]= {-1,0,1,0};                       //逆时针方向
int dy[]= {0,1,0,-1};
bool vis[50][50];
int sum[50][50];
void dfs(int sx,int sy,int ex,int ey,int dir,int &ans)          //传入的实参前边加一个&意思就是说在此函数中它改变成多少,在原函数中它也改变成多少
{
    ans++;                                       //只要走到当前这一步步数就+1
    if(sx==ex&&sy==ey)
    {
        isok=true;                             //如果已经找到出口就标记一下以防返回上一层后接着遍历
        return ;
    }
    for(int i=dir+3; i<dir+7; i++)    //因为这是沿着右墙走,先考虑此时坐标的右边方位
    {
        int d=i%4;
        int xx=sx+dx[d];
        int yy=sy+dy[d];
        if(xx>0&&xx<=w&&yy>0&&yy<=h&&map[yy][xx]!='#')
        {
            dfs(xx,yy,ex,ey,d,ans);
            if(isok)
                return ;
        }
    }
    return ;
}
void bfs(int sx,int sy,int ex,int ey)
{
    queue<pair<int,int> >s;
    s.push(pair<int,int>(sx,sy));
    sum[sy][sx]=1;
    vis[sy][sx]=true;
    while(!s.empty())
    {
        int xx=s.front().first;
        int yy=s.front().second;
        s.pop();
        if(xx==ex&&yy==ey)
        {
            printf("%d\n",sum[ey][ex]);
            return ;
        }
        for(int i=0; i<4; i++)
        {
            int x=xx+dx[i];
            int y=yy+dy[i];
            if(x>0&&x<=w&&y>0&&y<=h&&!vis[y][x]&&map[y][x]!='#')
            {
                sum[y][x]=sum[yy][xx]+1;
                vis[y][x]=true;
                s.push(pair<int,int>(x,y));
            }
        }
    }
    return ;
}
int main()
{
    //freopen("aa.text","r",stdin);
    int T,sx,sy,ex,ey;
    scanf("%d",&T);
    while(T--)
    {
        memset(map,0,sizeof(map));
        scanf("%d %d",&w,&h);
        for(int i=1; i<=h; i++)
            scanf("%s",map[i]+1);
        for(int i=1; i<=h; i++)
            for(int j=1; j<=w; j++)
            {
                if(map[i][j]=='S')
                {
                    sx=j;                            //一定要分清楚谁代表行谁代表列
                    sy=i;
                }
                if(map[i][j]=='E')
                {
                    ex=j;
                    ey=i;
                }
            }
        int ans1=0,ans2=0;
        isok=false;
        dfs(sx,sy,ex,ey,0,ans2);
        isok=false;
        dfs(ex,ey,sx,sy,0,ans1);
        printf("%d %d ",ans1,ans2);
        memset(vis,0,sizeof(vis));
        memset(sum,0,sizeof(sum));
        bfs(sx,sy,ex,ey);
    }
    return 0;
}</span>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值