poj 2251 三维地图bfs

三维地图

poj 2251

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;

char map[35][35][35];
int l,r,c;

bool book[35][35][35];
// 定义 东西南北和上下
struct dis{
	int x,y,z;
	int step;
	dis(int x,int y,int z,int step):x(x),y(y),z(z),step(step){
	}
};
int dx[6]={1,0,0,-1,0,0};
int dy[6]={0,1,-1,0,0,0};
int dz[6]={0,0,0,0,1,-1};
int bfs(int x,int y,int z){
	queue<dis> st;
	st.push(dis(x,y,z,0));
	while(!st.empty()){
		dis cur=st.front();
		st.pop();
		if(map[cur.x][cur.y][cur.z]==‘E‘){
			return cur.step;
		}
		for(int i=0;i<6;i++){// 加入队列
			int x=cur.x + dx[i];
			int y=cur.y + dy[i];
			int z=cur.z + dz[i];
			if(x<l&&y<r&&z<c&&x>=0&&y>=0&&z>=0&&map[x][y][z]!=‘#‘&&!book[x][y][z]){
				book[x][y][z]=1;
				st.push(dis(x,y,z,cur.step+1));
			}

		}

	}
	return -1;
}

int main(){
	freopen("p2251.txt","r",stdin);
	while(scanf("%d%d%d",&l,&r,&c)&&l){
		for(int j=0;j<l;j++){
			for(int i=0;i<r;i++){
				scanf("%s",map[j][i]);
			}
			scanf("\n");
		}
		memset(book,0,sizeof(book));
		for(int i=0;i<l;i++)
		for(int j=0;j<r;j++)
		for(int k=0;k<c;k++)
			if(map[i][j][k]==‘S‘){
				int d=bfs(i,j,k);
				if(d==-1){
					cout<<"Trapped!\n";
				}else{
					printf("Escaped in %d minute(s).\n",d);
				} 

			}

	}	

	return 0;

}

原文地址:https://www.cnblogs.com/chichina/p/12659354.html

时间: 2024-09-30 00:05:44

poj 2251 三维地图bfs的相关文章

POJ 2251 三维广搜。

B - Dungeon Master Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is com

POJ 2251 Dungeon Master(bfs)

Dungeon Master Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or dow

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled wit

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

BFS POJ 2251 Dungeon Master

题目传送门 1 /* 2 BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <queue> 8 using namespace std; 9 10 const int MAXN = 33; 11 const int INF = 0x3f3

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ 2049 Finding Nemo BFS

题目大意:给你一个奇奇怪怪的迷宫, 这个迷宫包括墙和门.再给你一个起始坐标, 问你从迷宫内到外面至少要穿越多少的门. 题目分析: 穿越多少门等同于路过了多少个格子. 为此我们可以将整个地图中的格子,门,墙,墙的交界处(格子的顶点)全部抽象成点. 即坐标(奇数,奇数)为格子的坐标,坐标(奇数,偶数)或坐标(偶数,奇数)为门或墙的坐标,坐标(偶数,偶数)为格子的顶点. 这样题目就转化成了从起始点所在的格子走到迷宫外的格子最少要经过多少个格子,用step[i][j]表示走出迷宫后遇到的第一个格子的坐标