POJ2251Dungeon Master(从S走到N,要多少步)

就是很简单很标准的BFS

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int w, h, l;
int sx, sy, sl,el,ex,ey;
bool a[31][31][31];
int ans;
int dir[3][6] = { 0, 0, 0, 0, -1, 1,
-1, 1, 0, 0, 0, 0,
0, 0, -1, 1, 0, 0 };
struct node{
	int l, x, y, lenth;
};
void bfs(int z, int x, int y){
	node s[30000];
	bool visit[31][31][31] = { false };
	int head = 0, tail = 0;
	//printf("z=%d x=%d y=%d\n", z, x, y);
	s[head].x = x;
	s[head].y = y;
	s[head].l = z;
	s[head].lenth = 0;
	tail++;
	visit[l][x][y] = true;
	while (head < tail){
		int nl, nx, ny;
		for (int i = 0; i < 6; i++){
			nl = s[head].l + dir[0][i];
			nx = s[head].x + dir[1][i];
			ny = s[head].y + dir[2][i];
			if (nl < 0 || nl >= l || nx < 0 || nx >= h || ny < 0 || ny >= w)continue;

			if (visit[nl][nx][ny] == false && a[nl][nx][ny] == true){
				//printf("nl=%d nx=%d ny=%d\n", nl, nx, ny);
				s[tail].l = nl;
				s[tail].x = nx;
				s[tail].y = ny;
				s[tail].lenth = s[head].lenth + 1;
				if (nl == el&&nx == ex&&ny == ey){
					ans = ans < s[tail].lenth ? ans : s[tail].lenth;
					return;
				}
				tail++;
				visit[nl][nx][ny] = true;

			}
		}
		//visit[s[head].l][s[head].x][s[head].y] = true;
		head++;
	}
}
int main(){
	while (1){
		//scanf("%d%d%d", &l, &h, &w);
		cin >> l >> h >> w;
		if (l == 0)break;
		char c;
		memset(a, true, sizeof(a));
		//memset(visit, false, sizeof(visit));
		for (int i = 0; i < l; i++){
			for (int j = 0; j < h; j++){
				for (int k = 0; k < w; k++){
					//scanf("%c", &c);
					cin >> c;
					if (c == 'S'){
						sl = i, sx = j, sy = k;
					}
					else if (c == '#'){
						a[i][j][k] = false;
					}
					else if (c == 'E'){
						el = i, ex = j, ey = k;
					}
				}
			}
		}
		/*for (int i = 0; i < l; i++){
			for (int j = 0; j < h; j++){
				for (int k = 0; k < w; k++){
					if (a[i][j][k])printf(".");
					else printf("#");
				}
				printf("\n");
			}
			printf("\n");
		}*/
		ans = 30000;
		bfs(sl, sx, sy);
		if (ans < 30000)printf("Escaped in %d minute(s).\n", ans);
		else printf("Trapped!\n");
	}
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值