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. 修改oracle系统用户system的密码

    C:\Users\Administrator>echo %ORACLE_SID% %ORACLE_SID% C:\Users\Administrator>set ORACLE_SID=or ...

  2. 【Alpha版本】 第八天 11.16

    一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成我要应聘的列表显示,完成账户信息设置界面 完成我要应聘的详情显 ...

  3. 我的WCF Data Service 系列 (一、为什么要有WCF Data Service)

    开篇先说两名题外话, 在博问上,经常看到有个问性能问题,比如Entity Framework的性能行不行啊之类的. 其实这个行不行,关键还是看对象,一夜家族的老七可能勉强吃点蓝片片,也就行了,可真要让 ...

  4. javascript表单验证

    表单HTML <form action="" method="post"> <fieldset class="login" ...

  5. 9.13 JS循环

    循环:循环操作某一个功能(执行某段代码) 四要素: 循环初始值 循环条件 状态改变 循环体       for  穷举     迭代            i++;等价于i=i+1;  ++I;等价于 ...

  6. WinForm------RepositoryItemCheckEdit属性介绍

    //去掉第三种状态 editcheck1.OptionView.NullStyle = UnChecked

  7. VC----对话框Dialog

    一个非模态对话框,当作主窗体的创建:(符合窗口创建的步骤) 第一步:补充一个模板,在RC脚本文件文件中,这是和普通窗口不一样的地方.这利益于编译器和链接器的支持呀. #include "wi ...

  8. [Redis]如何通过Powershell创建Redis服务

    目前Redis在中国上线了,不过只是预览版而且不能通过Portal进行操作,不过可以通过Powershell创建,具体如下: 下载最新的Powershell SDK:http://www.window ...

  9. Cache-Aside Pattern解析

    使用这种模式,可以帮助我们维护Cache中的数据. 使用Cache容易遇到的问题: 使用缓存,主要是为了将一些重复访问的数据存到缓存,开发者希望缓存中的数据和数据源中的保持一致,这就需要程序中有相应的 ...

  10. vertical-align0 垂直对齐- 图片 兼容个浏览器

    效果:  代码: <html> <head> <style type="text/css"> img.top {vertical-align:t ...