POJ 2251 Dungeon Master(3维BFS)
2017-12-18 21:27
330 查看
Dungeon Master
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
Sample Output
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int i,j,k,n,l,r,c;
int si,sj,sk;
int ni,nj,nk;
char a[40][40][40];
int visited[40][40][40];
int dir[6][3]={{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
struct node
{
int ii,jj,kk;
};
int bfs()
{
int step = 1;
queue<node> q,qq,tmp;
node t,cur,next;
t.ii=si,t.jj=sj,t.kk=sk;
q.push(t);
while(!q.empty())
{
while(!q.empty())
{
cur = q.front();
q.pop();
for(i=0;i<6;i++)
{
ni=cur.ii+dir[i][0];
nj=cur.jj+dir[i][1];
nk=cur.kk+dir[i][2];
if(ni<0||nj<0||nk<=0||ni>=l||nj>=r||nk>=c||visited[ni][nj][nk]==1||a[ni][nj][nk]=='#') continue;
if(a[ni][nj][nk]=='E') return step;
visited[ni][nj][nk]=1;
next.ii=ni,next.jj=nj,next.kk=nk;
qq.push(next);
}
}
tmp = qq;
qq = q;
q = tmp;
step ++;
}
return -1;
}
int main()
{
while(cin>>l>>r>>c && (l+r+c)!=0)
{
for(i=0;i<l;i++)
for(j=0;j<r;j++)
for(k=0;k<c;k++)
{
cin>>a[i][j][k];
if(a[i][j][k]=='S')
si=i,sj=j,sk=k;
}
// 注意要清零
memset(visited, 0, sizeof(visited));
int ret = bfs();
if(ret==-1)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<ret<<" minute(s)."<<endl;
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13187 | Accepted: 5113 |
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!
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int i,j,k,n,l,r,c;
int si,sj,sk;
int ni,nj,nk;
char a[40][40][40];
int visited[40][40][40];
int dir[6][3]={{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
struct node
{
int ii,jj,kk;
};
int bfs()
{
int step = 1;
queue<node> q,qq,tmp;
node t,cur,next;
t.ii=si,t.jj=sj,t.kk=sk;
q.push(t);
while(!q.empty())
{
while(!q.empty())
{
cur = q.front();
q.pop();
for(i=0;i<6;i++)
{
ni=cur.ii+dir[i][0];
nj=cur.jj+dir[i][1];
nk=cur.kk+dir[i][2];
if(ni<0||nj<0||nk<=0||ni>=l||nj>=r||nk>=c||visited[ni][nj][nk]==1||a[ni][nj][nk]=='#') continue;
if(a[ni][nj][nk]=='E') return step;
visited[ni][nj][nk]=1;
next.ii=ni,next.jj=nj,next.kk=nk;
qq.push(next);
}
}
tmp = qq;
qq = q;
q = tmp;
step ++;
}
return -1;
}
int main()
{
while(cin>>l>>r>>c && (l+r+c)!=0)
{
for(i=0;i<l;i++)
for(j=0;j<r;j++)
for(k=0;k<c;k++)
{
cin>>a[i][j][k];
if(a[i][j][k]=='S')
si=i,sj=j,sk=k;
}
// 注意要清零
memset(visited, 0, sizeof(visited));
int ret = bfs();
if(ret==-1)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<ret<<" minute(s)."<<endl;
}
return 0;
}
相关文章推荐
- POJ 2251 Dungeon Master (BFS)
- poj 2251 Dungeon Master(bfs)
- POJ 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master(三维6方向BFS)
- POJ 2251 Dungeon Master(3D迷宫 bfs)
- POJ-2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master【BFS】
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
- poj 2251 Dungeon Master(bfs)
- POJ 2251 Dungeon Master(3D迷宫 bfs)
- POJ 2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master (BFS)
- poj 2251 Dungeon Master(BFS)
- POJ - 2251 Dungeon Master (三维bfs)
- poj-2251-Dungeon Master(bfs)
- poj_2251 Dungeon Master(bfs)
- poj-2251-Dungeon Master【BFS】
- POJ 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master(BFS)
- 算法训练之BFS POJ 2251 Dungeon Master 三维最短路径