首页 > 代码库 > POJ:3083 Children of the Candy Corn(bfs+dfs)

POJ:3083 Children of the Candy Corn(bfs+dfs)

http://poj.org/problem?id=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.

Sample Input

28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########

Sample Output

37 5 517 17 9

题意:有一个迷宫,#代表墙,. 代表能走,S是起点,E是终点,M为宽,列数N为高;

先输出左转优先时,从S到E的步数(DFS)

再输出右转优先时,从S到E的步数(DFS)

最后输出S到E的最短步数(BFS)

题目解析:

之前一直没读懂题意,之后读懂了,却一直不会写,看了一下大神写的题解(汗颜啊),这题就是代码有点长,

但数据水,0MS,282kb。一遍就A了。

左优先时,

依左上右下的顺时针方向走。根据上一个来的方向判断当前坐标开始走的方向

右优先时,
依右上左下的逆时针方向走。根据上一个来的方向判断当前坐标开始走的方向,可以走四个方向,最后才走当前位置的对面方向。
我是假设向下是0,向上是1,向右是2,向左是3,
左优先时,当前位置如果是向左,那么下一步依次走下,左,上,右。

(*)左边、右边优先搜索都不是找最短路,因此走过的路可以再走,无需标记走过的格

分析:

最短路好办,关键是沿着墙走不太好想。

但只要弄懂如何转,这题就容易了。

单就沿着左走看一下:

当前方向    检索顺序

↑ :   ← ↑ → ↓

→ :   ↑ → ↓ ←

↓ :   → ↓ ← ↑

← :   ↓ ← ↑ →

如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。

在DFS时,加一个参数,用来保存当前的方向。

