NYOJ--353--bfs+优先队列--3D dungeon

40 篇文章 0 订阅
22 篇文章 0 订阅
/*
	Name: NYOJ--3533D dungeon
	Author: shen_渊 
	Date: 15/04/17 15:10
	Description: bfs()+优先队列,队列也能做,需要开一个vis[35][35][35]标记
*/

#include<iostream>
#include<queue>
using namespace std;
struct node{
	int x,y,z,steps;
	node():steps(0){
	};
	bool operator <(const node &a)const {
		return steps>a.steps;
	}
};
int bfs();
priority_queue<node> q;
int l,r,c;
node s,e;
char map[35][35][35];
int dir[][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int main()
{
//	freopen("in.txt","r",stdin);
	while(cin>>l>>r>>c,l+r+c){
		for(int i=0; i<l; ++i){
			for(int j=0; j<r; ++j){
				cin>>map[i][j];
				for(int k=0; k<c; ++k){
					if(map[i][j][k] == 'S')s.x = j,s.z = i,s.y = k;
					if(map[i][j][k] == 'E')e.x = j,e.y = k,e.z = i;
				}
			}
		}
		int t;
		if (t=bfs())
            cout << "Escaped in " << t << " minute(s)." << endl;
        else
            cout << "Trapped!" << endl;
	}
	return 0;
}
int bfs(){
	while(!q.empty()) q.pop();
	q.push(s);
	while(!q.empty()){
		node temp = q.top();q.pop();
		for(int i=0; i<6; ++i){
			node a = temp;
			a.z += dir[i][0];
			a.x += dir[i][1];
			a.y += dir[i][2];
			a.steps++;
			if(a.z == e.z&& a.x==e.x&&a.y==e.y)return a.steps;
			if(map[a.z][a.x][a.y] == '#')continue;
			if(a.z<0 || a.z>=l)continue;
			if(a.x<0 || a.x>=r)continue;
			if(a.y<0 || a.y>=c)continue;
			q.push(a);
			map[a.z][a.x][a.y] = '#';
		}
	}
	return 0;	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值