zoukankan      html  css  js  c++  java
  • hdu 2102 A计划(BFS,基础)

    题目

    //要仔细写的BFS,着重对#穿越的处理哦;

    //花了几个小时终于把这道简单的BFS给弄好了,我果然还需要增加熟练度,需要再仔细一些;

    //代码有点乱,但我不想改了,,,,,

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    struct tt
    {
        int x,y,step,floor;
    };
    int visit[20][20][2],n,m,t;
    char map1[20][20],map2[20][20];
    queue <tt> q;
    int xx[4]={-1,0,1,0};
    int yy[4]={0,1,0,-1};
    
    int bfs()
    {
        int i,flag;
        tt front,temp;
        temp.x=0;temp.y=0;temp.step=0;temp.floor=1;
        q.push(temp);
        visit[0][0][0]=1;
        while(!q.empty())
        {
            front=q.front();
            q.pop();
            for(i=0;i<4;i++)
            {
                
                temp.x=front.x+xx[i];
                temp.y=front.y+yy[i];
                temp.step=front.step+1;
                temp.floor=front.floor;
                if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m)
                {
                    if(temp.floor==1&&visit[temp.x][temp.y][0]==0&&map1[temp.x][temp.y]!='*')
                    {
                        flag=0;
                        visit[temp.x][temp.y][0]=1;
                        if(map1[temp.x][temp.y]=='P')
                            return temp.step;
    
                        if(map1[temp.x][temp.y]=='#')
                        {
                            flag=1;
                            if(map2[temp.x][temp.y]=='P')
                                return temp.step; 
                            else if(map2[temp.x][temp.y]=='.'&&visit[temp.x][temp.y][1]==0)
                            {
                                temp.floor=2;
                                q.push(temp);
                                
                            }
                            visit[temp.x][temp.y][1]=1;
                        }
                         if(flag==0)
                            q.push(temp);
                    }
                    else if(temp.floor==2&&visit[temp.x][temp.y][1]==0&&map2[temp.x][temp.y]!='*')
                    {
                        flag=0;
                        
                        if(map2[temp.x][temp.y]=='P')
                            return temp.step;
                        
                        if(map2[temp.x][temp.y]=='#')
                        {
                            flag=1;
                            if(map1[temp.x][temp.y]=='P')
                                return temp.step; 
                            else if(map1[temp.x][temp.y]=='.'&&visit[temp.x][temp.y][0]==0)
                            {
                                temp.floor=1;
                                q.push(temp);
                                
                            }
                            visit[temp.x][temp.y][0]=1;
                        }
    
                        visit[temp.x][temp.y][1]=1;
                        if(flag==0)
                            q.push(temp);
                    }
                    
                }
            }
        }
        return t+1;
    }
    
    int main()
    {
        int T,i,ans;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d%d",&n,&m,&t);
            for(i=0;i<n;i++)
            {
                scanf("%s",map1[i]);
            }
            for(i=0;i<n;i++)
            {
                scanf("%s",map2[i]);
            }
            memset(visit,0,sizeof(visit));
            while(!q.empty())
                q.pop();
            ans=bfs();
            if(ans<=t)
                printf("YES
    ");
            else
                printf("NO
    ");   
        }
        return 0;
    }
    View Code
    一道又一道,好高兴!
  • 相关阅读:
    【转】IDEA2019.1.3版本破解
    Docker部署Vue
    Docker使用
    MySql触发器
    JVM 理论基础目录(待更新,系列完全写完后会统一整理好)
    JVM 5 JAVA 垃圾回收机制
    JVM 运行时数据区:程序计数器、Java 虚拟机栈和本地方法栈,方法区、堆以及直接内存
    JVM 整体流程介绍
    JVM 入门指南
    Linux 常用命令(根据自己的理解随时更新)
  • 原文地址:https://www.cnblogs.com/laiba2004/p/3529002.html
Copyright © 2011-2022 走看看