POJ 3083 Children of the Candy Corn

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

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

对于这个题,昨天晚上静下心来好好想想发现对于求最短的距离bfs就可以,这是很简单的,但是怎么才能绕墙呢,看了看discuss大部分都是用dfs,我想了想不太会,然后就在那画图,发现这个题其实不用dfs就可以,绕墙到目的地,只会有一条路径,只需要记录刚走过的两个点,判断走的方向 ,对于每种走的方向考虑的情况都是一样的,以沿着左边的墙向上走为例,主要有以下几种情况
1 左边不是墙了,这个时候就需要向左走一个。
2 左边是墙,这个时候需要向前走,这个时候两种情况 (1):前方不是墙直接向前走就可以了
                                                                                        (2):前方是墙,这个时候就要右拐,也分两种情况:
                                                                                                                  《1》 右拐不是墙,直接走可以了。
                                                                                                                   《2》 右拐是墙,这个时候就要退回去了
靠着左边的墙走,会有四种方向,这四种方向考虑的情况都是上面那样,对于向左拐还是右拐,可以开数组,存储一下方向,到时候直接用就行。
这段代码是我刚写出的代码,发现向左和向右处理的都差不多,就是换了换数组,就做了简化处理。两段代码都贴一下吧
#include <iostream>
#include <string.h>
using namespace std;
int vex1[]={0,0,-1,1},vey1[]={-1,1,0,0};
int vex2[]={0,0,1,-1},vey2[]={1,-1,0,0};
int vectorx[]={-1,1,0,0},vectory[]={0,0,1,-1};
int status[50][50],a[50][50],res1,res2,res3,pos_stax,pos_stay,pos_endx,pos_endy;
int n,m;
int main()
{
    void deal_left();
    void deal_right();
    void deal();
    int i,j,s,t;
    char c;
    cin>>t;
    while(t--)
    {
        cin>>m>>n;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                cin>>c;
                if(c=='#')
                {
                    a[i][j]=0;
                }else if(c=='S')
                {
                    a[i][j]=0;
                    pos_stax=i; pos_stay=j;
                }else if(c=='E')
                {
                    a[i][j]=1;
                    pos_endx=i; pos_endy=j;
                }else if(c=='.')
                {
                    a[i][j]=1;
                }
            }
        }
        deal_left();
        deal_right();
        deal();
        cout<<res1<<" "<<res2<<" "<<res3<<endl;
    }
    return 0;
}
void deal_left()
{
    int i,j,x1,y1,x2,y2,x3,y3,s,x4,y4,t,temp1,temp2;
    s=2; x1=pos_stax; y1=pos_stay;
    for(i=0;i<=3;i++)
    {
        x2=x1+vex1[i]; y2=y1+vey1[i];
        if(x2>=1&&x2<=n&&y2>=1&&y2<=m&&a[x2][y2])
        {
            break;
        }
    }
    while(1)
    {
        x4=x2-x1; y4=y2-y1;
        if(x4<0&&y4==0)
        {
            t=0;
        }else if(x4>0&&y4==0)
        {
            t=1;
        }else if(x4==0&&y4>0)
        {
            t=2;
        }else if(x4==0&&y4<0)
        {
            t=3;
        }
        if(a[x2+vex1[t]][y2+vey1[t]])
        {
            x1=x2; y1=y2;
            x2=x2+vex1[t]; y2=y2+vey1[t];
            s+=1;
        }else
        {
            x3=x2+vectorx[t]; y3=y2+vectory[t];
            if(a[x3][y3])
            {
                x1=x2; y1=y2;
                x2=x3; y2=y3;
            }else
            {
                x3=x2+vex2[t]; y3=y2+vey2[t];
                if(a[x3][y3])
                {
                    x1=x2; y1=y2;
                    x2=x3; y2=y3;
                }else
                {
                    temp1=x1; temp2=y1;
                    x1=x2;    y1=y2;
                    x2=temp1; y2=temp2;
                }
            }
            s++;
        }
        if(x2==pos_endx&&y2==pos_endy)
        {
            res1=s;
            break;
        }
    }
}
void deal_right()
{
    int i,j,x1,y1,x2,y2,x3,y3,s,x4,y4,t,temp1,temp2;
    int sum=0;
    s=2; x1=pos_stax; y1=pos_stay;
    for(i=0;i<=3;i++)
    {
        x2=x1+vex1[i]; y2=y1+vey1[i];
        if(x2>=1&&x2<=n&&y2>=1&&y2<=m&&a[x2][y2])
        {
            break;
        }
    }
    while(1)
    {
        x4=x2-x1; y4=y2-y1;
        if(x4<0&&y4==0)
        {
            t=0;
        }else if(x4>0&&y4==0)
        {
            t=1;
        }else if(x4==0&&y4>0)
        {
            t=2;
        }else if(x4==0&&y4<0)
        {
            t=3;
        }
        if(a[x2+vex2[t]][y2+vey2[t]])
        {
            x1=x2; y1=y2;
            x2=x2+vex2[t]; y2=y2+vey2[t];
            s+=1;
        }else
        {
            x3=x2+vectorx[t]; y3=y2+vectory[t];
            if(a[x3][y3])
            {
                x1=x2; y1=y2;
                x2=x3; y2=y3;
            }else
            {
                x3=x2+vex1[t]; y3=y2+vey1[t];
                if(a[x3][y3])
                {
                    x1=x2; y1=y2;
                    x2=x3; y2=y3;
                }else
                {
                    temp1=x1; temp2=y1;
                    x1=x2;    y1=y2;
                    x2=temp1; y2=temp2;
                }
            }
            s++;
        }
        if(x2==pos_endx&&y2==pos_endy)
        {
            res2=s;
            break;
        }
    }
}
void deal()
{
    int i,j,base,top,x,y,xend,yend;
    int queue[100000][2];
    int sum[50][50];
    memset(status,0,sizeof(status));
    memset(sum,0,sizeof(sum));
    base=top=0;
    queue[top][0]=pos_stax; queue[top++][1]=pos_stay;
    sum[pos_stax][pos_stay]=1; status[pos_stax][pos_stay]=1;
    while(base<top)
    {
        x=queue[base][0]; y=queue[base++][1];
        for(i=0;i<=3;i++)
        {
            xend=x+vex1[i]; yend=y+vey1[i];
            if(xend>=1&&xend<=n&¥d>=1&¥d<=m&&!status[xend][yend]&&a[xend][yend])
            {
                sum[xend][yend]=sum[x][y]+1;
                status[xend][yend]=1;
                queue[top][0]=xend; queue[top++][1]=yend;
                if(xend==pos_endx&¥d==pos_endy)
                {
                    break;
                }
            }
        }
        if(i!=4)
        {
            break;
        }
    }
    res3=sum[pos_endx][pos_endy];
}

