Children of the Candy Corn

来源:互联网 发布:手机淘宝2015旧版本5.5 编辑:程序博客网 时间:2024/05/16 15:12

Children of the Candy Corn

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 65   Accepted Submission(s) : 24
Problem 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'. <br> <br>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 ('#'). <br> <br>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
28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########
 

Sample Output
37 5 517 17 9
 
    这个题难在题意上,题意说是给一个地图,“ . ”是路,“#”是墙,从S走到E,有三种走法,各求最少经过多少个点,要算上S、E,一个点如果走过一次还可以在走一次,要算上次数的。但是关键的走法:1.向左走  2.向右走  3.随便 。3都可以想到,最短路径,广搜做。那1、2是个什么走法呢?我研究好久才研究出来,拿走法1 向左走来说,你从上一个点来到现在所处的点,肯定是上下左右(就按这个地图来说)某个方向过来的,那么就以你现在面朝的方向为前方,优先向左走(哪怕右边就是终点也不能向右);如果左边没路,再优先向前走;前边没路,优先向右;右边还没路,向后走,后边不可能没路的,你刚过来。总结起来,你在任何点,要走的话方向顺序应该是 “左前右后”。 同理,走法2,顺序就是“右前左后”。别问我怎么知道的,比着地图试出来的规律。

    知道了这个走法,能做了,但是想一想你面朝不同方向的时候前后左右都不一样啊,这可怎么整?我感觉这是有规律的,但是我没找,没有必要吧,用个笨方法(如下),1、2、3、4 的表示一下,在判断一下,也就多个几十行代码的事,而且大半都是复制粘贴改改数据,但是很重要的一点:一点都不能错!!!


代码如下:

#include<iostream>#include<cstring>#include<stdio.h>using namespace std;char p[50][50];int vis[50][50];int n,m,si,sj;int dxy[5][2]={{0,0},{-1,0},{0,-1},{1,0},{0,1}};                    //上     左     下int tl,tr,tt;int searchl(int x,int y,int pre,int t){    tl++;    int xx,yy;    int d[5];//先确定方向  左前右后 顺序    if(pre==1)    {        d[1]=2;   d[2]=1;   d[3]=4;   d[4]=3;    }    else if(pre==2)    {        d[1]=3;   d[2]=2;   d[3]=1;   d[4]=4;    }    else if(pre==3)    {        d[1]=4;   d[2]=3;   d[3]=2;   d[4]=1;    }    else if(pre==4)    {        d[1]=1;   d[2]=4;   d[3]=3;   d[4]=2;    }    for(int i=1;i<=4;i++)    {        xx=x+dxy[d[i]][0];        yy=y+dxy[d[i]][1];        if(p[xx][yy]=='E')        {            return t+1;        }        if(p[xx][yy]=='.')//如果有路   走,且 break        {            searchl(xx,yy,d[i],t+1);            break;        }    }}int searchr(int x,int y,int pre,int t){    tr++;    int xx,yy;    int d[5];//先确定方向  右前左后 顺序    if(pre==1)    {        d[1]=4;   d[2]=1;   d[3]=2;   d[4]=3;    }    else if(pre==2)    {        d[1]=1;   d[2]=2;   d[3]=3;   d[4]=4;    }    else if(pre==3)    {        d[1]=2;   d[2]=3;   d[3]=4;   d[4]=1;    }    else if(pre==4)    {        d[1]=3;   d[2]=4;   d[3]=1;   d[4]=2;    }    for(int i=1;i<=4;i++)    {        xx=x+dxy[d[i]][0];        yy=y+dxy[d[i]][1];        if(p[xx][yy]=='E')        {            return t+1;        }        if(p[xx][yy]=='.')//如果有路   走,且 break        {            searchr(xx,yy,d[i],t+1);            break;        }    }}struct node{    int x,y,t;};int ok1(int x,int y){    if(x>=1&&x<=n&&y>=1&&y<=m)        return 1;    return 0;}int ok2(int x,int y){    if(x>1&&x<n&&y>1&&y<m)        return 1;    return 0;}int ioan(int x,int y){    int head=0,tail=1;    node q[10000],a,b;    q[1].t=1;    q[1].x=x;    q[1].y=y;    do    {        head++;        a=q[head];        for(int i=1;i<=4;i++)        {            b.x=a.x+dxy[i][0];            b.y=a.y+dxy[i][1];            b.t=a.t+1;            if(ok1(b.x,b.y) && p[b.x][b.y]=='E')            {                return b.t;            }            if(ok2(b.x,b.y))            {                if(p[b.x][b.y]=='.' && vis[b.x][b.y]==0)                {                    vis[b.x][b.y]=1;                    tail++;                    q[tail]=b;                }            }        }    }while(head<tail);}int main(){    //freopen("D:\\aaa.txt","r",stdin);    int t;    cin>>t;    while(t--)    {        cin>>m>>n;        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                cin>>p[i][j];                if(p[i][j]=='S')                {                    si=i;                    sj=j;                }            }        }        int d=0;//从起点开始的方向        if(si==1)            d=3;        else if(si==n)            d=1;        else if(sj==1)            d=4;        else if(sj==m)            d=2;        int x=si+dxy[d][0];        int y=sj+dxy[d][1];        tl=2;        tr=2;        int left=searchl(x,y,d,2);        int right=searchr(x,y,d,2);        int mid=ioan(si,sj);        cout<<left<<" "<<right<<" "<<mid<<endl;    }    return 0;}


原创粉丝点击