hdu2102 A计划(2层bfs)

看的别人的思路写,还WA了n次,我的心是崩溃的再见

以后三维坐标统一变成x为z轴好了,省夜长梦多。。。

还有最fuck的一点,我吧queue<node>q定义为全局变量,居然显示WA。实在是不解啊哭

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;

const int N = 20;
const int INF = 1000000;

char Map[2][N][N];
bool vis[2][N][N];
int m, n, t, flag;

int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

struct node
{
    int x, y, z;
    int step;
};

node s_pos;

bool check(int x, int y, int z)
{
    if(x >= 0 && x < 2 && y >= 0 && y < n && z >= 0 && z < m && Map[x][y][z] != '*') return true;
    else return false;
}

void bfs()
{
    memset(vis, 0, sizeof(vis));
    vis[s_pos.x][s_pos.y][s_pos.z] = true;
    queue <node> q;
    q.push(s_pos);
    while(!q.empty())
    {
        node tmp = q.front();
        q.pop();
        if(Map[tmp.x][tmp.y][tmp.z] == 'P' && tmp.step <= t)
        {
            flag = 1;
            return;
        }
        for(int i = 0; i < 4; i ++)
        {
            node tmp2;
            tmp2 = tmp;
            tmp2.y += dir[i][0];
            tmp2.z += dir[i][1];
            tmp2.step += 1;

            if(check(tmp2.x, tmp2.y, tmp2.z) && !vis[tmp2.x][tmp2.y][tmp2.z] && tmp2.step <= t)
            {
                vis[tmp2.x][tmp2.y][tmp2.z] = 1;
                if(Map[tmp2.x][tmp2.y][tmp2.z] == '#')
                {
                    if(Map[(tmp2.x + 1) % 2][tmp2.y][tmp2.z] != '*' && !vis[(tmp2.x + 1) % 2][tmp2.y][tmp2.z])
                        tmp2.x = (tmp2.x + 1) % 2;
                }
                q.push(tmp2);
            }
        }
    }
}

int main()
{
  //  freopen("in.txt", "r", stdin);
    int C;
    scanf("%d", &C);
    while(C --)
    {
        scanf("%d%d%d", &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[k][i][j];
                    if(Map[k][i][j] == 'S')
                    {
                        s_pos.x = k;
                        s_pos.y = i;
                        s_pos.z = j;
                        s_pos.step = 0;
                    }
                }
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
            {
                if(Map[0][i][j] == '#' && Map[1][i][j] == '*')
                    Map[0][i][j] = '*';
                else if(Map[0][i][j] == '*' && Map[1][i][j] == '#')
                    Map[1][i][j] = '*';
                else if(Map[0][i][j] == '#' && Map[1][i][j] == '#')
                    Map[0][i][j] = Map[1][i][j] = '*';
            }
        flag = 0;
        bfs();
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值