hdu2102——A计划——————【BFS】

题目比较简单,只是一个简单搜索,只是添加了一点条件。传输机实现上下层转移,仅仅这个条件。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int f[4][2] = { { -1, 0}, {0, 1}, {1, 0}, {0, -1}};
char Map[15][15][2];
bool vis[15][15][2];
struct pos {

    int x, y, z, step;

};

bool BFS ( int n, int m, int t ) {

    pos st;
    st.step = 0, st.x = 0, st.y = 0, st.z = 0;
    vis[st.x][st.y][st.z] = 1;
    queue<pos>Q;
    Q.push ( st );
    while ( !Q.empty() ) {

        pos tmp;
        tmp.x = Q.front().x , tmp.step = Q.front().step ;
        tmp.y = Q.front().y , tmp.z = Q.front().z;
        //  cout<<Map[tmp.x][tmp.y][tmp.z]<<" "<<tmp.x<<" "<<tmp.y<<" "<<tmp.z<<" "<<tmp.step<<endl;
        if ( Map[tmp.x][tmp.y][tmp.z] == 'P' && tmp.step <= t ) {

            return true;
        }
        pos temp;
        for ( int i = 0; i < 4; i++ ) {

            temp.x = tmp.x + f[i][0];
            temp.y = tmp.y + f[i][1];
            temp.z = tmp.z;
            temp.step = tmp.step + 1;
            if ( temp.x >= 0 && temp.x < n && temp.y >= 0 && temp.y < m && temp.z >= 0 && temp.z < 2 && temp.step <= t && !vis[temp.x][temp.y][temp.z] ) {

                vis[temp.x][temp.y][temp.z] = 1;
                if ( Map[temp.x][temp.y][temp.z] == '.' || Map[temp.x][temp.y][temp.z] == 'P' ) {

                    Q.push ( temp );
                    vis[temp.x][temp.y][temp.z] = 1;
                }
                if ( Map[temp.x][temp.y][temp.z] == '#' ) {

                    if ( temp.z == 0 )
                        temp.z = 1;
                    else
                        temp.z = 0;
                    if ( Map[temp.x][temp.y][temp.z] == '.' || Map[temp.x][temp.y][temp.z] == 'P' && !vis[temp.x][temp.y][temp.z] ) {

                        Q.push ( temp );
                    }
                }
            }
        }
        Q.pop();
    }

    return false;
}
int main() {

    int C, N, M , T;
    scanf ( "%d", &C );
    while ( C-- ) {

        memset ( vis, 0, sizeof ( vis ) );
        cin >> N >> M >> T;
        for ( int k = 0; k < 2; k++ ) {

            for ( int i = 0; i < N; i++ ) {

                for ( int j = 0; j < M; j++ ) {

                    cin >> Map[i][j][k];
                }
            }
        }
        bool ans = BFS ( N, M, T );
        if ( ans ) {

            printf ( "YES\n" );
        }
        else
            printf ( "NO\n" );
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值