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 ...
随机推荐
- ubantu安装sogou输入法
Ubuntu的搜狗输入法安装步骤 1 本来想先移除ibus,但是在之后发现如果直接使用下面的命令 sudo apt-get remove ibus 移除ibus将导致系统某些地方不正常的问题,例如 ...
- C++ essentials 之 static 关键字
extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...
- iOS - libc++abi.dylib: terminate_handler unexpectedly threw an exception
代码出现crash,报错:libc++abi.dylib: terminate_handler unexpectedly threw an exception 当我们很明确是某一块代码执行导致了错误, ...
- rhino(犀牛) --- color control
create color materials, if "材料赋予方式" is "图层", the color of "材质" is show ...
- spring--基本介绍
1.1.1 Spring是什么 Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发.应用程序是由 ...
- 手写控件,frame,center和bounds属性
一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4)如果是button等控件,还需考虑控件的单击事件等 (5)注意:View ...
- SortedSet接口
TreeSet中实现了SortedSet接口,此接口主要用于排序操作,即实现此接口的子类都属于排序的子类. import java.util.Set; import java.util.SortedS ...
- dedecms标签的sql语句
{dede:sql sql='Select content from dede_arctype where id=1' titlelen='40′} [field:content/] {/dede:s ...
- Swiper基本上使用
导入三个文件 jquery-1.11.1.min.js,swiper.min.js,swiper.min.css 攻略教程 http://www.swiper.com.cn/api/function/ ...
- python 二分法查找实例(递归、循环)
二分法,主要应用于有序序列中,原理是每次查找都将原序列折半,逐渐缩小查找范围的一种算法. 需求 要求在一个有序序列中,例如[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 ...