poj 2251 bfs基础
来源:互联网 发布:小学生沉迷网络的案例 编辑:程序博客网 时间:2024/06/10 03:35
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
题意:给你一个图,不过就是三维的,然后让你从s走到E,求最短距离
思路就不说了直接bfs,不过就是三维而已
ac代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int x,y,z,vis[50][50][50],c;char a[50][50][50];int cnx[6]= {1,0,-1,0,0,0};int cny[6]= {0,-1,0,1,0,0};int cnz[6]= {0,0,0,0,1,-1};int x1,y1,z1;int x2,y2,z2;struct node{ int xx,yy,zz; int k;};int bfs(int m,int n,int q){ queue<node> que; node head,next; head.xx=m; head.yy=n; head.zz=q; head.k=0; que.push(head); vis[m][n][q]=1; while(!que.empty()) { head=que.front(); que.pop(); if(a[head.xx][head.yy][head.zz]=='E') { return head.k; } for(int i=0;i<6;i++) { int m1=head.xx+cnx[i]; int n1=head.yy+cny[i]; int q1=head.zz+cnz[i]; if(m1<0||m1>=x||n1<0||n1>=y||q1<0||q1>=z||vis[m1][n1][q1]==1||a[m1][n1][q1]=='#') continue; next.xx=m1; next.yy=n1; next.zz=q1; next.k=head.k+1; que.push(next); vis[m1][n1][q1]=1; } } return 0;}int main(){ while(cin>>x>>y>>z) { if(x==0&&y==0&&z==0) break; for(int i=0; i<x; i++) for(int j=0; j<y; j++) for(int k=0; k<z; k++) { cin>>a[i][j][k]; if(a[i][j][k]=='S') { x1=i,y1=j,z1=k; } if(a[i][j][k]=='E') { x2=i,y2=j,z2=k; } } memset(vis,0,sizeof(vis)); int h; h=bfs(x1,y1,z1); if(h==0) cout<<"Trapped!"<<endl; else printf("Escaped in %d minute(s).\n",h); }}
0 0
- poj 2251 bfs基础
- poj 2251 Dungeon Master 【bfs基础题】
- POJ 2251(基础三维BFS)
- POJ 2251 基础搜索 BFS 五
- POJ 迷宫问题+基础bfs
- POJ 2251 Dungeon Master 三维空间BFS(基础题)
- POJ 2251 BFS
- poj 2251 三维BFS
- POJ 2251(BFS)
- POJ 2251 BFS 简单
- poj 2251(简单BFS)
- poj 2251 (bfs)
- poj 2251 bfs
- poj 2251(BFS)
- poj 2251 BFS
- POJ 2251(BFS)
- 【BFS】POJ 2251
- POJ 2251 简单BFS
- Java Web 解决POI导入大批量Excel2007数据报内存溢出问题
- 字符串替换
- Storm原理剖析
- 为树莓派添加 DS1302 实时时钟(硬件时钟)
- django进阶-3
- poj 2251 bfs基础
- Android—App—必备开发组件—调试工具篇—Stetho[配合OkHttp框架使用]
- grep
- nginx 对某个目录或整个网站进行登录认证的方法
- Hive的基本语法(二)
- SpringMVC.xml配置文件 几种配置方法
- NULL字符串和empty字符串的区别
- apriori
- IDEA设置字体大小