poj 3083 Children of the Candy Corn

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

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

提示

题意:

玉米田迷宫是万圣节比较流行的活动,参加者必须面对恐怖去寻找迷宫的出口。

我们保证迷宫有入口和出口,你可以选择优先向左或向右走。当然,只有采取最短路径为最好(这不废话么)。

如果一个麦田要建成玉米田而你是策划者,那么你需要考虑优先向左或向右走以及最短路径所走的步数,请输出以上方式所走的步数。

思路:

这题比较麻烦的是优先向左或向右你需要考虑一下方向,我用0,1,2,3分别表示右,上,左,下,以下给出优先向左、向右的情况:

向左:

当前方向:遍历方向:
↓ ← ↑ →
← ↑ → ↓
↑ → ↓ ←
→ ↓ ←

向右:

当前方向:遍历方向:
↑ ← ↓ →
→ ↑ ← ↓
↓ → ↑ ←
← ↓ → ↑

似乎有那么点规律,规律要说起来并不能表达清楚,语文水平有限。也只能希望大家能看懂我的代码。(如果在纸上画画可能思路会清晰许多)

示例程序

Source Code

Problem: 3083		Code Lengthr: 3286B
Memory: 412K		Time: 0MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
    int x,y,step;
}q[40000];
char map[40][41];
int bfs(int sx,int sy,int w,int h)
{
    int v[40][40],f=0,top=0;
    memset(v,0,sizeof(v));
    q[top].x=sx;
    q[top].y=sy;
    q[top].step=1;
    v[sx][sy]=1;
    top++;
    while(f<top)
    {
        if(map[q[f].x][q[f].y]=='E')
        {
            return q[f].step;
        }
        if(q[f].y-1>=0&&v[q[f].x][q[f].y-1]==0&&map[q[f].x][q[f].y-1]!='#')
        {
            q[top].x=q[f].x;
            q[top].y=q[f].y-1;
            q[top].step=q[f].step+1;
            v[q[top].x][q[top].y]=1;
            top++;
        }
        if(q[f].x-1>=0&&v[q[f].x-1][q[f].y]==0&&map[q[f].x-1][q[f].y]!='#')
        {
            q[top].x=q[f].x-1;
            q[top].y=q[f].y;
            q[top].step=q[f].step+1;
            v[q[top].x][q[top].y]=1;
            top++;
        }
        if(q[f].x+1<h&&v[q[f].x+1][q[f].y]==0&&map[q[f].x+1][q[f].y]!='#')
        {
            q[top].x=q[f].x+1;
            q[top].y=q[f].y;
            q[top].step=q[f].step+1;
            v[q[top].x][q[top].y]=1;
            top++;
        }
        if(q[f].y+1<w&&v[q[f].x][q[f].y+1]==0&&map[q[f].x][q[f].y+1]!='#')
        {
            q[top].x=q[f].x;
            q[top].y=q[f].y+1;
            q[top].step=q[f].step+1;
            v[q[top].x][q[top].y]=1;
            top++;
        }
        f++;
    }
}
int f(int sx,int sy,int w,int h)
{
    int step=1,de=2;		//一开始就面向左
    while(map[sx][sy]!='E')
    {
        if(de==2&&sy-1>=0&&map[sx][sy-1]!='#')
        {
            sy--;
            de++;		//如果有这一步,那么下一次向左的方向就是de++
            step++;
        }
        else if(de==1&&sx-1>=0&&map[sx-1][sy]!='#')
        {
            sx--;
            de++;
            step++;
        }
        else if(de==0&&sy+1<w&&map[sx][sy+1]!='#')
        {
            sy++;
            de++;
            step++;
        }
        else if(de==3&&sx+1<h&&map[sx+1][sy]!='#')
        {
            sx++;
            de=0;		//这里de++变成了de=4,4对应0,因为都是向右
            step++;
        }
        else
        {
            de=(de+3)%4;	//遍历方向
        }
    }
    return step;
}
int f1(int sx,int sy,int w,int h)
{
    int step=0,de=0;		//一开始就面向右
    while(map[sx][sy]!='E')
    {
        if(de==0&&sy+1<w&&map[sx][sy+1]!='#')
        {
            sy++;
            de=3;		//如果有这一步,那么下一次向左的方向就是de--(这里de--变成了de=-1,-1对应3,因为都是向下)
            step++;
        }
        else if(de==1&&sx-1>=0&&map[sx-1][sy]!='#')
        {
            sx--;
            de--;
            step++;
        }
        else if(de==2&&sy-1>=0&&map[sx][sy-1]!='#')
        {
            sy--;
            de--;
            step++;
        }
        else if(de==3&&sx+1<h&&map[sx+1][sy]!='#')
        {
            sx++;
            de--;
            step++;
        }
        else
        {
            de=(de+1)%4;	//遍历方向
        }
    }
    return step+1;
}
int main()
{
    int t,w,h,sx,sy,tx,ty,i,i1,i2,a,b,c;
    scanf("%d",&t);
    for(i=1;t>=i;i++)
    {
        scanf("%d %d",&w,&h);
        for(i1=0;h>i1;i1++)
        {
            scanf("%s",map[i1]);
            for(i2=0;w>i2;i2++)
            {
                if(map[i1][i2]=='S')
                {
                    sx=i1;
                    sy=i2;
                }
            }
        }
        a=f(sx,sy,w,h);		//优先向左
        b=f1(sx,sy,w,h);	//优先向右
        c=bfs(sx,sy,w,h);	//最短路径(求最短路径最好还是BFS)
        printf("%d %d %d\n",a,b,c);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值