HDU - 2102 - A计划 (BFS)

题意:

两层地图,只有通过时空传输机才能在两层之间移动,问能否在规定时间移动到'P'

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
const int maxn = 100 + 10;
char G[2][maxn][maxn];
int n,m,t;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
struct Point
{
    int x,y,f,step;
};
bool check(Point p)
{
    if(p.step > t || G[p.f][p.x][p.y] == '*' )
        return 0;
    return 1;
}
int bfs()
{
    queue<Point> que;
    Point now,next;
    now.x  = now.y = 1;
    now.f =  now.step = 0;
    que.push(now);
    while(!que.empty())
    {
        now = que.front();
        que.pop();
        if(G[now.f][now.x][now.y] == '*' ) continue;
        if(G[now.f][now.x][now.y] == 'P' )
            return 1;
        G[now.f][now.x][now.y] = '*';
        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + dx[i];
            next.y = now.y + dy[i];
            next.step = now.step + 1;
            next.f = now.f;
            if(!check(next)) continue;
            if(G[next.f][next.x][next.y] == '#')
            {
                G[next.f][next.x][next.y] = '*';
                next.f = 1 - next.f;
                if(G[next.f][next.x][next.y] == '#' || G[next.f][next.x][next.y]=='*')
                {
                    G[next.f][next.x][next.y]  = G[1-next.f][next.x][next.y]  = '*';
                    continue;
                }
            }
            que.push(next);
        }
    }
    return 0;
}
int main()
{
    int c;
    for(cin >> c; c > 0; c--)
    {
        memset(G,'*',sizeof G);
        cin>>n>>m>>t;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                cin>>G[0][i][j];
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= m; j++)
            {
                cin>>G[1][i][j];
            }
        }
        if(bfs())
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值