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 ...
随机推荐
- ExceptionLess新玩法 — 记日志
ExceptionLess 之前也有介绍过这个框架,其实网上也有很多的资料,无论是部署还是一些详细的高级玩法都讲的很清楚也很棒,博主也学习了一些他们的博文,因为很多的东西比如本地部署别人已经写了,我再 ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- iOS - 落叶加载动画效果
代码下载地址:https://github.com/nLoser/LeafLoadingView 效果: 说明:效果是在网上看到的,并且自己按照效果自己实现,树叶使用CAEmitterLayer做的, ...
- JS-纯js制作动态成绩表(流程控制语句+js内置对象)
流程控制for循环+if判断+Math对象+Array对象+Date对象制作成绩表 <!DOCTYPE html><html> <head> <meta ch ...
- sql 中的运算符级别 如and or not
写了这么多简单的sql,很多东西忘记得差不多了,差点连最基本sql运算符优先级都忘了.平时最常用到and or的优先级都忘了 and的优先级高于or的优先级 举个例子 select * from us ...
- wpf的毛边窗体效果 前台代码
<Window x:Class="wpfwindowsmove.毛边窗体" xmlns="http://schemas.microsoft.com/w ...
- string length()
#include <set> std::set<std::string> setName; int main() { std::string strName = "世 ...
- 10月14日上午PHP环境搭建
第一步:安装wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b文件,安装过程中可能会遇到问题,把遇到的问题代码复制粘贴到360人工服务,查找方案 ...
- HTML5基本布局
HTML4布局 HTML5布局 基本的HTML5文档的模式为: <!DOCTYPE html> <html lang = "en"> <head> ...
- WindowsService 创建.安装.部署
windows服务的用法很适合用于一些长期跑的项目..不需要人工操作..不需要服务器一直登陆..很方便.. 不说废话..直接开整.. 启动VS2012..创建Windows服务项目.. 确定..创建成 ...