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> ...
随机推荐
- C#字符串颠倒输出
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- 检查.gitignore语法
每次配置git的时候,要写gitignore文件,你知道你的.gitignore匹配那些文件吗? 看看.gitignore放过了哪些文件: git ls-files -ocm --exclude-st ...
- (正则表达式应用) 替换自闭合标签(self-closing tag)的method
var str = "<sup><div class=\"he's\"/></sup><span id=\"cs\&q ...
- String,StringBuffer,StringBuilder的简单比较
原文:http://blog.csdn.net/rmn190/article/details/1492013 String 字符串常量StringBuffer 字符串变量(线程安全)StringB ...
- java设计模式——接口模式
java将接口的概念提升为独立的结构,体现了接口与实现分离.java接口允许多个类提供相同的功能,也允许一个同时实现多个接口.java的接口与抽象类十分相似.java与抽象类中的区别: 1.一个类可以 ...
- ubuntu npm 私有库搭建 (npmjs.org 官方版本)
目标 npm.xxx.com 安装和推送nodejs包 npmui.xxx.com 管理已经推送的nodejs包 安装 couchdb https://launchpad.net/~couch ...
- VS下面的编译错误-----转换到 COFF 期间失败: 文件无效或损坏
最近写了一个vs的小项目,然后编译的时候vs提示了"转换到 COFF 期间失败: 文件无效或损坏"的问题. 去网上搜索了一个解决方案.原作者的链接是:http://jingyan. ...
- Noah的学习笔记之Python篇:函数“可变长参数”
Noah的学习笔记之Python篇: 1.装饰器 2.函数“可变长参数” 3.命令行解析 注:本文全原创,作者:Noah Zhang (http://www.cnblogs.com/noahzn/) ...
- JSP中两种模式的总结
运用JSP/Servlet实现的Web动态交互,主要采用: 模式一:JSP+JavaBean 链接:http://wxmimperio.coding.io/?p=155 模式二;JSP+Servlet ...
- pragma指令简介
整理日:2015年3月12日 资源来来自己网络 在编写程序的时候,我们经常要用到#pragma指令来设定编译器的状态或者是指示编译器完成一些特定的动作. 下面介绍了一下该指令的一些常用参数,希望对大家 ...