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> ...
随机推荐
- object-C 手动内存管理(MRC)
object-C的内存管理和javascript的垃圾回收不一样,今天总结下手动内存管理,ARC的后边补上. 1:基本铺垫 oc采用引用计数来表示对象的状态,比如通过init创建出来的一个对象引用计数 ...
- js实现简单计算器
效果图: 刚开始做时没考虑到清零和退格两个功能,嘻嘻,后来加的整体与传统计算器比有点小瑕疵. 代码: <!DOCTYPE html><html><head> < ...
- (转)JS页面间传值
一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—> <input type="text& ...
- Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (111)解决方法
登陆mysql的时候,出现了这个问题: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' ( ...
- [CSS]overflow内容溢出
定义和用法 overflow 属性规定当内容溢出元素框时发生的事情. 说明 这个属性定义溢出元素内容区的内容会如何处理.如果值为 scroll,不论是否需要,用户代理都会提供一种滚动机制.因此,有 ...
- 移动端触摸滑动插件Swiper
移动端触摸滑动插件Swiper 04/02/2015 一.了解Swiper 目前移动端项目一般都需要具有触屏焦点图的效果,如果你也需要实现这一功能的话,Swiper是一个不错的选择. 1.他不需要加载 ...
- 配置SHH集群
==特别要注意当前用户的问题== 1. 修改路由信息 vi /etc/hosts 10.211.55.15 hmaster1 10.211.55.16 hmaster2 10.211.55.17 hs ...
- 转】VB中Set的用法
Set 语句 将对象引用赋给变量或属性. 语法 Set objectvar = {[New] objectexpression | Nothing} Set 语句的语法包含下面部分: 部分 描述 ob ...
- java相关各种页面跳转
AK相信页面跳转在这个圈圈圆圆里是个地球人都能经常遇到的事,AK也在平时的工作学习中记录了一些,这里就做一个小小的总结,有任何的疑问和质疑都希望您能告诉我,不用担心后果,必定至少您还能理我,AK万分感 ...
- go bytes缓冲区使用介绍
缓冲区原理简介: go字节缓冲区底层以字节切片做存储,切片存在长度len与容量cap, 缓冲区写从长度len的位置开始写,当len>cap时,会自动扩容.缓冲区读会从内置标记off位置开始读(o ...