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

Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:
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.
 
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
 
Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
 
Sample Input
5 6
.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.
 
Sample Output
It takes 13 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)
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 + 优先队列)的更多相关文章

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

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

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

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

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

  6. HDU-1026 Ignatius and the Princess I(BFS) 带路径的广搜

      此题需要时间更少,控制时间很要,这个题目要多多看, Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Me ...

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

  8. hdu1026 Ignatius and the Princess I (优先队列 BFS)

    Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...

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

随机推荐

  1. RabbitMQ Queue分发多个Consumer

    多个Consumer的消息分发 之前讲过一个queue对应一个consumer的小例子, 但是在实际项目中,一个consumer肯定是不够的,queue中的消息过多.一个consumer明显会处理过慢 ...

  2. python函数(五)

    函数 1.函数基本语法及特性 背景提要 现在老板让你写一个监控程序,监控服务器的系统状况,当cpu\memory\disk等指标的使用量超过阀值时即发邮件报警, 你掏空了所有的知识量,写出了以下代码 ...

  3. OpenGLES入门笔记二

    #import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> #import <OpenGLES/ES2/gl.h& ...

  4. Tarjan_LCA

    貌似求LCA使用倍增已经可以应付掉大多数需要LCA的题了.. 但是有些时候$O(MlogN)$的复杂度就不可接受了 Tarjan_LCA对于每个询问采用离线处理 总复杂度为$O(M+N)$ 这个复杂度 ...

  5. python03 面向对象的编程01

    话不多说,直接进入正文: 编程思想: 我个人所了解的有2种,还有个函数式编程的概念模模糊糊(大概是把常用的代码封装成一个函数,重复调用即可,先认为就是这样吧) 1 面向对象:面向对象是把所有的事务物当 ...

  6. uC/OS-II汇编代码

    ;*************************************************************************************************** ...

  7. js021-Ajax与Comet

    js021-Ajax与Comet 本章内容: 使用XMLHttpRequet对象 使用XMLHttpRequet事件 跨域Ajax通信的限制 Ajax技术的核心是XMLHttpRequet对象 21. ...

  8. Windows10安装MongoDB

    环境:Windows10x64,mongodb-win32-x86_64-2008plus-ssl-3.2.9-signed.msi 步骤: 安装msi文件到D:\ 新建配置文件mongo.confi ...

  9. CSS3自适配手机屏幕

    @media only screen and (max-width:350px){ .img{ width: 80px; height:70px; background-image: url(./im ...

  10. SQL日期格式转换

    CONVERT(nvarchar(20), [Date],101) as 'Date'    10/20/2016 CONVERT(nvarchar(20), [Date],102) as 'Date ...