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;
}

  

原文地址:https://www.cnblogs.com/sola1994/p/4296705.html