poj 3083 Children of the Candy Corn(搜索)

http://poj.org/problem?id=3083

题意:迷宫沿着左墙走,和沿着右强的距离以及最短路径的距离分别是多少,当向左走的时候1的位置就是面向左,向右走1的位置面向右,这样可以省去很多事

分析:DFS+BFS

         先确定前一位置的方向,再决定下一位置是朝哪个方向旋转

#include<stdio.h>

#include<string.h>

#include<queue>

using namespace std;

const int MAXN=50;

int m,n;

int flag,ans1,ans2,ans3;

char map[MAXN][MAXN];

int cnt[MAXN][MAXN];

int vis[MAXN][MAXN];

struct Node

{

    int x,y;

} s,e;



void DFS_L(int x,int y,int step,int face)

{

    if(flag) return ;

    if(x==e.x && y==e.y)

    {

        ans1=step;

        flag=1;

        return ;

    }

    switch(face)

    {

    case 1://

        if(map[x+1][y]) DFS_L(x+1,y,step+1,4);

        else if(map[x][y-1]) DFS_L(x,y-1,step+1,1);

        else if(map[x-1][y]) DFS_L(x-1,y,step+1,2);

        else if(map[x][y+1]) DFS_L(x,y+1,step+1,3);

        break;

    case 2://

        if(map[x][y-1]) DFS_L(x,y-1,step+1,1);

        else if(map[x-1][y]) DFS_L(x-1,y,step+1,2);

        else if(map[x][y+1]) DFS_L(x,y+1,step+1,3);

        else if(map[x+1][y]) DFS_L(x+1,y,step+1,4);

        break;

    case 3://

        if(map[x-1][y]) DFS_L(x-1,y,step+1,2);

        else if(map[x][y+1]) DFS_L(x,y+1,step+1,3);

        else if(map[x+1][y]) DFS_L(x+1,y,step+1,4);

        else if(map[x][y-1]) DFS_L(x,y-1,step+1,1);



    case 4://

        if(map[x][y+1]) DFS_L(x,y+1,step+1,3);

        else if(map[x+1][y]) DFS_L(x+1,y,step+1,4);

        else if(map[x][y-1]) DFS_L(x,y-1,step+1,1);

        else if(map[x-1][y]) DFS_L(x-1,y,step+1,2);

        break;

    }

}



void DFS_R(int x,int y,int step,int face)

{

    if(flag) return ;

    if(x==e.x && y==e.y)

    {

        ans2=step;

        flag=1;

        return ;

    }

    switch(face)

    {

    case 1://

        if(map[x+1][y]) DFS_R(x+1,y,step+1,4);

        else if(map[x][y+1]) DFS_R(x,y+1,step+1,1);

        else if(map[x-1][y]) DFS_R(x-1,y,step+1,2);

        else if(map[x][y-1]) DFS_R(x,y-1,step+1,3);

        break;

    case 2://

        if(map[x][y+1]) DFS_R(x,y+1,step+1,1);

        else if(map[x-1][y]) DFS_R(x-1,y,step+1,2);

        else if(map[x][y-1]) DFS_R(x,y-1,step+1,3);

        else if(map[x+1][y]) DFS_R(x+1,y,step+1,4);

        break;

    case 3://

        if(map[x-1][y]) DFS_R(x-1,y,step+1,2);

        else if(map[x][y-1]) DFS_R(x,y-1,step+1,3);

        else if(map[x+1][y]) DFS_R(x+1,y,step+1,4);

        else if(map[x][y+1]) DFS_R(x,y+1,step+1,1);



    case 4://

        if(map[x][y-1]) DFS_R(x,y-1,step+1,3);

        else if(map[x+1][y]) DFS_R(x+1,y,step+1,4);

        else if(map[x][y+1]) DFS_R(x,y+1,step+1,1);

        else if(map[x-1][y]) DFS_R(x-1,y,step+1,2);

        break;

    }

}





int BFS()

{

    int i;

    queue<Node> Q;

    Q.push(s);

    Node head,next;

    while(!Q.empty())

    {

        head=Q.front();

        Q.pop();



        if(!vis[head.x-1][head.y] && map[head.x-1][head.y])

        {

            next.x=head.x-1;

            next.y=head.y;

            Q.push(next);

            vis[next.x][next.y]=1;

            cnt[next.x][next.y]=cnt[head.x][head.y]+1;

        }

        if(!vis[head.x+1][head.y]&& map[head.x+1][head.y])

        {

            next.x=head.x+1;

            next.y=head.y;

            Q.push(next);

            vis[next.x][next.y]=1;

            cnt[next.x][next.y]=cnt[head.x][head.y]+1;

        }

        if(!vis[head.x][head.y-1]&& map[head.x][head.y-1])

        {

            next.x=head.x;

            next.y=head.y-1;

            Q.push(next);

            vis[next.x][next.y]=1;

            cnt[next.x][next.y]=cnt[head.x][head.y]+1;

        }

        if(!vis[head.x][head.y+1] && map[head.x][head.y+1])

        {

            next.x=head.x;

            next.y=head.y+1;

            Q.push(next);

            vis[next.x][next.y]=1;

            cnt[next.x][next.y]=cnt[head.x][head.y]+1;

        }

        vis[head.x][head.y]=1;

        if(e.x==head.x && e.y==head.y) return cnt[head.x][head.y];

    }

    return cnt[head.x][head.y];

}



int main()

{

    int T,i,j;

    scanf("%d",&T);

    while(T--)

    {

        memset(map,0,sizeof(map));

        scanf("%d%d",&m,&n);

        for(i=1; i<=n; i++)

        {

            scanf("%s",map[i]+1);

            for(j=1; map[i][j]; j++)

            {

                if(map[i][j]=='.') map[i][j]=1;

                else if(map[i][j]=='#') map[i][j]=0;

                if(map[i][j]=='S')

                {

                    s.x=i;

                    s.y=j;

                    map[i][j]=1;

                }

                else if(map[i][j]=='E')

                {

                    e.x=i;

                    e.y=j;

                    map[i][j]==1;

                }

            }

        }

        flag=0;

        DFS_L(s.x,s.y,1,1);

        flag=0;

        DFS_R(s.x,s.y,1,1);

        memset(cnt,0,sizeof(cnt));

        memset(vis,0,sizeof(vis));

        ans3=BFS();

        printf("%d %d %d\n",ans1,ans2,ans3+1);

    }

    return 0;

}

 

你可能感兴趣的:(children)