POJ 2251Dungeon Master
来源:互联网 发布:头戴式蓝牙推荐 知乎 编辑:程序博客网 时间:2024/06/10 02:47
题目大意:
一个三维迷宫,从起点开始可以向“东南西北,上下”六个方向移动,求由起点到终点的最短路。迷宫表示方法:
“#”:不能通过, “.”:可以通过,“S”为起始位置。“E”为终点。
求最少需要多长时间走到终点(经过每一个点耗时一分钟)
简单思路:
由起点开始向6个方向移动,采用广搜,直到扩展到终。凡最短路问题一般采用广搜bfs。
#include<iostream>#include<stdio.h>#include<cstring>#include<cstdlib>using namespace std;struct q{ int x, y, z; int length;}q[30000]; //模拟队列int xx[] = {1, -1, 0, 0, 0, 0};int yy[] = {0, 0, 0, 0, -1, 1};int zz[] = {0, 0, -1, 1, 0, 0};bool visit[40][40][40];char map [40][40][40];int l, r, c, sx, sy, sz;int bfs(){ int cur, pre, dx, dy, dz; memset(visit, false, sizeof(visit)); q[0].x = sx, q[0].y = sy, q[0].z = sz; pre = cur = 0; while(pre <= cur) { for(int i = 0; i < 6; i++) {//6个方向 dx = q[pre].x + xx[i]; dy = q[pre].y + yy[i]; dz = q[pre].z + zz[i]; if(!visit[dx][dy][dz] && //未访问 (map[dx][dy][dz]=='.' || map[dx][dy][dz]=='E') && //能走 dx>=0 && dx<l && dy>=0 && dy<r && dz>=0 && dz<c) //没越界 {//如果能走,则入队 visit[dx][dy][dz] = true;//访问 cur += 1; q[cur].x = dx; q[cur].y = dy; q[cur].z = dz; q[cur].length = q[pre].length + 1; if(map[dx][dy][dz] == 'E') return q[cur].length; } } pre += 1;//出队 } return 0;}int main(){ int i, j, k, ans;// while(scanf("%d%d%d", &l, &r, &c)) while(scanf("%d%d%d\n", &l, &r, &c)) //这/n把我坑惨了!! { if(l == 0 && r == 0 && c == 0) break; for(i = 0; i < l; i++,getchar()) for(j = 0; j < r; j++,getchar()) for(k = 0; k < c; k++) { scanf("%c", &map[i][j][k]); if(map[i][j][k] == 'S') sx = i, sy = j, sz = k; } ans = bfs(); if(ans) printf("Escaped in %d minute(s).\n", ans);//这里有一个"." else printf("Trapped!\n"); } return 0;}
0 0
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- Poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ-2251-Dungeon Master
- POJ 2251Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- poj 2251 Dungeon Master
- POJ 2251 - Dungeon Master
- POJ 2251 Dungeon Master
- POJ 2251 Dungeon Master
- POJ-2251-Dungeon Master
- poj 2251 Dungeon Master
- 项目从mysql移植到SQL sqlserver问题汇总
- jquery图片轮播,三种方式
- Xcode 报错: value of type InerfaceController does not conform to HKWorkoutSessionDelegate in assignmen
- HDU 1500 Chopsticks(动态规划)
- 基础篇——数论基础
- POJ 2251Dungeon Master
- Web Uploader图片上传 java实现
- VC6.0工程设置
- android监听应用缓存和任务栈顶的方法
- java设计模式:观察者模式实例详解(基于java内置观察者)
- 对单一职责原则的理解
- Method Swizzling(iOS的hook机制)
- 用js根据当前季度获取上一季度
- 查看Linux内核源码技巧的记录