搜索--九度1456.[BFS]
来源:互联网 发布:java外包公司 编辑:程序博客网 时间:2024/06/04 00:25
题目[胜利大逃亡]:http://ac.jobdu.com/problem.php?pid=1456
坐标变换数组是一种很好的形式,以后多尝试
#include<cstdio>#include<queue>using namespace std;int cas[51][51][51] = { 0 }; //坐标点int mark[51][51][51] = { 0 }; //是否已访问int a, b, c, t;int go[][3] = { //坐标变换数组 1,0,0, -1,0,0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1};struct point { int x, y, z;//坐标 int time; //访问到本点的最短时间};queue<point> N; //队列int BFS() { while (!N.empty()) {//非空 point temp = N.front(); // 出队列 N.pop(); point now; for (int i = 0; i < 6; i++) { now.x = temp.x + go[i][0]; now.y = temp.y + go[i][1]; now.z = temp.z + go[i][2]; now.time = temp.time + 1; if (now.x < 0 || now.x >= a || now.y < 0 || now.y >= b || now.z < 0 || now.z >= c)//区域外 continue; if (1 == mark[now.x][now.y][now.z]) continue; //已访问 if (1 == cas[now.x][now.y][now.z]) continue; //墙 N.push(now); //入队列 mark[now.x][now.y][now.z] = 1; if (a - 1 == now.x && b - 1 == now.y && c - 1 == now.z)//到达终点 return now.time; } } return -1;}int main() { int n; scanf("%d", &n); while (n-- > 0) { //初始化数据 scanf("%d%d%d%d", &a, &b, &c, &t); for (int i = 0; i < a; i++) for (int j = 0; j < b; j++) for (int k = 0; k < c; k++) { scanf("%d", &cas[i][j][k]); mark[i][j][k] = 0; } while (N.empty() == false) N.pop(); //清队列 point temp; temp.x = temp.y = temp.z = temp.time = 0; N.push(temp); // 零点入队列 int BFS_minT = BFS(); if (BFS_minT <= t) printf("%d\n", BFS_minT); else printf("-1\n"); }//while return 0;}
0 0
- 搜索--九度1456.[BFS]
- 搜索--九度1457.[BFS]【好题】
- 搜索BFS
- 搜索 bfs
- 搜索 BFS
- BFS搜索
- 搜索--九度1461.[DFS]
- 九度:1456 <BFS><胜利大逃亡>
- 九度OJ 1482 BFS+HASH
- 九度1009二叉搜索树
- 九度:1027<欧拉回路><搜索>
- 九度OJ 1009 二叉搜索树
- 九度题目1009:二叉搜索树
- 九度题目1009:二叉搜索树
- 九度oj-1009-二叉搜索树
- 九度OJ - 1009 - 二叉搜索树
- 九度OJ 1009 二叉搜索树
- 【九度】题目1009:二叉搜索树
- MySql优化之索引
- Maven仓库 和生命周期
- POJ 3211 Washing Clothes 可能会
- 【Linux基础学习之三】Bash的一些基础知识总结(通配符、重定向、管道、命令补全)
- Python VS Java
- 搜索--九度1456.[BFS]
- mysql常用函数笔记
- 章节号对应的翻译
- R-QQ聊天记录分析练习
- 【Linux基础学习之四】文件系统的基础知识总结(主要目录结构、文件类型、mount、硬连接)
- solidworks 之迈迪同步轮尺寸与设计不复的解决
- 形态学中灰度图像腐蚀实现的MATLB代码
- 理解 Flexbox:你需要知道的一切
- Android Service Manager