POJ 2251 Dungeon Master
来源:互联网 发布:唐小僧 坚持以数据和 编辑:程序博客网 时间:2024/04/29 16:01
一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped!
一个三维BFS,一开始直接利用vis数组记录到达当前点的最短时间,不过MLE了,后来改成vis记录是否走过便过了,不解。
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <cmath>#include <vector>#include <set>#include <list>#include <queue>#include <map>using namespace std;#define L(i) i<<1#define R(i) i<<1|1#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-4#define maxn 1000010#define MOD 1000000007char s[33][33][33];int vis[33][33][33],l,r,c;int sx,sy,sz;int mx[6] = {0,0,0,0,1,-1};int my[6] = {0,0,1,-1,0,0};int mz[6] = {1,-1,0,0,0,0};struct node{ int x,y,z,t;}st,ss;void bfs(){ queue<node> q; vis[sx][sy][sz] = 0; st.x = sx; st.y = sy; st.z = sz; //st.t = 0; q.push(st); while(!q.empty()) { st = q.front(); q.pop(); if(s[st.x][st.y][st.z] == 'E') { printf("Escaped in %d minute(s).\n",vis[st.x][st.y][st.z]); return; } //printf("%d %d %d\n",st.x,st.y,st.z); for(int i = 0; i < 6; i++) { int dx = st.x + mx[i]; int dy = st.y + my[i]; int dz = st.z + mz[i]; if(dx < 0 || dx >= l || dy < 0 || dy >= r || dz < 0 || dz >= c) continue; if(s[dx][dy][dz] == '#' || vis[dx][dy][dz] < vis[st.x][st.y][st.z] + 1) continue; vis[dx][dy][dz] = vis[st.x][st.y][st.z] + 1; ss.x = dx; ss.y = dy; ss.z = dz; //ss.t = st.t + 1; q.push(ss); } } printf("Trapped!\n");}int main(){ int t; while(scanf("%d%d%d",&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 i = 0; i < l; i++) for(int j = 0; j < r; j++) for(int k = 0; k < c; k++) { if(s[i][j][k] == 'S') { sx = i; sy = j; sz = k; } vis[i][j][k] = 100000; } bfs(); } 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
- 有关回退事件的区别:history.go()和history.back()
- USACO 5.1.1凸包
- 微信几处比较难的引用
- JavaSE学习小笔记(1)
- IIS中查看W3WP.exe进程对应的应用程序池的方法
- POJ 2251 Dungeon Master
- Android关闭输入法键盘
- install Python 3.3 on Ubuntu 12.04
- 用UITextView加载rtfd文件
- android:configChanges属性描述
- 最熟悉的路,莫过于归途
- 【一】基本概念
- 从零开始学习Git
- Android防止按钮两次点击