hdu1026 Ignatius and the Princess I (优先队列 BFS)
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<stdio.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef struct N
{
int i,j,time;
char c;
int fron,nam;
friend bool operator<(N n1,N n2)
{
return n2.time<n1.time;
}
}node;
node map[105][105];
stack<node> S;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}},h,w,maxT,flog;
void InStack(node Map)
{
node q=Map;
while(q.fron!=-1)
{
S.push(q);
q=map[q.fron/w][q.fron%w]; }
S.push(q);
//printf("%d\n",p);
}
void BFS(void)
{
priority_queue<node> Q;
node q,p;
int e,x,y,k,i;
map[0][0].c='X'; map[0][0].fron=-1;
q.i=0;q.j=0;q.time=0;
Q.push(q);
while(!Q.empty())
{
q=Q.top();
Q.pop();
k=0;
for(e=0;e<4;e++)
{
y=q.i+dir[e][0];
x=q.j+dir[e][1];
if(y>=0&&y<h&&x>=0&&x<w&&map[y][x].c!='X')
{
map[y][x].fron=map[q.i][q.j].nam; if(map[y][x].c=='.')
map[y][x].time=q.time+1;
else
map[y][x].time=q.time+map[y][x].c-'0'+1;
map[y][x].c='X';
p.i=y; p.j=x;p.time=map[y][x].time;
if(p.i==h-1&&p.j==w-1)
{
maxT=map[h-1][w-1].time;
InStack(map[h-1][w-1]);
flog=1;
//printf("%d\n",maxT);
return ;
} Q.push(p);
}
}
}
}
int main()
{
int i,j,t;
node q,p;
while(scanf("%d%d",&h,&w)>0)
{
t=0;
for(i=0;i<h;i++)
{
getchar();
for(j=0;j<w;j++)
{
scanf("%c",&map[i][j].c);
map[i][j].i=i; map[i][j].j=j;
map[i][j].nam=t++;
}
}
if(map[h-1][w-1].c=='X')
{
printf("God please help our poor hero.\n");
printf("FINISH\n");
continue ;
}
flog=0;
BFS();
if(!flog)
printf("God please help our poor hero.\n");
else
{
p=S.top();
S.pop();//printf("%d\n",map[1][4].time);
printf("It takes %d seconds to reach the target position, let me show you the way.\n",maxT);
for(i=1;i<=maxT;i++)
{
if(p.time<i)
{
q=S.top();
S.pop();
}
if(p.time<i)
printf("%ds:(%d,%d)->(%d,%d)\n",i,p.i,p.j,q.i,q.j);
else
printf("%ds:FIGHT AT (%d,%d)\n",i,p.i,p.j);
p=q;
}
}
printf("FINISH\n");
}
}
hdu1026 Ignatius and the Princess I (优先队列 BFS)的更多相关文章
- 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+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- 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:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- 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)
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) Memory Limit: 65536/32768 K (J ...
- HDU 1026 Ignatius and the Princess I (BFS)
题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...
随机推荐
- cocos2d-x系列 Mac下配置cocos2d-x开发环境(android和ios)
一.下载cocos2d-x http://cocos2d-x.org/projects/cocos2d-x/wiki/Download cocos2d-x-2.1.4.zip @ June.18, 2 ...
- gnome中文翻译之po
文件类型: po: 用msginit分析pot文件,生成各语言对应的po文件,比如中文的zh_CN.po. mo: 用msgfmt将po文件编译生成mo文件,这是二进制文件,不能直接编辑. gmo: ...
- Android中用Application类实现全局变量
最近在项目中,遇到了application这个类,开始不知道有什么用,经过学习后才知道它的用途也蛮大的,举个例子,如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型:而在 ...
- HTML5峰会小记
5259月228日,在大连理工的伯川图书馆举办了一次HTML5峰会,小记一下这次峰会的内容. 名为HTML5峰会,其本质就是一次各大赞助商的轮番登场,产品介绍间隙插播一下HTML5.但是并不影响这次峰 ...
- git实现版本回退
1. 首先查看自己的版本: ***:~/piaoshifu_object/epiao.piaoshifu.cn$ git log commit c8d5c67861d2d0e21856cc2b4f60 ...
- nodejs compressor
http://www.2cto.com/kf/201203/122015.html http://www.cnblogs.com/terrylin/archive/2013/06/01/3112596 ...
- AWK 简明教程
AWK 简明教程 转自:http://coolshell.cn/articles/9070.html 有一些网友看了前两天的<Linux下应该知道的技巧>希望我能教教他们用awk和sed, ...
- Vim识别编码
http://blog.chinaunix.net/uid-20357359-id-1963123.html
- Principles of Motion Sensing
Principlesof Motion Sensing Various sensors capable of detecting motionin free space have been comme ...
- 浅析深究什么是SOA?(转)
http://blog.vsharing.com/fengjicheng/A1059842.html 阅读提示: 本文探讨SOA概念背后的核心内涵,如何将SOA落地的实务方法. 金蝶中间件作为全球领先 ...