POJ - 2251 Dungeon Master

1.题面

http://poj.org/problem?id=2251

2.题意

没题意

3.思路

bfs搜索

4.代码

/*****************************************************************
    > File Name: tst.cpp
    > Author: Uncle_Sugar
    > Mail: uncle_sugar@qq.com
    > Created Time: 2016年07月25日 星期一 18时43分23秒
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

template<class T>void PrintArray(T* first,T* last,char delim=' '){ 
	for (;first!=last;first++) cout << *first << (first+1==last?'\n':delim); 
}

const int debug = 1;
const int size  = 10 + 30; 
const int INF = INT_MAX>>1;
typedef long long ll;

char g[size][size][size];
int vis[size][size][size];
int l,r,c;

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

struct Coord{
	int x,y,z;
	Coord(){}
	Coord(int _x,int _y,int _z):x(_x),y(_y),z(_z){}
}coord[size];

Coord s,e;

bool inrange(int x,int y,int z){
	return x<l&&x>=0&&y<r&&y>=0&&z<c&&z>=0;
}

int main()
{
	std::ios::sync_with_stdio(false);cin.tie(0);
	int i,j,k;
	queue<Coord> que;	
	while (cin >> l >> r >> c){
		if (l==0&&r==0&&c==0) break;
		while (!que.empty()) que.pop();
		memset(vis,-1,sizeof(vis));
		for (i=0;i<l;i++){
			for (j=0;j<r;j++){
				cin >> g[i][j];
				for (k=0;k<c;k++){
					if (g[i][j][k]=='S'){
						s = Coord(i,j,k);
						g[i][j][k] = '.';
					}
					if (g[i][j][k]=='E'){
						e = Coord(i,j,k);
						g[i][j][k] = '.';
					}
				}
			}
		}
		que.push(s);
		vis[s.x][s.y][s.z] = 0;
		while (!que.empty()){
			Coord T = que.front();que.pop();
			for (i=0;i<6;i++){
				int nx = T.x + dx[i];		
				int ny = T.y + dy[i];
				int nz = T.z + dz[i];
				if (inrange(nx,ny,nz)&&g[nx][ny][nz]!='#'&&vis[nx][ny][nz]==-1){
					vis[nx][ny][nz] = vis[T.x][T.y][T.z] + 1;	
					que.push(Coord(nx,ny,nz));
				}
			}
			if (vis[e.x][e.y][e.z]!=-1) break;
		}
		if (vis[e.x][e.y][e.z]!=-1)
			cout << "Escaped in " << vis[e.x][e.y][e.z] << " 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、付费专栏及课程。

余额充值