POJ 2251 Dungeon Master

17 篇文章 0 订阅

Dungeon Master

题目是对三维地图的搜索,用bfs求最短时间。同二维的情况一样,都是一样的搜索。代码如下:

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

#define MAXN 5000
#define NMIN 35

struct Node{
	int x ;
	int y ;
	int z ;
	int s ;
};

int maze[NMIN][NMIN][NMIN] ;

int dx[6] = {1 , -1 , 0 , 0 , 0 , 0} ;
int dy[6] = {0 , 0 , 1 , -1 , 0 , 0} ;
int dz[6] = {0 , 0 , 0 , 0 , -1 , 1} ;

int l ;//高度
int r ;//行
int c ;//列

struct Node e ;
struct Node s ;

void bfs(){
	Node a ;
	Node b ;
	a.s = 0 ;
	
	queue<Node> Q ;
	Q.push(s) ;

	while(!Q.empty()){
		a = Q.front() ;
		
		if(a.x == e.x && a.y == e.y && a.z == e.z){
			printf("Escaped in %d minute(s).\n" , a.s) ;
			return ;
		}

		Q.pop() ;

		for(int i = 0 ; i < 6 ; i ++){
			b.x = a.x + dx[i] ;
			b.y = a.y + dy[i] ;
			b.z = a.z + dz[i] ;
			b.s = a.s + 1 ;
			
			if(b.x < 1 || b.x > l || b.y < 1 || b.y > r||
					b.z < 1 || b.z > c)
				continue ;
			if(maze[b.x][b.y][b.z]){
				continue ;
			}

			maze[b.x][b.y][b.z] = 1 ;
			
			if(b.x == e.x && b.y == e.y && e.z == b.z){
				printf("Escaped in %d minute(s).\n" , b.s) ;
				return ;
			}
			Q.push(b) ;
		}

	}
	printf("Trapped!\n") ;
}

bool read(){
	scanf("%d %d %d" , &l , &r , &c) ;

	if(!(c&&r&&l))
		return 0 ;
	
	char ch ;
	int i ;
	int j ;
	int k ;
	
	for(i = 1 ; i <= l ; i ++){
		for(j = 1 ; j <= r ; j ++){
			getchar() ;
			for(k = 1 ; k <= c ; k ++){
				ch = getchar() ;
				if(ch=='#')
					maze[i][j][k] = 1 ;
				else 
					maze[i][j][k] = 0 ;
				if(ch=='S'){
					s.x = i ;
					s.y = j ;
					s.z = k ;
				}
				if(ch=='E'){
					e.x = i ;
					e.y = j ;
					e.z = k ;
				}
			}
		}

		getchar() ;
	}

	return 1 ;
}

int main(){
	while(read()){
		bfs() ;
	}
	return 0 ;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值