POJ - 3083 Children of the Candy Corn 中文题意+题解

 POJ - 3083
**

Children of the Candy Corn
==========================

**
Time Limit: 1000MS 		Memory Limit: 65536KB 		64bit IO Format: %I64d & %I64u

Submit Status

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



题意简述:给一个bool图,起点和终点,起点的方向给定(题上说起点一定在一个可以确定方向的角角里,即起点只有一个方向可以走),第一次按左上右下的顺序找起点到终点的路径长度,第二次按右上左下的顺序寻找,第三次求起点到终点的最短路,前两遍用dfs,第3遍用宽搜,注意第二遍dfs可以抄第一遍,改一下顺序即可,细节很多,详见代码


```
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<map>
#include<queue>
using namespace std;
struct node{int xx;int yy;int st;int pre;};
bool arr[50][50];
int gox[4]={-1,1,0,0};
int goy[4]={0,0,1,-1};
int a,b,c,d,n,m; int sx,sy,ex,ey;
int flag=0;
queue<node> q;
bool vis[50][50];
int dfs1(int xx,int yy,int dd,int step)
{
        int x=xx;
        int y=yy;
        if (flag==1)    return 0;
        if (ex==xx && ey==yy)
        {
                flag=1;
                cout<<step+1<<" ";
                return 0;
        }
        int tt;
        step++;
        if (dd==0)
        {
                if (arr[x+1][y]==0 && !(x+1==sx && y==sy)){tt=3;dfs1(x+1,y,tt,step);}
                else if (arr[x][y-1]==0 && !(x==sx && y-1==sy)) {tt=0;dfs1(x,y-1,tt,step);}
                else if (arr[x-1][y]==0 && !(x-1==sx && y==sy))    { tt=1; dfs1(x-1,y,tt,step);}
                else if (arr[x][y+1]==0 && !(x==sx && y+1==sy))   {  tt=2;dfs1(x,y+1,tt,step); }
        }
        else if (dd==1)
        {
                if (arr[x][y-1]==0 && !(x==sx && y-1==sy))      {tt=0;dfs1(x,y-1,tt,step);}
                else if (arr[x-1][y]==0 && !(x-1==sx && y==sy))      {tt=1;dfs1(x-1,y,tt,step);}
                else if (arr[x][y+1]==0 && !(x==sx && y+1==sy)  )     {tt=2;dfs1(x,y+1,tt,step);}
                else if (arr[x+1][y]==0 && !(x+1==sx && y==sy) )       {tt=3;dfs1(x+1,y,tt,step);}
        }
        else if (dd==2)
        {
                if (arr[x-1][y]==0 && !(x-1==sx && y==sy)   )  {tt=1;dfs1(x-1,y,tt,step);}
                else if (arr[x][y+1]==0 && !(x==sx && 1+y==sy)   )     {tt=2;dfs1(x,y+1,tt,step);}
                else if (arr[x+1][y]==0 && !(x+1==sx && y==sy)   )     {tt=3;dfs1(x+1,y,tt,step);}
                else if (arr[x][y-1]==0 && !(x==sx && y-1==sy)   )     {tt=0;dfs1(x,y-1,tt,step);}
        }

        else if (dd==3)
        {
                if (arr[x][y+1]==0 && !(x==sx && y+1==sy)  )   {tt=2;dfs1(x,y+1,tt,step);}
                else if (arr[x+1][y]==0 && !(x+1==sx && y==sy) )       {tt=3;dfs1(x+1,y,tt,step);}
                else if (arr[x][y-1]==0 && !(x==sx && y-1==sy)   )     {tt=0;dfs1(x,y-1,tt,step);}
                else if (arr[x-1][y]==0 && !(x-1==sx && y==sy)  )      {tt=1;dfs1(x-1,y,tt,step);}
        }
}
int dfs2(int xx,int yy,int dd,int step)
{

        int x=xx;
        int y=yy;
        if (flag==1)
                return 0;
        if (xx==ex && yy==ey)
        {
                flag=1;
                cout<<step+1<<" ";
                return 0;
        }
        int tt;
        step++;
        if (dd==0)
        {
                if (arr[x-1][y]==0 && !(x-1==sx && y==sy))    { tt=1; dfs2(x-1,y,tt,step);}
                else if (arr[x][y-1]==0 && !(x==sx && y-1==sy)) {tt=0;dfs2(x,y-1,tt,step);}
                else if (arr[x+1][y]==0 && !(x+1==sx && y==sy)){tt=3;dfs2(x+1,y,tt,step);}
                else if (arr[x][y+1]==0 && !(x==sx && y+1==sy))   {  tt=2;dfs2(x,y+1,tt,step); }

        }
        else if (dd==1)
        {
                if (arr[x][y+1]==0 && !(x==sx && y+1==sy) )     {tt=2;dfs2(x,y+1,tt,step);}
                else if (arr[x-1][y]==0 && !(x-1==sx && y==sy))      {tt=1;dfs2(x-1,y,tt,step);}
                else if (arr[x][y-1]==0 && !(x==sx && y-1==sy))      {tt=0;dfs2(x,y-1,tt,step);}
                else if (arr[x+1][y]==0 && !(x+1==sx && y==sy) )       {tt=3;dfs2(x+1,y,tt,step);}
        }
        else if (dd==2)
        {
                if (arr[x+1][y]==0 && !(x+1==sx && y==sy)   )     {tt=3;dfs2(x+1,y,tt,step);}
                else if (arr[x][y+1]==0 && !(x==sx && 1+y==sy)   )     {tt=2;dfs2(x,y+1,tt,step);}
                else if (arr[x-1][y]==0 && !(x-1==sx && y==sy)   )  {tt=1;dfs2(x-1,y,tt,step);}
                else if (arr[x][y-1]==0 && !(x==sx && y-1==sy)   )     {tt=0;dfs2(x,y-1,tt,step);}
        }

        else if (dd==3)
        {
                if (arr[x][y-1]==0 && !(x==sx && y-1==sy)   )     {tt=0;dfs2(x,y-1,tt,step);}
                else if (arr[x+1][y]==0 && !(x+1==sx && y==sy) )       {tt=3;dfs2(x+1,y,tt,step);}
                else  if (arr[x][y+1]==0 && !(x==sx && y+1==sy)  )   {tt=2;dfs2(x,y+1,tt,step);}
                else if (arr[x-1][y]==0 && !(x-1==sx && y==sy)  )      {tt=1;dfs2(x-1,y,tt,step);}
        }




}


int main()
{
        int t;
        scanf("%d",&t);
        while(t--)
        {

                char cc;
                scanf("%d%d",&m,&n);
                for (int i=0;i<=m+1;i++)
                        for (int j=0;j<=n+1;j++)
                                arr[i][j]=1;
                for (int i=1;i<=n;i++)
                        for (int j=1;j<=m;j++)
                        {
                                cin>>cc;
                                if (cc=='#')
                                        arr[i][j]=1;
                                else if (cc=='.')
                                        arr[i][j]=0;
                               else if (cc=='S')
                                {
                                        sx=i;
                                        sy=j;
                                        arr[i][j]=0;
                                }
                                else
                                {
                                        ex=i;
                                        ey=j;
                                        arr[i][j]=0;
                                }
                        }
                int ans=0;
                //jud the direction
                int x=sx;
                int y=sy;
                if (arr[x][y-1]==0)
                        d=0;
                else if (arr[x][y+1]==0)
                        d=2;
                else if (arr[x+1][y]==0)
                        d=3;
                else if (arr[x-1][y]==0)
                        d=1;
                flag=0;
                dfs1(sx,sy,d,0);
                flag=0;
                dfs2(sx,sy,d,0);
        while(!q.empty())
                q.pop();
        node tt;
        tt.xx=sx;
        tt.yy=sy;
        tt.st=1;
        tt.pre=0;
        q.push(tt);
        int step=0;
        node now;
        node nex;
        int f=0;
        memset(vis,0,sizeof(vis));
        while(!q.empty())
        {
                now=q.front();
                q.pop();
                for (int i=0;i<=3;i++)
                {
                        nex.xx=now.xx+gox[i];
                        nex.yy=now.yy+goy[i];
                        nex.st=now.st+1;
                        if (arr[nex.xx][nex.yy]==1 || vis[nex.xx][nex.yy]==1)
                                continue;
                        if (nex.xx==ex && nex.yy==ey)
                                {
                                f=1;
                                cout<<nex.st<<endl;
                                break;
                                }
                        if (nex.xx>=1 && nex.xx<=n && nex.yy>=1 && nex.yy<=m)
                        {
                                q.push(nex);
                                vis[nex.xx][nex.yy]=1;
                        }
                }
        if (f==1)       break;
        }

        }
}

```

猜你喜欢

转载自blog.csdn.net/williamcode/article/details/50947927