当前位置: 首页 > news >正文

nyoj 523 双向广搜

 

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
/*
    用普通搜索TLE,已知起点和终点,可以考虑双向广搜或A*算法加速搜索
    双向广搜,一个方向从出发点向终点搜索,一个方向从终点向出发点搜索,搜索到相同的结点时,即找到最短路径。 
*/
const int N = 52;
int dir[6][3] = {
    1,0,0,  -1,0,0,
    0,1,0,  0,-1,0,
    0,0,1,  0,0,-1
};
int mp[N][N][N];
int vis[N][N][N];//访问标记: 0没有访问过  1 被从开始到终点的方向访问过  2 被终到始的方向访问过
int step[N][N][N];
int h, m, n;
int times;
struct Node {
    int z, x, y;
    int step; 
    Node(int _z, int _x, int _y){
        z = _z; x = _x; y = _y;
    }
};

int bfs()
{
    if(mp[h-1][m-1][n-1] == 1) return -1;//终点可能是墙...
    memset(&vis[0][0][0], 0, sizeof(vis));
    memset(&step[0][0][0], 0, sizeof(step));
    Node start(0, 0, 0);
    vis[0][0][0] = 1;
    Node end(h-1, m-1, n-1);
    vis[h-1][m-1][n-1] = 2;
    queue<Node> que;
    que.push(start);
    que.push(end);
    while(!que.empty()){
        Node cur = que.front(); 
        que.pop();
        if(cur.step > times) continue;
        int cx = cur.x, cy = cur.y, cz= cur.z;
        for(int i=0; i<6; i++){
            int tx = cx+dir[i][0];
            int ty = cy+dir[i][1];
            int tz = cz+dir[i][2];
            if(tx <0 || tz<0 || ty<0 || tz>=h || tx>=m || ty >=n) continue;
            if(mp[tz][tx][ty] == 1) continue;
            if(vis[tz][tx][ty] == 0){
                Node next(tz, tx, ty);
                step[tz][tx][ty] = step[cz][cx][cy]+1;
                vis[tz][tx][ty] = vis[cz][cx][cy];//访问标记要保持一致
                que.push(next);
            } else if(vis[tz][tx][ty] != vis[cz][cx][cy]){//如果访问标记不一致,则说明来自不同方向上搜索的相遇的结点 
                return step[cz][cx][cy]+step[tz][tx][ty]+1 <= times? step[cz][cx][cy]+step[tz][tx][ty]+1: -1;
            }

        }
        
    }
    return -1;
}

int main()
{
    //INPUT;
    //OUTPUT;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d", &h, &m, &n, &times);
        for(int i=0; i<h; i++){
            for(int j=0; j<m; j++){
                for(int k=0; k<n; k++){
                    scanf("%d", &mp[i][j][k]);
                }
            }
        }
        printf("%d\n", bfs());
    }

    return 0;
}

转载于:https://www.cnblogs.com/huwtylv/p/4319356.html

相关文章:

  • JS调用后台方法大全
  • 即时通信3
  • frame-relay实验
  • eclipse启动不了报错java was started but returned exit code=13
  • GDAL编译Windows平台下64位的方式
  • java调用webservice
  • 使用JSR234实现对图片的缩放
  • 《大型分布式网站架构设计与实践》
  • 迷路
  • SQL Server 高可用使用环境
  • Java基础之异常
  • 心情流水账
  • 人生健康三标准
  • PHP如何判断一个gif图片是否为动画?
  • 项目管理理论与实践(4)——UML应用(上)
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • CSS魔法堂:Absolute Positioning就这个样
  • JavaScript实现分页效果
  • Mac转Windows的拯救指南
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • MySQL数据库运维之数据恢复
  • python_bomb----数据类型总结
  • Python学习之路13-记分
  • React 快速上手 - 07 前端路由 react-router
  • 包装类对象
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 基于Android乐音识别(2)
  • 老板让我十分钟上手nx-admin
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 入手阿里云新服务器的部署NODE
  • 通过git安装npm私有模块
  • 微服务核心架构梳理
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 小试R空间处理新库sf
  • 在Docker Swarm上部署Apache Storm:第1部分
  • SAP CRM里Lead通过工作流自动创建Opportunity的原理讲解 ...
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • #Spring-boot高级
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (day6) 319. 灯泡开关
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (转)关于如何学好游戏3D引擎编程的一些经验
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .bat批处理(三):变量声明、设置、拼接、截取
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • .net经典笔试题
  • ;号自动换行
  • [ linux ] linux 命令英文全称及解释
  • [ vulhub漏洞复现篇 ] AppWeb认证绕过漏洞(CVE-2018-8715)
  • [3D基础]理解计算机3D图形学中的坐标系变换