#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <queue>using namespace std;int n,m;char map[101][101];int v[101][101];struct node{    int ans,x,y;};struct node t,f;int tx,ty,maxx,maxy;int fx[]= {1,-1,0,0};int fy[]= {0,0,1,-1};void bfs(){    queue<node>q;    memset(v,0,sizeof(v));    t.x=tx;    t.y=ty;    t.ans=1;    q.push(t);    v[t.x][t.y]=1;    while(!q.empty())    {        t=q.front();        q.pop();        if(map[t.x][t.y]==E)        {            printf("%d\n",t.ans);            return ;        }        for(int i=0; i<4; i++)        {            f.x=t.x+fx[i];            f.y=t.y+fy[i];            if(f.x>=0&&f.x<n&&f.y>=0&&f.y<m&&v[f.x][f.y]==0&&map[f.x][f.y]!=#)            {                f.ans=t.ans+1;                v[f.x][f.y]=1;                q.push(f);            }        }    }}void dfs1(int xx,int yy,int d,int ans){    if(map[xx][yy]==E)    {        maxx=ans;        return ;    }    if(d==0)//0是下1是上2是右3是左    {        if(xx>=0&&xx<n&&yy+1>=0&&yy+1<m&&map[xx][yy+1]!=#)        {            dfs1(xx,yy+1,2,ans+1);        }        else if(xx+1>=0&&xx+1<n&&yy>=0&&yy<m&&map[xx+1][yy]!=#)        {            dfs1(xx+1,yy,0,ans+1);        }        else if(xx>=0&&xx<n&&yy-1>=0&&yy-1<m&&map[xx][yy-1]!=#)        {            dfs1(xx,yy-1,3,ans+1);        }        else if(xx-1>=0&&xx-1<n&&yy>=0&&yy<m&&map[xx-1][yy]!=#)        {            dfs1(xx-1,yy,1,ans+1);        }    }    else if(d==1)    {        if(xx>=0&&xx<n&&yy-1>=0&&yy-1<m&&map[xx][yy-1]!=#)        {            dfs1(xx,yy-1,3,ans+1);        }        else if(xx-1>=0&&xx-1<n&&yy>=0&&yy<m&&map[xx-1][yy]!=#)        {            dfs1(xx-1,yy,1,ans+1);        }        else if(xx>=0&&xx<n&&yy+1>=0&&yy+1<m&&map[xx][yy+1]!=#)        {            dfs1(xx,yy+1,2,ans+1);        }        else if(xx+1>=0&&xx+1<n&&yy>=0&&yy<m&&map[xx+1][yy]!=#)        {            dfs1(xx+1,yy,0,ans+1);        }    }    else if(d==2)    {        if(xx-1>=0&&xx-1<n&&yy>=0&&yy<m&&map[xx-1][yy]!=#)        {            dfs1(xx-1,yy,1,ans+1);        }        else if(xx>=0&&xx<n&&yy+1>=0&&yy+1<m&&map[xx][yy+1]!=#)        {            dfs1(xx,yy+1,2,ans+1);        }        else  if(xx+1>=0&&xx+1<n&&yy>=0&&yy<m&&map[xx+1][yy]!=#)        {            dfs1(xx+1,yy,0,ans+1);        }        else if(xx>=0&&xx<n&&yy-1>=0&&yy-1<m&&map[xx][yy-1]!=#)        {            dfs1(xx,yy-1,3,ans+1);        }    }    else if(d==3)    {        if(xx+1>=0&&xx+1<n&&yy>=0&&yy<m&&map[xx+1][yy]!=#)        {            dfs1(xx+1,yy,0,ans+1);        }        else if(xx>=0&&xx<n&&yy-1>=0&&yy-1<m&&map[xx][yy-1]!=#)        {            dfs1(xx,yy-1,3,ans+1);        }        else if(xx-1>=0&&xx-1<n&&yy>=0&&yy<m&&map[xx-1][yy]!=#)        {            dfs1(xx-1,yy,1,ans+1);        }        else if(xx>=0&&xx<n&&yy+1>=0&&yy+1<m&&map[xx][yy+1]!=#)        {            dfs1(xx,yy+1,2,ans+1);        }    }    return ;}void dfs2(int xx,int yy,int d,int ans){    if(map[xx][yy]==E)    {        maxy=ans;        return ;    }    if(d==0)//0是下1是上2是右3是左    {        if(xx>=0&&xx<n&&yy-1>=0&&yy-1<m&&map[xx][yy-1]!=#)        {            dfs2(xx,yy-1,3,ans+1);        }        else if(xx+1>=0&&xx+1<n&&yy>=0&&yy<m&&map[xx+1][yy]!=#)        {            dfs2(xx+1,yy,0,ans+1);        }        else if(xx>=0&&xx<n&&yy+1>=0&&yy+1<m&&map[xx][yy+1]!=#)        {            dfs2(xx,yy+1,2,ans+1);        }        else if(xx-1>=0&&xx-1<n&&yy>=0&&yy<m&&map[xx-1][yy]!=#)        {            dfs2(xx-1,yy,1,ans+1);        }    }    else if(d==1)    {        if(xx>=0&&xx<n&&yy+1>=0&&yy+1<m&&map[xx][yy+1]!=#)        {            dfs2(xx,yy+1,2,ans+1);        }        else if(xx-1>=0&&xx-1<n&&yy>=0&&yy<m&&map[xx-1][yy]!=#)        {            dfs2(xx-1,yy,1,ans+1);        }        else if(xx>=0&&xx<n&&yy-1>=0&&yy-1<m&&map[xx][yy-1]!=#)        {            dfs2(xx,yy-1,3,ans+1);        }        else if(xx+1>=0&&xx+1<n&&yy>=0&&yy<m&&map[xx+1][yy]!=#)        {            dfs2(xx+1,yy,0,ans+1);        }    }    else if(d==2)    {        if(xx+1>=0&&xx+1<n&&yy>=0&&yy<m&&map[xx+1][yy]!=#)        {            dfs2(xx+1,yy,0,ans+1);        }        else if(xx>=0&&xx<n&&yy+1>=0&&yy+1<m&&map[xx][yy+1]!=#)        {            dfs2(xx,yy+1,2,ans+1);        }        else  if(xx-1>=0&&xx-1<n&&yy>=0&&yy<m&&map[xx-1][yy]!=#)        {            dfs2(xx-1,yy,1,ans+1);        }        else if(xx>=0&&xx<n&&yy-1>=0&&yy-1<m&&map[xx][yy-1]!=#)        {            dfs2(xx,yy-1,3,ans+1);        }    }    else if(d==3)    {        if(xx-1>=0&&xx-1<n&&yy>=0&&yy<m&&map[xx-1][yy]!=#)        {            dfs2(xx-1,yy,1,ans+1);        }        else if(xx>=0&&xx<n&&yy-1>=0&&yy-1<m&&map[xx][yy-1]!=#)        {            dfs2(xx,yy-1,3,ans+1);        }        else if(xx+1>=0&&xx+1<n&&yy>=0&&yy<m&&map[xx+1][yy]!=#)        {            dfs2(xx+1,yy,0,ans+1);        }        else if(xx>=0&&xx<n&&yy+1>=0&&yy+1<m&&map[xx][yy+1]!=#)        {            dfs2(xx,yy+1,2,ans+1);        }    }    return ;}int main(){    int T,j;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&m,&n);        for(int i=0; i<n; i++)        {            scanf("%*c%s",map[i]);        }        for(int i=0; i<n; i++)        {            for(j=0; j<m; j++)            {                if(map[i][j]==S)                {                    tx=i;                    ty=j;                    if(i==0)//开始方向向下                    {                        dfs1(tx+1,ty,0,2);                        dfs2(tx+1,ty,0,2);                    }                    else if(i==n-1)//开始方向向上                    {                        dfs1(tx-1,ty,1,2);                        dfs2(tx-1,ty,1,2);                    }                    else if(j==0)//开始方向向右                    {                        dfs1(tx,ty+1,2,2);                        dfs2(tx,ty+1,2,2);                    }                    else if(j==m-1)//开始方向向左                    {                        dfs1(tx,ty-1,3,2);                        dfs2(tx,ty-1,3,2);                    }                    break;                }            }            if(j!=m) break;        }        printf("%d %d ",maxx,maxy);        bfs();    }    return 0;}

 

POJ:3083 Children of the Candy Corn(bfs+dfs)