地牢逃生(bfs,dfs均可)

题目来源:http://poj.org/problem?id=2251

 

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 

Escaped in x minute(s).


where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line 

Trapped!

Sample Input

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

地图逃生的升级版本(笑)

这个是3d的,但3d,2d都一样,只是加了一个坐标轴就行了;

题目大意:从地牢中逃生:s为初始位置,e为出口,#是石头,点(.)是路(只能走路),可以上下左右前后走,但不能斜着走;

如果能走出去输出最短路,不能就输出trappd!;

走迷宫。。。。

首先是bfs版:

 

#include<stdio.h>
#include<string.h>
#include<math.h>

#include<queue>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
#define da    10000000
#define xiao -10000000
#define clean(a,b) memset(a,b,sizeof(a))
#define max 35000

int l,r,c;
int fz[6]={0,0,0,0,1,-1},fx[6]={-1,0,1,0,0,0},fy[6]={0,-1,0,1,0,0};//方向
int shuzu[35][35][35];
char chuan[35][35][35];

struct dian{
	int x,y,z;
};

int solo(int sz,int sx,int sy,int ez,int ex,int ey)
{
	int time=0;
	int i=0,j;
	dian p;
	p.x=sx;
	p.y=sy;
	p.z=sz;
	queue<dian> que;
	que.push(p);
	shuzu[sz][sx][sy]=0;
	while(que.size())
	{
		dian s=que.front();
		que.pop();
		if(s.x==ex&&s.y==ey&&s.z==ez)
			break;
		for(i=0;i<6;++i)
		{
			int x=s.x+fx[i],y=s.y+fy[i],z=s.z+fz[i];
			if(x>=1&&x<=r&&z>=1&&z<=l&&y>=1&&y<=c)//在迷宫 
			{
				if(chuan[z][x][y]!='#')//不是石头
				{
					if(shuzu[z][x][y]==da)//没走过
					{
						shuzu[z][x][y]=shuzu[s.z][s.x][s.y]+1;
						s.x=x;
						s.y=y;
						s.z=z;
						que.push(s);
						s.x=x-fx[i];
						s.y=y-fy[i];
						s.z=z-fz[i];
					}
				}
			}
		}
	}
	return shuzu[ez][ex][ey];
	
}

int main()
{
	while(~scanf("%d%d%d",&l,&r,&c))
	{
		if(l==0&&r==0&&c==0)
			break;
		int i,j,z,sz,sx,sy,ez,ex,ey;
		for(z=1;z<=l;++z)
		{
			for(i=1;i<=r;++i)
			{
				
				for(j=1;j<=c;++j)
				{
					cin>>chuan[z][i][j];//scnaf("%s",&chuan[z][i][j]);
					if(chuan[z][i][j]=='S')
						sz=z,sx=i,sy=j;
					else if(chuan[z][i][j]=='E')
						ez=z,ex=i,ey=j;
				}
			}
		}
		for(z=1;z<=l;++z)
		{
			for(i=1;i<=r;++i)
			{
				for(j=1;j<=c;++j)
					shuzu[z][i][j]=da;
			}
		}
		int sev=solo(sz,sx,sy,ez,ex,ey);
		if(sev==da)
			printf("Trapped!\n");
		else
			printf("Escaped in %d minute(s).\n",sev);	
	}
	
}

 

 

然后是dfs版:其实这两者都能写走迷宫的题

 

 

 

 

#include<stdio.h>
#include<string.h>
#include<math.h>

#include<queue>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

#define ll long long
#define da    10000000
#define xiao -10000000
#define clean(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f

char map[50][50][50];
int fx[6]={0,0,-1,1,0,0},fy[6]={1,-1,0,0,0,0},fz[6]={0,0,0,0,1,-1};		//有六个方向可以选择 

int l,r,c;
int sum=0,f=0,bu;

void dfs(int sx,int sy,int sz,int ex,int ey,int ez)
{
	map[sz][sy][sx]='#';							//第一次经过的必定是最短的 
	if(sz==ez&&sx==ex&&sy==ey)
	{
		f=1;										//找到标记 
		bu=sum;										//记录步数 
		return ;
	}
	int i,j,k;
	for(i=0;i<6;++i)								//每个方向都试一次 
	{
		int nx=sx+fx[i];							//记录x 
		int ny=sy+fy[i];							//记录y 
		int nz=sz+fz[i];							//记录z 
		if(nx>=1&&nx<=c&&ny>=1&&ny<=r&&nz>=1&&nz<=l)//在地牢中 
		{
			if(map[nz][ny][nx]!='#')				//这个方向不是墙 
			{
				sum++;								//步数+1 
				dfs(nx,ny,nz,ex,ey,ez);				//再次寻找 
				sum--;								//倒回去,步数-1; 
			}
		}
	}
	
}

int main()
{
	int i,j,z;
	while(~scanf("%d%d%d",&l,&r,&c))
	{
		if(l==0&&r==0&&c==0)
			break;
		clean(map,'\0');
		f=0;
		sum=0;
		int sx,sy,sz,ex,ey,ez;
		for(i=1;i<=l;++i)								//楼层 
		{
			for(j=1;j<=r;++j)							//y轴 
			{
				for(z=1;z<=c;++z)						//x轴 
				{
					cin>>map[i][j][z];					//输入 
					if(map[i][j][z]=='S')				//找初始点 
					{
						sx=z;
						sy=j;
						sz=i;
					}
					else if(map[i][j][z]=='E')			//找出口 
					{
						ex=z;
						ey=j;
						ez=i;
					}
				}
			}
		}
		dfs(sx,sy,sz,ex,ey,ez);							//传入s与e 
		if(f)
			printf("Escaped in %d minute(s).n",bu);		//找到 
		else
			printf("Trapped!\n");						//未找到 	
	}
}

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值