首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >poj 3083 Children of the Candy Corn(搜索)

poj 3083 Children of the Candy Corn(搜索)

作者头像
用户1624346
发布2018-04-17 16:10:02
7460
发布2018-04-17 16:10:02
举报
文章被收录于专栏:calmoundcalmound

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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-02-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档