poj2251 三維簡單BFS


D - (熱身)簡單寬搜回顧
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status

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!
解析見代碼:

/*
三維BFS,尋找最短路
注意事項:首先是初始化,每輸入一組數據記得要先將隊列清空
另外要注意因為字符輸入比較多,還多有空行,一定要把必要的回車符吃掉
三維的BFS和二維的並沒有多少區,就是標記數組和地圖數組開成三維的就可以
狀態的轉移,從一個點因為是三維的,本來應該是有6個移動方向,但是因為出口肯定是在
上面,所以沒有必要往下走
*/
#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
bool visit[35][35][35];
char map[35][35][35];
int flag;
int l,r,c;
struct node
{
int x,y,z;
int s;
};
queue<node> q;
void init()
{
while(!q.empty())
q.pop();
memset(visit,false,sizeof(visit));
memset(map,'.',sizeof(map));
flag=0;
}
int main()
{
int i,j,k;
char ch;
while(scanf("%d%d%d",&l,&r,&c)&&(l||r||c))
{
init();
getchar();//吃回車
node start;
for(i=0;i<l;i++)
{ for(j=0;j<r;j++)
{ for(k=0;k<c;k++)//三維的輸入
{
cin>>ch;
map[i][j][k]=ch;
if(ch=='S') //記錄起點位置
{
start.x=i;
start.y=j;
start.z=k;
start.s=0;
}
}
getchar();
}
getchar();
}
q.push(start);
visit[start.x][start.y][start.z]=1;//標記
while(!q.empty())
{
node m=q.front();
q.pop();
if(map[m.x][m.y][m.z]=='E')
{
printf("Escaped in %d minute(s).\n",m.s);
flag=1;//已經找到
break;
}
m.s++;
node m2;
if(m.x-1>=0)
{
m2=m;
m2.x--;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.x+1<l)
{
m2=m;
m2.x++;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.y-1>=0)
{
m2=m;
m2.y--;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.y+1<r)
{
m2=m;
m2.y++;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.z-1>=0)
{
m2=m;
m2.z--;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
if(m.z+1<c)//向上走,不會向下走
{
m2=m;
m2.z++;
if(visit[m2.x][m2.y][m2.z]==0&&map[m2.x][m2.y][m2.z]!='#')
{ visit[m2.x][m2.y][m2.z]=1;
q.push(m2);
}
}
}
if(!flag)
puts("Trapped!");
}
return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM