Dungeon Master ZOJ 1940【优先队列+广搜】
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?
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.
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....
.###.
.##..
###.# #####
#####
##.##
##... #####
#####
#.###
####E 1 3 3
S##
#E#
### 0 0 0
Escaped in 11 minute(s).
Trapped!
//一次AC。感觉太爽了。!
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[40][40][40];
int x1,y1,z1,x2,y2,z2;
int dx[]={0,1,0,-1,0,0};
int dy[]={1,0,-1,0,0,0};
int dz[]={0,0,0,0,1,-1};
int n,row,column;
struct Dungeon//地牢
{
int x,y,z;
int time;
friend bool operator < (Dungeon a,Dungeon b)
{
return a.time>b.time;
}
}; bool judge(int xt,int yt,int zt)
{
if(xt>row||xt<1||yt>column||yt<1||zt>n||zt<1) //越界
return 0;
if(map[zt][xt][yt]=='#')
return 0;
return 1;
}
int BFS()
{
priority_queue<Dungeon>q;
Dungeon pos,next;
pos.x=x1;
pos.y=y1;
pos.z=z1;
pos.time=0;
map[z1][x1][y1]='#';
q.push(pos);
while(!q.empty())
{
pos=q.top();
q.pop();
for(int i=0;i<6;++i)
{
next.x=pos.x+dx[i];
next.y=pos.y+dy[i];
next.z=pos.z+dz[i];
next.time=pos.time+1;
if(judge(next.x,next.y,next.z))
{
if(next.x==x2&&next.y==y2&&next.z==z2)
{
return next.time;
}
map[next.z][next.x][next.y]='#';
q.push(next);
}
}
}
return -1;
}
int main()
{ while(~scanf("%d%d%d",&n,&row,&column),n+row+column)
{
getchar();
for(int i=1;i<=n;++i)
{
for(int j=1;j<=row;++j)
{
for(int k=1;k<=column;++k)
{
scanf("%c",map[i][j]+k);
if(map[i][j][k]=='S')
{
z1=i,x1=j,y1=k;
}
else if(map[i][j][k]=='E')
{
z2=i,x2=j,y2=k;
}
}
getchar();
}
getchar();
}
int res;
res=BFS();
if(res==-1) printf("Trapped!\n");
else printf("Escaped in %d minute(s).\n",res);
}
return 0;
}
Dungeon Master ZOJ 1940【优先队列+广搜】的更多相关文章
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索) Description You ar ...
- 【POJ - 2251】Dungeon Master (bfs+优先队列)
Dungeon Master Descriptions: You are trapped in a 3D dungeon and need to find the quickest way out! ...
- USACO Milk Routing /// 优先队列广搜
题目大意: 在n个点 m条边的无向图中 需要运送X单位牛奶 每条边有隐患L和容量C 则这条边上花费时间为 L+X/C 求从点1到点n的最小花费 优先队列维护 L+X/C 最小 广搜到点n #inclu ...
- nyoj 1022 最少步数【优先队列+广搜】
最少步数 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 这有一个迷宫,有0~8行和0~8列: 1,1,1,1,1,1,1,1,1 1,0,0,1,0,0,1,0,1 ...
- [题解](优先队列广搜)POJ_3635_Full Tank
用二元组$(city,fuel)$即可记录所有状态,以当前花费为关键字优先队列,开数组记录直接做即可 有一个点在于每次不用枚举所有的加油数量,只需要加一即可,因为如果在加一升更优的话又会扩展出加更多油 ...
- ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 A.Saving Tang Monk II(优先队列广搜)
#include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN]; ]; int n, m, sx, sy, ex, ...
- hrbust 1621 迷宫问题II 广搜
题目链接:http://acm.hrbust.edu.cn/vj/index.php?/vj/index.php?c=&c=contest-contest&cid=134#proble ...
- hdu5025 状态压缩广搜
题意: 悟空要救唐僧,中途有最多就把钥匙,和最多五条蛇,要求就得唐僧并且拿到所有种类的钥匙(两个1只拿一个就行),拿钥匙i之前必须拿到钥匙i-1,打蛇多花费一秒,问救出唐僧并且拿到所有种类 ...
- POJ 2251 Dungeon Master(广搜,三维,简单)
题目 简单的3d广搜,做法类似与 hdu 的 胜利大逃亡 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<str ...
随机推荐
- python每日一类(3):os和sys
os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...
- hdu 5163(前缀和+分类讨论)
Taking Bus Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Guava源码学习(二)Ordering
基于版本:Guava 22.0 Wiki:Ordering 0. Ordering简介 Guava的Ordering提供了链式风格的比较器的实现,我们可以用Ordering轻松构建复杂的比较器. 1. ...
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table【递推】
A. Maximum in Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- poj2728(最小比率生成树)
poj2728 题意 给出 n 个点的坐标和它的高度,求一颗生成树使得树上所连边的两点高度差之和除以距离之和最小. 分析 01分数规划+最小生成树. 对于所有的边,在求最小生成树过程中有选或不选的问题 ...
- JSON 中的 key
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript(Standard ECMA-262 ...
- [COCI2015]TRAKTOR
题目大意: 一个$X\times Y(X,Y\leq10^5)$的格子中,每秒钟依次$n(n\leq10^6)$个蘑菇, 告诉你每个蘑菇出现的时间和位置,问何时第一次出现$k(2\leq k\leq ...
- Android的数据存储方式概述
数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 ...
- Android - toolbar navigation 样式
1.修改title 边距 修改边距使用系统的app属性来引入使用,即: xmlns:app="http://schemas.android.com/apk/res-auto" 1 ...
- eclipse 启动报错 java was started but returned code=13
eclipse启动不了,出现“Java was started but returned exit code=13......”对话框如下 我的解决方法是:去控制面板--程序--卸载程序和功能下面查看 ...