hdu1026Ignatius and the Princess ,又要逃离迷宫了
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1026/
题意就是一个迷宫,然后有些位置上有守卫,守卫有一个血量,要多花费血量的时间击败他,要求从(0,0)到(n-1,m-1)的最少用时。显然是用优先队列和bfs实现,因为求解图中每一层结点各自的时间不同,所以我们必须选出时间短的。还有就是递归打印路径的问题,注意一下就好。
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define pf printf
#define mem(a,b) memset(a,b,sizeof(a))
#define prime1 1e9+7
#define prime2 1e9+9
#define pi 3.14159265
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define scand(x) scanf("%llf",&x)
#define f(i,a,b) for(int i=a;i<=b;i++)
#define scan(a) scanf("%d",&a)
#define dbg(args) cout<<#args<<":"<<args<<endl;
#define inf 0x3f3f3f3f
#define maxn 105
int n,m,t;
int Map[maxn][maxn];
int dir[][]={,,,-,,,-,};
int flag[maxn][maxn];
bool vis[maxn][maxn];
struct node{
int x,y,time;
node(int x,int y,int t):x(x),y(y),time(t){}
node(){}
friend bool operator < ( const node& a,const node& b)
{
return a.time>b.time;//反向定义操作符,堆顶为time最小的node
}
};
node cur,nxt;
int bfs()
{
priority_queue<node>q;
q.push(node(,,));
vis[][]=;
while(!q.empty())
{
cur=q.top();
if(cur.x==n-&&cur.y==m-)return cur.time;
q.pop();
f(i,,)
{
nxt=cur;
nxt.x+=dir[i][];
nxt.y+=dir[i][];
nxt.time++;
if(nxt.x<||nxt.x>=n||nxt.y<||nxt.y>=m||Map[nxt.x][nxt.y]==-)continue;
if(!vis[nxt.x][nxt.y])
{
vis[nxt.x][nxt.y]=;
flag[nxt.x][nxt.y]=i+;//保证起点为0,扩展结点方向为大于1
if(Map[nxt.x][nxt.y])nxt.time+=Map[nxt.x][nxt.y];
q.push(nxt);
}
}
}
return -;
}
void print(int x,int y)
{
if(!flag[x][y])return;
int px=x-dir[flag[x][y]-][];
int py=y-dir[flag[x][y]-][];
print(px,py);//递归打印
pf("%ds:(%d,%d)->(%d,%d)\n",++t,px,py,x,y);
while(Map[x][y]--)
pf("%ds:FIGHT AT (%d,%d)\n",++t,x,y);
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
std::ios::sync_with_stdio(false);
while(scanf("%d%d",&n,&m)!=EOF)
{
char c;
mem(vis,false);
mem(flag,);
f(i,,n-)
f(j,,m-)
{
scanf(" %c",&c);
if(c=='X')Map[i][j]=-;//将字符地图转化成数字地图
else if(c=='.')Map[i][j]=;
else Map[i][j]=c-'';
}
int ans=bfs();
if(ans==-)
{
pf("God please help our poor hero.\n");
}
else
{
pf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
t=;
print(n-,m-);
}
pf("FINISH\n");
}
}
hdu1026Ignatius and the Princess ,又要逃离迷宫了的更多相关文章
- hdu 1728:逃离迷宫(DFS,剪枝)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- hdoj 1728 逃离迷宫
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 逃离迷宫(HDU 1728 BFS)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 4524 郑厂长系列故事——逃离迷宫 小水题
郑厂长系列故事——逃离迷宫 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- HDU 1728 逃离迷宫(DFS||BFS)
逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...
- hdu 1728 逃离迷宫 (BFS)
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
- HDU 1728 逃离迷宫(DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
随机推荐
- Python---10小结
因一边上班一边自学python,一旦忙起来,python就会放两天,可是2天后之前学的内容就会有点忘记. 今天把python的各种启动方法总结一下; 我的文档路径: ------- 1打开文件所在的c ...
- Oil Deposits(油田)(DFS)
题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...
- [python每日一练]--0012:敏感词过滤 type2
题目链接:https://github.com/Show-Me-the-Code/show-me-the-code代码github链接:https://github.com/wjsaya/python ...
- Dream权限追踪系统<=2.0.1 重安装漏洞
在./install/install.php中 if(file_exists('lock.txt')){ echo '系统已安装,请不要重复安装!如需安装,请删除install文件夹下的lock.tx ...
- swap和shm的区别
在使用docker的过程中,发现其有很多内存相关的命令,对其中的swap(交换内存)和shm(共享内存)尤其费解.于是查阅了一些资料,弄明白了二者的基本区别. swap 是一个文件,是使用硬盘空间的一 ...
- C++走向远洋——26(项目二,2,构造函数与析构函数)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:game.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- 一文看懂js中元素的客户区大小(clientWidth,clientHeight)
元素的客户区 元素的客户区大小,指的是元素内容及其内边距所占据的空间大小. 相关属性如下: 1. clientWidth:元素内容区宽度+元素左右内边距 2. clientHeight:元素内容区高度 ...
- 【Python3】HTML基础
[web前端]HTML基础 一.BS模式 BS(Browser-Server)模式:顾名思义为浏览器-服务器的意思,对比的话类似我们PC上面浏览器使用的产品即为BS模式产品,例如google doc. ...
- 《第31天:JQuery - 轮播图》
源码下载地址:链接:https://pan.baidu.com/s/16K9I... 提取码:0ua2 写这篇文章,当做是对自已这一天的一个总结.写轮播图要准备的东西:三张尺寸大小一样的图片.分为三个 ...
- LeetCode 153.Find Minimum in Rotated Sorted Array(M)(P)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...