hdu1026.Ignatius and the Princess I(bfs + 优先队列)
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13911 Accepted Submission(s): 4370 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<queue>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
const int M = + , inf = 0x3f3f3f3f ;
int n , m;
char map[M][M] ;
bool vis[M][M] ;
int move[][] = {{,} , {-,} , {,} , {,-}} ;
struct node
{
int x , y , time ;
int pet ;
int nxt ;
}e[ + ];
int l , r ;
int cas ,k ; bool cmp (node a , node b)
{
return a.time < b.time ;
}
void bfs ()
{
node ans , tmp ;
l = , r = ;
k = ;
for (int i = ; i < n ; i ++) for (int j = ; j < m ; j ++) vis[i][j] = ;
e[l].x = , e[l].y = , e[l].time = , e[l].nxt = - , e[l].pet = ;
while (l != r) {
std::sort (e + l , e + r , cmp ) ;
//printf ("(%d,%d)\n" , e[l].x , e[l].y ) ;
ans = e[l] ;
if (e[l].x == n - && e[l].y == m - ) {k = ; return ;}
// printf ("S====(%d,%d)\n" , ans.x , ans.y ) ;
for (int i = ; i < ; i ++) {
tmp.x = ans.x + move[i][] , tmp.y = ans.y + move[i][] ; tmp.pet = ;
if (tmp.x < || tmp.y < || tmp.x >= n || tmp.y >= m) continue ;
if (map[tmp.x][tmp.y] == 'X' ) continue ;
if (vis[tmp.x][tmp.y]) continue ;
if ( isdigit ( map[tmp.x][tmp.y] ) ) {tmp.time = ans.time + + map[tmp.x][tmp.y] - '' ; tmp.pet = map[tmp.x][tmp.y] - '' ;}
else tmp.time = ans.time + ;
tmp.nxt = l ;
vis[tmp.x][tmp.y] = ;
// printf ("(%d,%d)\n" , tmp.x , tmp.y ) ;
e[r ++] = tmp ;
}
l ++ ;
}
} void solve (int k)
{
if (k == -) return ;
solve (e[k].nxt) ;
int t = e[k].nxt ;
if ( t != - ) {
printf ("%ds:(%d,%d)->(%d,%d)\n" , cas ++ , e[t].x , e[t].y , e[k].x , e[k].y ) ;
for (int i = ; i < e[k].pet ; i ++) printf ("%ds:FIGHT AT (%d,%d)\n" , cas ++ , e[k].x , e[k].y) ;
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%d%d" , &n , &m)) {
for (int i = ; i < n ; i ++) scanf ("%s" , map[i]) ;
bfs () ;
if (k) {
printf ("It takes %d seconds to reach the target position, let me show you the way.\n" , e[l].time ) ;
cas = ;
solve (l) ;
puts ("FINISH") ;
}
else {
puts ("God please help our poor hero.") ;
puts ("FINISH") ;
}
}
return ;
}
这种不需要走回头路的,而且走的过程中会出现罚时的题目,就用优先队列吧.
发现输出路径的用模拟来写很方便.
hdu1026.Ignatius and the Princess I(bfs + 优先队列)的更多相关文章
- 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/100 ...
- 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 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】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 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 ...
- HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜
此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Me ...
- 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 ...
- hdu1026 Ignatius and the Princess I (优先队列 BFS)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 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 ...
随机推荐
- English Snippets
There is no Zen master to prod you with a stick, but I have some questions for you. Your answers wil ...
- Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
运行环境是win7 x64 sublime text2,百度发现是对齐问题. 具体来说是由于有的地方使用了4个空格,有的地方使用了tab键. 代码区直接全选就会看到有的地方是四个点有个地方是一个横线, ...
- Beta Daily Scrum 第三天
[目录] 1.任务进度 2.困难及解决 3.燃尽图 4.代码check-in 5.总结 1. 任务进度 学号 今日完成 明日完成 612 初步完成成就界面的统计图表 继续编写成就界面的图表 615 白 ...
- 谈谈 ES6 的 Promise 对象
https://segmentfault.com/a/1190000002928371 前言 开篇首先设想一个日常开发常常会遇到的需求:在多个接口异步请求数据,然后利用这些数据来进行一系列的操作.一般 ...
- jQuery小技巧
1. 禁止右键点击 $(document).bind("contextmenu",function(e){ return false; }); 2.隐藏搜索文本框文字 $(docu ...
- windows7-PowerDesigner 15.1 的安装图解
下载 PowerDesigner 15.1 的安装文件和破解文件 破解文件下载地址:http://pan.baidu.com/share/link?shareid=177873&uk=3626 ...
- htons
在Windows和Linux网络编程时需要用到的,用来将主机字节顺序转化为网络字节顺序,以Windows下的代码为例: 1 2 #include<winsock2.h> u_shortht ...
- 转移大于2m的pdf文件到另外一个文件夹
转移大于2m的pdf文件到另外一个文件夹 remove_moret2M_pdfs.py # -*- coding: utf-8 -*- """ Created on Mo ...
- C#验证子网掩码的正确性
1. IP合法关于IP地址的合法性验证很简单,方法也很多,比如字符串分解.正则表达式等. 2. 子网掩码是否合法简单来讲,子网掩码就类似这样一串数字,前面一段是连续的1, 类似 ...
- JSP 属性范围
参考文献:http://www.cnblogs.com/xdp-gacl/p/3781056.html 一.属性范围 所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围 ...