poj Children of the Candy Corn(BFS+DFS)

来源:互联网 发布:mac 无法开机无法充电 编辑:程序博客网 时间:2024/05/20 15:57
Children of the Candy Corn

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
题意:
第一种:左手优先,只要左手边有路,就一直往左走,若左手边无路,则按顺时针依次寻找出路,最后输出到终点所走的步数;
第二种:右手优先,同上;
第三种:最短路。


先看大牛的算法(超厉害):
——————————
**注意三个问题:
* 1.使用DFS计算左转优先和右转优先的路径,使用BFS计算最短路径
* 2.这里的DFS不需要标记,因为按照方向顺时针(或逆时针)前进时,除非无路可走才会返回,所以不会因为没有标记而出现问题,不过它的前进方向是相对,最初的方向由起点S确定,而下一步的方向则由前一步的走向决定 
以顺时针,左手优先为例,即(相对于当前方向,左转90°后的方向为第一优先,依次右转90°找可能的通路) 
如何左转90度?0,1,2,3代表四个方向,那么可以用(d+3)%4,就可以左转90度到优先位置,当然往右转的过程中,还是会超过0,1,2,3,到了外面,所以在运算时,使用d%4就可以了。 
* 3.起点到终点顺时针的话,它的逆时针也可以使用顺时针所使用的DFS,只要把起点和终点倒过来就可以了,也就是DFS可以共用。

代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int vis[44][44];char s[44][44];int n,m,cont,ans,flag;//cont记录步数,flag判断是否已找到结果,ans为最短的步数int dir[][2]={{0,-1},{-1,0},{0,1},{1,0}};//顺时针struct node{    int x,y,step;};int judge(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&s[x][y]!='#')        return 1;    return 0;}void dfs(int x1,int y1,int x2,int y2,int d){    if(x1==x2&&y1==y2)    {        flag=1;        return ;    }    d=(d+3)%4;//在上一个方向的基础上,左转90度到优先位置    for(int i=d;i<d+4;i++)    {        int xx=x1+dir[i%4][0],yy=y1+dir[i%4][1];        if(xx>=0&&xx<n&&yy>=0&&yy<m&&s[xx][yy]!='#')        {            cont++;            dfs(xx,yy,x2,y2,i);            if(flag)                return ;        }    }}void bfs(int x1,int y1){    memset(vis,0,sizeof(vis));    node now,next;    queue<node>q;    now.x=x1,now.y=y1,now.step=1;    vis[x1][y1]=1;    q.push(now);    while(!q.empty())    {        now=q.front();        if(s[now.x][now.y]=='E')        {            printf("%d\n",now.step);            return ;        }        for(int i=0;i<4;i++)        {            int x=now.x+dir[i][0],y=now.y+dir[i][1];            if(judge(x,y))            {                vis[x][y]=1;                next.x=x,next.y=y,next.step=now.step+1;                q.push(next);            }        }        q.pop();    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&m,&n);        int i,j,x1,y1,x2,y2;        for(i=0;i<n;i++)        {            scanf("%s",s[i]);            for(j=0;j<m;j++)            {                if(s[i][j]=='S')                    x1=i,y1=j;                else if(s[i][j]=='E')                    x2=i,y2=j;            }        }        cont=1,flag=0;        dfs(x1,y1,x2,y2,0);        printf("%d ",cont);        cont=1,flag=0;        dfs(x2,y2,x1,y1,0);        printf("%d ",cont);        bfs(x1,y1);    }    return 0;}

—————————————————————————————————————————————————————————————————————————————
ps:起初我的理解是,设立4个方向数组,分别表示从前一个点走到这一个点的走向的优先方向,再设4个方向为1,2,3,4,放在DFS中代表这一步是从那个方向过来的,这样解决了方向的优先级问题,但是第1步该怎样走就想不通了,所以无奈只能看大牛的博客了0.0

