7.A计划

原题:https://www.acwing.com/problem/content/solution/4236/1/

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

const int N=15,INF=1e9;
int n,m,limit;
char g[2][N][N];
int dist[2][N][N];
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
struct Node{
    int x,y,z;
};

bool bfs()
{
    queue<Node> q;
    q.push({0,0,0});
    memset(dist,0x3f,sizeof dist);
    dist[0][0][0]=0;
    while(q.size())
    {
        auto t=q.front();
        q.pop();
        if(g[t.z][t.x][t.y]=='P'){
            return dist[t.z][t.x][t.y]<=limit;
        }
        for(int i=0;i<4;i++)
        {
            int x=dx[i]+t.x,y=dy[i]+t.y,z=t.z;
            if(x<0||x>=n||y<0||y>=m||g[z][x][y]=='*') continue;
            if(g[z][x][y]=='#'){
                if(g[z^1][x][y]=='#') continue;
                if(g[z^1][x][y]=='*') continue;
                if(dist[z^1][x][y]!=0x3f3f3f3f) continue;
                dist[z^1][x][y]=dist[t.z][t.x][t.y]+1;
                q.push({x,y,z^1});
            }
            else if(dist[z][x][y]==0x3f3f3f3f){
                dist[z][x][y]=dist[t.z][t.x][t.y]+1;
                q.push({x,y,z});
            }
        }
    }
    return false;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&limit);
        for(int i=0;i<2;i++)
            for(int j=0;j<n;j++)
                scanf("%s",g[i][j]);
        if(bfs()) puts("YES");
        else puts("NO");
    }
    return 0;
}

posted on   skaman  阅读(41)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示