zoukankan      html  css  js  c++  java
  • hdu 2102 A计划(bfs)

    之前没有考虑到 两层都是 # 的情况 

    #include<cstdio>
    #include<cmath>
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #define mem(a,b) memset(a,b,sizeof(a))
    using namespace std;
    int n,m,coun,ok1;
    char mat[5][20][20];
    int op[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    struct node{
      int t;
      int w,x,y;
    };
    
    bool ok(node next)
    {
        if(next.x<0||next.x>=n||next.y<0||next.y>=m) return false;   
        if(mat[next.w][next.x][next.y]=='S'||mat[next.w][next.x][next.y]=='*') return false;
        return true;
    }
    void bfs()
    {
        queue<node> q;
        node now,next;
        now.t=0;
        now.w=0;
        now.x=0;
        now.y=0;
        q.push(now);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(now.t>=coun) return;
    
            for(int i=0;i<4;i++)
            {
                next.x=now.x+op[i][0];
                next.y=now.y+op[i][1];
                next.w=now.w;
                if(ok(next))
                {
                    next.t=now.t+1;
                    if(mat[now.w][next.x][next.y]=='#') {next.w=1-now.w;}
                    if(!ok(next)) continue;
                if(mat[next.w][next.x][next.y]=='P')
                {
                    ok1=1;
                    return ;
                }            
                if(mat[next.w][next.x][next.y]=='.')
                {
                    mat[next.w][next.x][next.y]='S';
                    q.push(next);
                }            
                }            
            }
        }
    }
    int main()
    {
        int i,t;
        cin>>t;
        while(t--)
        {
            ok1=0;
            scanf("%d%d%d",&n,&m,&coun);
            memset(mat,0,sizeof(mat));
            for(i=0;i<n;i++)
            {
                scanf("%s",mat[0][i]);
            }
            for(i=0;i<n;i++)
            {
                scanf("%s",mat[1][i]);
            }
            bfs();
            if(ok1)
                printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    牛顿插值法及其C++实现
    见鬼吧,拉格朗日插值法
    迭代法与开根号求值(letcode 69)
    多项式计算之秦九韶算法
    (扩展根目录容量方法汇总)把Linux系统迁移到另一个分区或者硬盘
    jdk 动态代理源码分析
    python 随笔
    java 笔记
    enum(枚举类型)
    提高工作效率的准则
  • 原文地址:https://www.cnblogs.com/sola1994/p/4296705.html
Copyright © 2011-2022 走看看