哈哈,我让第一步左手优先就向左走,右手优先就向右走,然后果断A粗来了(这次我用了8个方向数组,其实4个也可以,逆着就是逆序了)生气
代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int dir_1[][2]= {{0,-1},{-1,0},{0,1},{1,0}};//左手优先,顺时针int dir_2[][2]= {{-1,0},{0,1},{1,0},{0,-1}};int dir_3[][2]= {{0,1},{1,0},{0,-1},{-1,0}};int dir_4[][2]= {{1,0},{0,-1},{-1,0},{0,1}};int dir_5[][2]= {{0,-1},{1,0},{0,1},{-1,0}};//右手优先,逆时针int dir_6[][2]= {{1,0},{0,1},{-1,0},{0,-1}};int dir_7[][2]= {{0,1},{-1,0},{0,-1},{1,0}};int dir_8[][2]= {{-1,0},{0,-1},{1,0},{0,1}};char s[44][44];int vis[44][44];int n,m,left_ans,right_ans,ans,flag;struct node{    int x,y,step;} ;int judge(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&s[x][y]!='#')        return 1;    return 0;}int bfs(int x1,int y1)//BFS求最短路{    memset(vis,0,sizeof(vis));    node now,next;    queue<node>q;    now.x=x1,now.y=y1,now.step=1;    vis[x1][y1]=1;    q.push(now);    while(!q.empty())    {        now=q.front();        if(s[now.x][now.y]=='E')            return now.step;        for(int i=0; i<4; i++)        {            int x=now.x+dir_1[i][0],y=now.y+dir_1[i][1];            if(judge(x,y))            {                vis[x][y]=1;                next.x=x,next.y=y,next.step=now.step+1;                q.push(next);            }        }        q.pop();    }}int judge_dir(int x,int y)//判断走向{    if(x==0&&y==1)        return 2;    if(x==0&&y==-1)        return 0;    if(x==1&&y==0)        return 3;    if(x==-1&&y==0)        return 1;}void left_dfs(int x,int y,int step,int dir)//左手优先搜索{    if(s[x][y]=='E')    {        left_ans=step;        flag=1;        return ;    }    if(dir==0)    {        for(int i=0; i<4; i++)        {            int xx=x+dir_4[i][0],yy=y+dir_4[i][1];            if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]!='#')            {                int k=judge_dir(dir_4[i][0],dir_4[i][1]);                left_dfs(xx,yy,step+1,k);            }            if(flag)                return ;        }    }    else if(dir==2)    {        for(int i=0; i<4; i++)        {            int xx=x+dir_2[i][0],yy=y+dir_2[i][1];            if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]!='#')            {                int k=judge_dir(dir_2[i][0],dir_2[i][1]);                left_dfs(xx,yy,step+1,k);            }            if(flag)                return ;        }    }    else if(dir==3)    {        for(int i=0; i<4; i++)        {            int xx=x+dir_3[i][0],yy=y+dir_3[i][1];            if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]!='#')            {                int k=judge_dir(dir_3[i][0],dir_3[i][1]);                left_dfs(xx,yy,step+1,k);            }            if(flag)                return ;        }    }    else if(dir==1)    {        for(int i=0; i<4; i++)        {            int xx=x+dir_1[i][0],yy=y+dir_1[i][1];            if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]!='#')            {                int k=judge_dir(dir_1[i][0],dir_1[i][1]);                left_dfs(xx,yy,step+1,k);            }            if(flag)                return ;        }    }}void right_dfs(int x,int y,int step,int dir)//右手优先搜索{    if(s[x][y]=='E')    {        right_ans=step;        flag=1;    }    if(flag)        return ;    if(dir==1)    {        for(int i=0; i<4; i++)        {            int xx=x+dir_7[i][0],yy=y+dir_7[i][1];            if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]!='#')            {                int k=judge_dir(dir_7[i][0],dir_7[i][1]);                right_dfs(xx,yy,step+1,k);            }            if(flag)                return ;        }    }    else if(dir==2)    {        for(int i=0; i<4; i++)        {            int xx=x+dir_6[i][0],yy=y+dir_6[i][1];            if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]!='#')            {                int k=judge_dir(dir_6[i][0],dir_6[i][1]);                right_dfs(xx,yy,step+1,k);            }            if(flag)                return ;        }    }    else if(dir==3)    {        for(int i=0; i<4; i++)        {            int xx=x+dir_5[i][0],yy=y+dir_5[i][1];            if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]!='#')            {                int k=judge_dir(dir_5[i][0],dir_5[i][1]);                right_dfs(xx,yy,step+1,k);            }            if(flag)                return ;        }    }    else if(dir==0)    {        for(int i=0; i<4; i++)        {            int xx=x+dir_8[i][0],yy=y+dir_8[i][1];            if(xx>=0&&yy>=0&&xx<n&&yy<m&&s[xx][yy]!='#')            {                int k=judge_dir(dir_8[i][0],dir_8[i][1]);                right_dfs(xx,yy,step+1,k);            }            if(flag)                return ;        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&m,&n);        int i,j,x1,y1,x2,y2;        for(i=0; i<n; i++)        {            scanf("%s",s[i]);            for(j=0; j<m; j++)            {                if(s[i][j]=='S')                    x1=i,y1=j;                else if(s[i][j]=='E')                    x2=i,y2=j;            }        }        ans=bfs(x1,y1);        flag=0;        left_dfs(x1,y1,1,0);        flag=0;        right_dfs(x1,y1,1,0);        printf("%d %d %d\n",left_ans,right_ans,ans);    }    return 0;}


1 0
原创粉丝点击