【B - Dungeon Master 】

80 篇文章 0 订阅
80 篇文章 0 订阅

思路:

  • 正常bfs。
  • 优先队列,movetovis,不递归,不传参。
    构造函数。
    出现死循环一般是 vis 没置 true

代码:

  • 47ms 788kB
//47ms		788kB


#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

const int maxn = 35;

struct node{
	int x,y;
	int z;
	int deep;
	node(int x,int y,int z,int deep) : x(x),y(y),z(z),deep(deep) {};
	friend bool operator > (node a , node b)
	{
		return a.deep > b.deep ;
	}
};
priority_queue <node , vector<node> , greater<node> > Q;
int L,R,C;
int stx,sty,stz;
bool ok;
int ans;
char mp [maxn][maxn][maxn];
bool vis[maxn][maxn][maxn];
int moveto[6][3] = {1,0,0 , -1,0,0 , 0,1,0 , 0,-1,0 , 0,0,1 , 0,0,-1}; 

void init(){
	while(!Q.empty())
		Q.pop();
	memset(vis,false,sizeof(vis));
	ans = 0;
	return ;
}

void input(){
	for(int k=1;k<=L;k++)
		for(int i=1;i<=R;i++)
			for(int j=1;j<=C;j++){
				cin>>mp[i][j][k];
				if(mp[i][j][k] == 'S')
					stx = i , sty = j , stz = k;
			}
	return ;
}

bool check(int x,int y,int z){
	if(vis[x][y][z])
		return false;
	if(x < 1 || y < 1 || z < 1 || x > R || y > C || z > L)
		return false;
	if(mp[x][y][z] == '#')
		return false;
	return true;
}

void bfs(){
	Q.push(node(stx,sty,stz,0));
	while(!Q.empty()){
		node n = Q.top() ; Q.pop() ;
		int nx , ny , nz , nd;
		for(int i=0;i<6;i++){
			nx = n.x + moveto[i][0];
			ny = n.y + moveto[i][1];
			nz = n.z + moveto[i][2];
			nd = n.deep + 1;
			if(mp[nx][ny][nz] == 'E'){
				ans = nd;
				break;
			}
			if(check(nx,ny,nz)){
				Q.push(node(nx,ny,nz,nd));
				vis[nx][ny][nz] = true;
			}
		}
		if(ans)
			break;
	}
	return ;
}

int main(){
	while(true){
		cin>>L>>R>>C;
		if(L + R + C == 0)
			break;
		init();
		input();
		bfs();
		if(ans)
			cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
		else
			cout<<"Trapped!"<<endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值