hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1026
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21841 Accepted Submission(s): 7023
Special Judge
1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.
Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
#include<bits/stdc++.h>
using namespace std;
#define max_v 105
#define INF 99999999
struct node
{
int x,y;
int sum;
friend bool operator<(const node &a,const node &b)
{
return a.sum>b.sum;
}
};
struct nn
{
int x,y;
}pre[max_v][max_v];
int f[][]={{,},{-,},{,},{,-}};
char s[max_v][max_v];
int ptr[max_v][max_v];
priority_queue <node> Q;
int n,m,ans;
int bfs()
{
int i;
while(!Q.empty())
{
Q.pop();
}
node temp,tx;
temp.x=;
temp.y=;
temp.sum=;
ptr[][]=;
Q.push(temp);
while(!Q.empty())
{
node t=Q.top();
Q.pop();
if(t.x==n-&&t.y==m-)
{
ans=t.sum;
return ;
}
for(i=;i<;i++)
{
int a=t.x+f[i][];
int b=t.y+f[i][];
if(a>=&&a<n&&b>=&&b<m&&s[a][b]!='X')
{
tx.x=a;
tx.y=b;
tx.sum=t.sum+;
if(s[a][b]!='.')
tx.sum+=s[a][b]-'';
if(ptr[a][b]>tx.sum)
{
ptr[a][b]=tx.sum;
pre[a][b].x=t.x;
pre[a][b].y=t.y;
Q.push(tx);
}
}
}
}
return ;
}
void pf(int x,int y)
{
int i;
if(x==&&y==)
return ;
pf(pre[x][y].x,pre[x][y].y);
printf("%ds:(%d,%d)->(%d,%d)\n",ptr[pre[x][y].x][pre[x][y].y]+,pre[x][y].x,pre[x][y].y,x,y);
if(s[x][y]!='.')
{
for(i=;i<=s[x][y]-'';i++)
{
printf("%ds:FIGHT AT (%d,%d)\n",ptr[pre[x][y].x][pre[x][y].y]++i,x,y);
}
}
}
int main()
{
int i,j;
while(~scanf("%d %d",&n,&m))
{
for(i=;i<n;i++)
{
scanf("%s",s[i]);
}
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
ptr[i][j]=INF;
}
}
if(bfs())
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",ans);
pf(n-,m-);
}else
{
printf("God please help our poor hero.\n");
}
printf("FINISH\n");
}
return ;
}
hdu 1026 Ignatius and the Princess I(BFS+优先队列)的更多相关文章
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu 1026 Ignatius and the Princess I(bfs)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu1026.Ignatius and the Princess I(bfs + 优先队列)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
随机推荐
- Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(一:知识点回顾)
一.知识点回顾 1.Mybatis环境搭建(DAO层的实现)(使用maven项目管理工具) 需要引入的依赖包: <!-- 单元测试junit --> <dependency> ...
- Javascript之for循环该注意的问题
很多时候我们都用到for循环,而用到for循环部门往往对一个数组进行循环,其中我们很多时候都是这样写的: // 次佳的循环 for (var i = 0; i < myarray.length; ...
- fzu 2138 久违的月赛之一 容斥。
Problem 2138 久违的月赛之一 Accept: 40 Submit: 86 Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- Linux 更新python至2.7后ImportError: No module named _ssl
原文:http://blog.51cto.com/hunt1574/1630961 编译安装python 2.7后无法导入ssl包 解决办法: 1 下载地址:http://www.openssl.or ...
- 重构指南 - 分解复杂判断(Remove Arrowhead Antipattern)
当代码中有多层嵌套时,会降低代码的可读性,对于以后的修改也增加难度,所以我们需要分解复杂的判断并尽快返回. 重构前代码 public class Security { public ISecurity ...
- Hibernate 函数 ,子查询 和原生SQL查询
一. 函数 聚合函数:count(),avg(),sum(),min(),max() 例:(1)查询Dept表中的所有的记录条数. String hql=" select count(*) ...
- 关于background定位
直到刚刚我才发现我小瞧了background定位 因为项目里需要显示隐藏的按钮上有两个图标 开始想了几种方法都不行,然后突然就想到了background定位 果断试了一下 <input type ...
- 原生ajax与封装的ajax使用方法
当我们不会写后端接口来测试ajax时,我们可以使用node环境创建一个本地服务器. 1.创建一个本地服务器可参考http://www.cnblogs.com/heyujun-/p/6793900.ht ...
- 任务九:使用HTML/CSS实现一个复杂页面
任务目的 通过实现一个较为复杂的页面,加深对于HTML,CSS的实战能力 实践代码的复用.优化 任务描述 通过HTML及CSS实现设计稿 设计稿PSD文件(点击下载),效果如 效果图(点击打开) 整个 ...
- Redux学习笔记-基础知识
Redux概述 是什么:Redux是管理状态的容器,提供可预测的状态管理. 怎么做:Redux主要是用来管理组件或者应用的状态,本质上跟一个事件处理器差不多.通过分发action触发reduce来 ...