Dungeon Master POJ-2251 三维BFS
题目链接:http://poj.org/problem?id=2251
题目大意
你被困在了一个三维的迷宫,找出能通往出口的最短时间。如果走不到出口,输出被困。
思路
由于要找最短路径,其实就是BFS。一般的BFS是前后左右四个方向,这个题相当于是变成能往上下左右前后六个方向找。修改一下二维BFS搜索部分的代码即可。
题解
- #include <iostream>
- #include <queue>
- #include<fstream>
- #include <cstring>
- //#define debug
- using namespace std;
- char a[][][];
- int vis[][][];
- int x,y,z; //范围
- int d1[] = {,-,,,,};
- int d2[] = {,,,-,,};
- int d3[] = {,,,,,-};
- struct point{
- int x;
- int y;
- int z;
- int step;
- }p1,p2,e;
- bool isValid(point p){
- if(p.x>= && p.x<x && p.y>= && p.y<y && p.z>= && p.z<z && (a[p.x][p.y][p.z] == '.' || a[p.x][p.y][p.z] == 'E' )&& vis[p.x][p.y][p.z] == ){
- return true;
- }
- return false;
- }
- bool success(point p){
- if(p2.x == e.x && p2.y == e.y && p2.z == e.z){
- return true;
- }
- return false;
- }
- void bfs(){
- point tmp;
- queue<point> q;
- q.push(p1);
- while(!q.empty()){
- p2 = q.front();
- q.pop();
- if(success(p2)){
- return;
- }else{
- for(int ii=;ii<;ii++){ //向六个方向搜索
- tmp.x = p2.x+d1[ii];
- tmp.y = p2.y+d2[ii];
- tmp.z = p2.z+d3[ii];
- if(isValid(tmp)){
- tmp.step = p2.step+;
- vis[tmp.x][tmp.y][tmp.z]= ;
- q.push(tmp);
- }
- }
- }
- }
- }
- int main()
- {
- #ifdef debug
- //cin重定向
- ifstream cin("C:\\Users\\Administrator\\Desktop\\test.txt");
- #endif
- while((cin >> x >> y >> z)){
- if(x == ){
- break;
- }
- for(int i = ;i < x;i++){ //读入maze
- for(int j = ;j < y;j++){
- for(int k = ;k < z;k++){
- cin >> a[i][j][k];
- if(a[i][j][k] == 'S'){
- p1.x = i;
- p1.y = j;
- p1.z = k;
- p1.step = ;
- }else if(a[i][j][k] == 'E'){
- e.x = i;
- e.y = j;
- e.z = k;
- }
- }
- }
- }
- memset(vis,,sizeof(vis)); //初始化vis
- bfs();
- if(p2.x == e.x && p2.y == e.y && p2.z == e.z){
- cout << "Escaped in " << p2.step << " minute(s)." << endl;
- }else{
- cout << "Trapped!" << endl;
- }
- }
- }
Dungeon Master POJ-2251 三维BFS的更多相关文章
- POJ 2251 Dungeon Master (非三维bfs)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55224 Accepted: 20493 ...
- Dungeon Master poj 2251 dfs
Language: Default Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16855 ...
- POJ 2251 三维BFS(基础题)
Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! Th ...
- Dungeon Master POJ - 2251 (搜索)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48605 Accepted: 18339 ...
- Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- Dungeon Master POJ - 2251(bfs)
对于3维的,可以用结构体来储存,详细见下列代码. 样例可以过,不过能不能ac还不知道,疑似poj炸了, #include<iostream> #include<cstdio> ...
- (广搜)Dungeon Master -- poj -- 2251
链接: http://poj.org/problem?id=2251 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2137 ...
- kuangbin专题 专题一 简单搜索 Dungeon Master POJ - 2251
题目链接:https://vjudge.net/problem/POJ-2251 题意:简单的三维地图 思路:直接上代码... #include <iostream> #include & ...
- B - Dungeon Master POJ - 2251
//纯bfs #include <iostream> #include <algorithm> #include <cstring> #include <cs ...
- POJ 2251 Dungeon Master(3D迷宫 bfs)
传送门 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 11 ...
随机推荐
- C/C++ 修改系统时间,导致sem_timedwait 一直阻塞的问题解决和分析
修改系统时间,导致sem_timedwait 一直阻塞的问题解决和分析 介绍 最近修复项目问题时,发现当系统时间往前修改后,会导致sem_timedwait函数一直阻塞.通过搜索了发现int sem_ ...
- Unity Editor已停止工作
在更换系统之后,可能会出现打开刚安装好的Unity,显示Unity Editor已停止工作,这时候我们考虑是系统win7的问题.可以在原系统上升级,也可以重新安装,升级.文中所涉及到的软件,可在右侧加 ...
- Python数据类型详解——列表
Python数据类型详解--列表 在"Python之基本数据类型概览"一节中,大概介绍了列表的基本用法,本节我们详细学一下列表. 如何定义列表:在[]内以英文里输入法的逗号,,按照 ...
- STL中的map和multimap小结
(1)使用map/multimap之前必须包含头文件<map>:#include<map> 并且和所有的关联式容器一样,map/multimap通常以平衡二叉树来完成 ( ...
- Gym 101470 题解
A:Banks 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt&q ...
- codeforces 743D. Chloe and pleasant prizes(树形dp)
题目链接:http://codeforces.com/contest/743/problem/D 大致思路挺简单的就是找到一个父节点然后再找到其两个字节点总值的最大值. 可以设一个dp[x]表示x节点 ...
- Play on Words UVA - 10129
题目: Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has ...
- yzoj P1412 & 洛谷P1629 邮递员送信 题解
有一个邮递员要送东西,邮局在结点1.他总共要送N-1样东西,其目的地分别是2~N.由于这个城市的交通比较繁忙,因此所有的道路都是单行的,共有M条道路,通过每条道路需要一定的时间.这个邮递员每次只能带一 ...
- go Server示例
示例1: package main import ( "fmt" "log" "net/http" "time" ) f ...
- 【Redis】安装、开启以及关闭
一.Linux环境的操作 1.1 下载安装 1.2 启动 1.3 连接Redis客户端 1.4 关闭 二.Windows和Mac下的操作 2.1 下载安装 2.2 启动 2.3 连接客户端 2.4 关 ...