左右方向放在一个函数中处理。
#include <iostream>
#include <string>
using namespace std;
int bx1[]={0,0,-1,1},by1[]={-1,1,0,0};
int bx2[]={0,0,1,-1},by2[]={1,-1,0,0};
int vectorx[]={-1,1,0,0},vectory[]={0,0,1,-1};
int status[50][50],a[50][50],res1,res2,res3,pos_stax,pos_stay,pos_endx,pos_endy;
int n,m;
int main()
{
    void deal_left_Or_right(int vex1[5],int vey1[5],int vex2[5],int vey2[5],int &k);
    void deal();
    int i,j,s,t;
    char c;
    cin>>t;
    while(t--)
    {
        cin>>m>>n;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                cin>>c;
                if(c=='#')
                {
                    a[i][j]=0;
                }else if(c=='S')
                {
                    a[i][j]=0;
                    pos_stax=i; pos_stay=j;
                }else if(c=='E')
                {
                    a[i][j]=1;
                    pos_endx=i; pos_endy=j;
                }else if(c=='.')
                {
                    a[i][j]=1;
                }
            }
        }
        deal_left_Or_right(bx1,by1,bx2,by2,res1);
        deal_left_Or_right(bx2,by2,bx1,by1,res2);
        deal();
        cout<<res1<<" "<<res2<<" "<<res3<<endl;
    }
    return 0;
}
void deal_left_Or_right(int vex1[5],int vey1[5],int vex2[5],int vey2[5],int &k)
{
    int i,j,x1,y1,x2,y2,x3,y3,s,x4,y4,t,temp1,temp2;
    s=2; x1=pos_stax; y1=pos_stay;
    for(i=0;i<=3;i++)
    {
        x2=x1+vex1[i]; y2=y1+vey1[i];
        if(x2>=1&&x2<=n&&y2>=1&&y2<=m&&a[x2][y2])
        {
            break;
        }
    }
    while(1)
    {
        x4=x2-x1; y4=y2-y1;
        if(x4<0&&y4==0)
        {
            t=0;
        }else if(x4>0&&y4==0)
        {
            t=1;
        }else if(x4==0&&y4>0)
        {
            t=2;
        }else if(x4==0&&y4<0)
        {
            t=3;
        }
        if(a[x2+vex1[t]][y2+vey1[t]])
        {
            x1=x2; y1=y2;
            x2=x2+vex1[t]; y2=y2+vey1[t];
            s+=1;
        }else
        {
            x3=x2+vectorx[t]; y3=y2+vectory[t];
            if(a[x3][y3])
            {
                x1=x2; y1=y2;
                x2=x3; y2=y3;
            }else
            {
                x3=x2+vex2[t]; y3=y2+vey2[t];
                if(a[x3][y3])
                {
                    x1=x2; y1=y2;
                    x2=x3; y2=y3;
                }else
                {
                    temp1=x1; temp2=y1;
                    x1=x2;    y1=y2;
                    x2=temp1; y2=temp2;
                }
            }
            s++;
        }
        if(x2==pos_endx&&y2==pos_endy)
        {
            k=s;
            break;
        }
    }
}
void deal()
{
    int i,j,base,top,x,y,xend,yend;
    int queue[100000][2];
    int sum[50][50];
    memset(status,0,sizeof(status));
    memset(sum,0,sizeof(sum));
    base=top=0;
    queue[top][0]=pos_stax; queue[top++][1]=pos_stay;
    sum[pos_stax][pos_stay]=1; status[pos_stax][pos_stay]=1;
    while(base<top)
    {
        x=queue[base][0]; y=queue[base++][1];
        for(i=0;i<=3;i++)
        {
            xend=x+bx1[i]; yend=y+by1[i];
            if(xend>=1&&xend<=n&¥d>=1&¥d<=m&&!status[xend][yend]&&a[xend][yend])
            {
                sum[xend][yend]=sum[x][y]+1;
                status[xend][yend]=1;
                queue[top][0]=xend; queue[top++][1]=yend;
                if(xend==pos_endx&¥d==pos_endy)
                {
                    break;
                }
            }
        }
        if(i!=4)
        {
            break;
        }
    }
    res3=sum[pos_endx][pos_endy];
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值