UVA532

题意:走3D迷宫,找到出口

思想:BFS,六个方向一起搜

#include<iostream>
#include<stdio.h>
#include<string.h>
#define N 50 
using namespace std;

int vis[N][N][N], Map[N][N][N];
char map[N][N][N];
int a, n, m, num, c1, c2, c3, d1, d2, d3, flag;
int dz[] = {0, 0, 0, 0, -1, 1};
int dx[] = {-1, 1, 0, 0, 0, 0};
int dy[] = {0, 0, -1, 1, 0, 0};

typedef struct space{
	int z, x, y, d;
};

void bfs(int z, int x, int y){

	int front = 0, near = 1;	
	space s[30000];
	memset(s, 0, sizeof(s));	
	s[0].z = z;
	s[0].x = x;
	s[0].y = y;
	s[0].d = 0;
	vis[z][x][y] = 1;	
	while(front < near){
		for(int i = 0; i < 6; i++)	
		{
			int tz = s[front].z + dz[i];	
			int tx = s[front].x + dx[i];	
			int ty = s[front].y + dy[i];	
			if(Map[tz][tx][ty]){	
				if(tz >= 0 && tz < a && tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tz][tx][ty])	
				{	if(tz == c1 && tx == c2 && ty == c3)	
					{
						num = s[front].d + 1;		
						flag = 0;	
						return;
					}		
				
				vis[tz][tx][ty] = 1;	
				s[near].z = tz;
				s[near].x = tx;
				s[near].y = ty;	
				s[near].d = s[front].d + 1;	
				near++;
				}	
				
			}

		}	
		front++;	
	}

}

int main(){

	while(cin>>a>>n>>m && a + n + m){
		flag = 1;		
		memset(vis, 0, sizeof(vis));	
		memset(Map, 0, sizeof(Map));	
		for(int i = 0; i < a; i++)	
			for(int j = 0; j < n; j++)	
				for(int k = 0; k < m; k++)	
					cin>>map[i][j][k];	
			
		
		for(int i = 0; i < a; i++)		
			for(int j = 0; j < n; j++)		
				for(int k = 0; k < m; k++)			
				{	
					if(map[i][j][k] == 'E'){
						c1 = i;		
						c2 = j;		
						c3 = k;			
						Map[i][j][k] = 1;	
					}		
					else if(map[i][j][k] == 'S')				
					{		
						Map[i][j][k] = 1;	
						d1 = i;
						d2 = j;
						d3 = k;		
					}	
					else if(map[i][j][k] == '.')	
						Map[i][j][k] = 1;				
				}	
		
		if(c1 != d1 || c2 != d2 || c3 != d3)	
			bfs(d1, d2, d3);	
		
		if(flag)	
			printf("Trapped!\n");	
		else
			printf("Escaped in %d minute(s).\n", num);	
	}	

	return 0;
}




















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值