题目链接: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 ,又要逃离迷宫了的更多相关文章

  1. hdu 1728:逃离迷宫(DFS,剪枝)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. HDU 1728:逃离迷宫(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description   给定一个m × n (m行, n列)的迷宫,迷宫中有 ...

  3. hdoj 1728 逃离迷宫

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  4. 逃离迷宫(HDU 1728 BFS)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. hdu 4524 郑厂长系列故事——逃离迷宫 小水题

    郑厂长系列故事——逃离迷宫 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  6. HDU 1728 逃离迷宫(DFS||BFS)

    逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...

  7. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  8. HDU 1728 逃离迷宫(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)  ...

  9. 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 ...

  10. HDU 1728 逃离迷宫(DFS经典题,比赛手残写废题)

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 使用js闭包的好处

    使用闭包有以下几大好处: a:希望一个变量长期驻扎在内存中. b:避免全局变量的污染.

  2. Google在百慕大避税几十亿美金,为什么巨头和富豪都会选百慕大避税?

    为什么"越有钱越有钱"?为什么富豪只要不自己"作",就能让自己的财富疯狂增加?除了经营意识之外,关键他们还可以利用自己的资源.实力等去做很多看似让人不齿,但其实 ...

  3. 用 20 行 python 代码实现人脸识别!

    点击上方"Python编程与实战",选择"置顶公众号" 第一时间获取 Python 技术干货! 阅读文本大概需要 11分钟. 今天给大家介绍一个世界上最简洁的人 ...

  4. sms短信服务

    短信服务是app,电商类应用的基础功能.典型场景有: 用户注册,发送验证码 用户找回验证,发送验证码 用户账户异常,发送提示 用户账户变化,通知用户 短信服务开发有几个注意点: 供应商选型 短信模板 ...

  5. [python每日一练]--0012:敏感词过滤 type2

    题目链接:https://github.com/Show-Me-the-Code/show-me-the-code代码github链接:https://github.com/wjsaya/python ...

  6. 关于PHPExcel的一些资料

    下面是总结的几个使用方法 include 'PHPExcel.php'; include 'PHPExcel/Writer/Excel2007.php'; //或者include 'PHPExcel/ ...

  7. Hello World!(这不是第一篇)

    如题,这不是第一篇blog,但是为了表示这个闲置了1年多的blog现在被我正式启用了,我还是走个过场吧. #include <iostream> using namespace std; ...

  8. Linux 信号介绍

    是内容受限时的一种异步通信机制 首先是用来通信的 是异步的 本质上是 int 型的数字编号,早期Unix系统只定义了32种信号,Ret hat7.2支持64种信号,编号0-63(SIGRTMIN=31 ...

  9. 最近做的一个Spring Boot小项目,欢迎大家访问 http://39.97.115.152/

    最近做的一个Spring Boot小项目,欢迎大家访问 http://39.97.115.152/,帮忙找找bug,网站里有源码地址 网站说明 甲壳虫社区(Beetle Community) 一个开源 ...

  10. metrics模块

    class sklearn.metrics 方法 1.分类问题的度量 metrics.accuracy_score metrics.auc metrics.f1_score metrics.preci ...