其他分享
首页 > 其他分享> > POJ - 2251 Dungeon Master【搜索】

POJ - 2251 Dungeon Master【搜索】

作者:互联网

题目链接

题意

BFS

题解

BFS

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
using namespace std;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))
#define mid ((l+r)>>1)
#define mem(x,y) memset(x,y,sizeof x)

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
int f,n,m;
int g[35][35][35];
int s[3],e[3],vis[35][35][35];
int mx[]={1,0,-1,0,0,0},my[]={0,1,0,-1,0,0},mz[]={0,0,0,0,1,-1};
struct node{
	int z,x,y,w;
};
bool check(int z,int x,int y){
	if(vis[z][x][y])return 0;
	if(z<=0||z>f||x<=0||x>n||y<=0||y>m)return 0;
	if(g[z][x][y])return 0;
	return 1;
}
void bfs(){
	vis[s[0]][s[1]][s[2]]=1;
	queue<node>q;
	node w;
	w.z=s[0],w.x=s[1],w.y=s[2],w.w=0; q.push(w);
	while(!q.empty()){
		node w=q.front(); q.pop();
		if(w.z==e[0]&&w.x==e[1]&&w.y==e[2]){
			cout<<"Escaped in "<<w.w<<" minute(s)."<<endl;
			return;
		}
		for(int i=0;i<6;i++){
			node w1=w;
			w1.z+=mz[i],w1.x+=mx[i],w1.y+=my[i],w1.w++;
			if(check(w1.z,w1.x,w1.y)){
				vis[w1.z][w1.x][w1.y]=1;
				q.push(w1); 
			} 
		}
	}
	cout<<"Trapped!"<<endl; 
}
//#define endl '\n'
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	while(cin>>f>>n>>m&&f){
		for(int i=1;i<=f;i++)for(int j=1;j<=n;j++)mem(vis[i][j],0);
		for(int i=1;i<=f;i++)
			for(int j=1;j<=n;j++)
				for(int k=1;k<=m;k++){
					char ch; cin>>ch;
					if(ch=='S')s[0]=i,s[1]=j,s[2]=k;
					if(ch=='E')e[0]=i,e[1]=j,e[2]=k;
					g[i][j][k]=ch=='#';
				}
		bfs();
	}
}

标签:Dungeon,ch,int,35,POJ,w1,Master,include,define
来源: https://blog.csdn.net/qq_50554695/article/details/121165739