hdu2102 A计划

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102

题意:中文题就不翻译了,在T时刻前找到也行。

(在变量设置的时候T和cnt一开始都是T......然后wa了好几次才发现。太蠢了)

简单的bfs

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>

using namespace std;

const int maxn = 15;
int n, m, cnt;
char Map[2][maxn][maxn];
int x[] = {0, 1, 0, -1};
int y[] = {1, 0, -1, 0};

struct P
{
    int sig;
    int x;
    int y;
    int t;
    P(){t = 0; }
    P(int X, int Y, int Sig, int T):
    x(X), y(Y), sig(Sig), t(T)
    {}
} node;

int BFS(P s)
{
    s.t = 0;
    bool vis[2][maxn][maxn];
    memset(vis, 0, sizeof(vis));
    queue<P> Q;
    Q.push(s);
    vis[s.sig][s.x][s.y] = 1;
    while(!Q.empty())
    {
        P cur = Q.front();
        Q.pop();
        if(Map[cur.sig][cur.x][cur.y] == 'P')
        {
            return cur.t;
        }
        for(int i = 0; i < 4; i++)
        {
            int X = cur.x + x[i];
            int Y = cur.y + y[i];
            int Sig = cur.sig;
            int T = cur.t + 1;
            if(X < 0 || X >= n || Y < 0 || Y >= m ||
               (vis[Sig][X][Y] == 1 && Map[Sig][X][Y] == '.') || Map[Sig][X][Y] == '*' ||
                    (Map[Sig][X][Y] == '#' && Map[Sig ^ 1][X][Y] == '*') ||
                        (Map[Sig][X][Y] == '#' && Map[Sig ^ 1][X][Y] == '#'))
                continue;

            P tmp(X, Y, Sig, T);
            if(Map[Sig][X][Y] == '#')
                tmp.sig ^= 1;
            if(vis[tmp.sig][tmp.x][tmp.y] == 0)
            {
                Q.push(tmp);
                vis[tmp.sig][tmp.x][tmp.y] = 1;
            }
        }
    }
    return 99999;
}

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n >> m >> cnt;
        for(int i = 0; i < 2; i++)
        {
            for(int j = 0; j < n; j++)
            {
                scanf("%s", Map[i][j]);
                for(int k = 0; k < m; k++)
                {
                    if(Map[i][j][k] == 'S')
                    {
                        node.x = j;
                        node.y = k;
                        node.sig = i;
                    }
                }
            }
        }
        int ans = BFS(node);
        if(ans <= cnt)
        {
            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、付费专栏及课程。

余额充值