POJ 2251 Dungeon Master(三维BFS求最短路径)
3D dungeon
時間限制:?1 Sec??內(nèi)存限制:?128 MB
提交:?2??解決:?2
[提交][狀態(tài)][討論版][命題人:201506020829][Edit] [TestData]
題目描述
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??
輸入
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.
輸出
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!
樣例輸入
3 4 5 S.... .###. .##.. ###.###### ##### ##.## ##...##### ##### #.### ####E1 3 3 S## #E# ###0 0 0樣例輸出
Escaped in 11 minute(s). Trapped!二維矩陣BFS求最短路徑模板
點擊打開鏈接
三維問題注意廣搜的方向,以及三維矩陣的表示
//POJ 2251 Dungeon Master(三維簡單廣搜) //#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; int l,r,c; int sx,sy,sz; int ans; char s[35][35][35]; bool vis[35][35][35]; struct node{int x,y,z,step; }; bool check(int x,int y,int z) {if(x<0||x>=r||y<0||y>=c||z<0||z>=l||s[z][x][y]=='#'||vis[z][x][y])return 1;return 0; } int dic[6][3]={{0,0,1},{0,1,0},{1,0,0},{-1,0,0},{0,-1,0},{0,0,-1}}; void bfs(){queue<node> q;node start;start.step=0;start.x=sx;start.y=sy;start.z=sz;vis[sz][sx][sy]=1;q.push(start);while(!q.empty()){node now=q.front();q.pop();int x=now.x,y=now.y,z=now.z,step=now.step;vis[z][x][y]=1;if(s[z][x][y]=='E'){ans=step;return;}for(int i=0;i<6;i++){node next=now;next.x+=dic[i][0];next.y+=dic[i][1];next.z+=dic[i][2];if(check(next.x,next.y,next.z))continue;next.step++;vis[next.z][next.x][next.y]=1;q.push(next);}}ans=-1; } int main() {while(cin>>l>>r>>c&&l&&r&&c){for(int i=0;i<l;i++){for(int j=0;j<r;j++){scanf("%s",s[i][j]);for(int k=0;k<c;k++){if(s[i][j][k]=='S'){sz=i;sx=j;sy=k;}}}}memset(vis,0,sizeof(vis));ans=0;bfs();if(ans==-1)cout<<"Trapped!\n";else printf("Escaped in %d minute(s).\n",ans);}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的POJ 2251 Dungeon Master(三维BFS求最短路径)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西安电子科技大学第16届程序设计竞赛G题
- 下一篇: hdu 1421 动态规划