三维搜索(bfs)Dungeon Master
2016-02-25 17:14
344 查看
1.学会开三维数组保存空间的状态2.会用bfs搜索空间3.还有学会结构体的构造函数struct node {int vx, vy, vz;node( int x, int y, int y) : vx(x), vy(y), vy(z) { }};DescriptionYou 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. Youcannot move diagonally and the maze is surrounded by solid rock on all sides.Is an escape possible? If yes, how long will it take?InputThe 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 theexit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.OutputEach maze generates one line of output. If it is possible to reach the exit, print a line of the formEscaped 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 lineTrapped!Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0Sample Output
Escaped in 11 minute(s). Trapped!
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <queue>#include <set>#include <map>#include <algorithm>using namespace std;typedef pair<int, int> P;//#define LOCAL#define INF 0x3f3f3f3f#define MAX_N 31struct node {int vx, vy, vz;node(int x, int y, int z):vx(x), vy(y), vz(z){}};char maze[MAX_N][MAX_N][MAX_N];int ans[MAX_N][MAX_N][MAX_N];int vis[MAX_N][MAX_N][MAX_N];int L, R, C;//空间的高,行,列int sx, sy, sz, ex, ey, ez;//起点终点坐标int xyz[6][3] = {{0,1,0}, {1,0,0}, {0,-1,0}, {-1,0,0}, {0,0,1}, {0,0,-1}};bool bfs(){bool ans2 = 0;queue<node> que;que.push(node(sx, sy, sz));ans[sx][sy][sz] = 0;vis[sx][sy][sz] = INF;while(!que.empty()){// system("pause");node now = que.front();que.pop();// cout << now.vx << " " << now.vy << " " << now.vz << endl;if(now.vx == ex && now.vy == ey && now.vz == ez) {ans2 = 1;break;}for(int i = 0; i < 6; i++) {int nx = now.vx + xyz[i][0], ny = now.vy + xyz[i][1], nz = now.vz + xyz[i][2];//转移if(nx >= 0 && nx < R && ny >= 0 && ny < C && nz >= 0 && nz < L && maze[nx][ny][nz] != '#' && vis[nx][ny][nz] != INF) {que.push(node(nx, ny, nz));vis[nx][ny][nz] = INF;ans[nx][ny][nz] = ans[now.vx][now.vy][now.vz] + 1;}}}return ans2;}int main(){#ifdef LOCALfreopen("b:\\data.in.txt", "r", stdin);#endifwhile(scanf("%d%d%d", &L, &R, &C)){if(!L && !R && !C)break;for(int z = 0; z < L; z++)for(int x = 0; x < R; x++)for(int y = 0; y < C; y++) {cin >> maze[x][y][z];if(maze[x][y][z] == 'S') {sx = x, sy = y, sz = z;}if(maze[x][y][z] == 'E') {ex = x, ey = y, ez = z;}}// cout << sx << " " << sy << " " << sz << endl;// cout << ex << " " << ey << " " << ez << endl;// for(int z = 0; z < L; z++)// for(int x = 0; x < R; x++)// for(int y = 0; y < C; y++)// {// cout << maze[x][y][z] << " ";// if( y == C - 1)// cout << endl;// }memset(ans, 0, sizeof(ans));memset(vis, 0, sizeof(vis));int can = bfs();if(can)cout << "Escaped in " << ans[ex][ey][ez] << " minute(s)."<< endl;elsecout << "Trapped!" << endl;}return 0;}
相关文章推荐
- Android 简单demo kill外部应用进程、卸载外部应用、安装apk
- 前端组件化框架之路
- centos minimal 网络配置详细解读
- qt学习
- Python分析NGINX LOG版本二
- 在iPhone上展示哈夫曼二叉树
- globalZOrder()与localZOrder()
- linux设备驱动归纳总结(六):1.中断的实现
- 视频的中断退出
- UITableView属性及方法大全
- 解决apache启动错误"httpd:Could not reliably determine
- 大型网站架构系列:电商网站架构案例(1)
- 类 的默认拷贝构造是浅拷贝。 若要深拷贝,则需要自己重写拷贝构造函数。
- Web开发者推荐的最佳HTML5/CSS3代码生成器
- Android项目:手机安全卫士(2)—— 版本升级
- (26)odoo中的序列运用
- SSO单点登录
- 想更好的实现DevOps吗?不妨试试ChatOps
- UIModalPresentationStyle和UIModalTransitionStyle
- 基于HTML5 Ajax实现文件上传并显示进度条