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 ...
随机推荐
- haskell处理JSON(aeson)
aeson是haskell的一个库,其实我也不太懂,不过大概是这样的: 定义一个类型 如 data Person = Person { firstName :: String , lastNa ...
- .apache.commons.io 源代码学习(二)FilenameUtils类
FilenameUtils是apache common io中一个独立的工具类,对其他没有依赖,看其源代码的import即可知道. import java.io.File;import java.io ...
- Laravel中ajax添加CsrfToken的方法
//在模板文件的header头中添加 <meta name="_token" content="{{ csrf_token() }}"/> //aj ...
- HDU 2955 【01背包/小数/概率DP】
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- ZOJ 3949 (17th 浙大校赛 B题,树型DP)
题目链接 The 17th Zhejiang University Programming Contest Problem B 题意 给定一棵树,现在要加一条连接$1$(根结点)和$x$的边,求加 ...
- 关于Date数据类型格式化的转换
例如: jsp页面读取数据库中日期格式的列时可能显示为1988-05-03 00:00:00 格式,但是我们不想要后面的00:00:00时间,只想要前面的年月日,那么该如何做出修改呢? 方法一:我 ...
- csu1808
csu1808 题意 n 个点间有 m 条地铁,每条地铁可能属于不同的线路,每条地铁有权值即通过时花费的时间,如果乘坐第 i 条地铁来到地铁站 s,再乘坐第 j 条地铁离开,需要花费额外的时间 \(| ...
- POJ2955 Brackets(区间DP)
给一个括号序列,求有几个括号是匹配的. dp[i][j]表示序列[i,j]的匹配数 dp[i][j]=dp[i+1][j-1]+2(括号i和括号j匹配) dp[i][j]=max(dp[i][k]+d ...
- Linux下Shell的复制粘贴快捷键
[Shift]+[Insert]:复制 [Ctrl]+[Insert]:粘贴
- Markdown中超链接增加_blank的方法
很遗憾,无法在语法上实现,只能通过额外的的JS代码实现,比如: var links = document.links; for (var i = 0; i < links.length; i++ ...