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 (Java/Others)
Total Submission(s): 14677 Accepted Submission(s): 4653
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.
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.
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 <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; struct node
{
int x,y,step;
friend bool operator < (node n1,node n2) //优先队列,队列从小到大排序
{
return n1.step>n2.step;
}
} s1,s2,ss[][]; //ss数组记录走的所有路径
char map[][];
int visit[][],n,m,t,p;
int f[][]= {,,,-,,,-,}; void BFS()
{
priority_queue <node> q; //优先队列的定义
while(!q.empty())
q.pop();
s1.x=;
s1.y=;
s1.step=;
visit[][]=;
ss[s1.x][s1.y].x=; //起点为左上角
ss[s1.x][s1.y].y=;
q.push(s1);
while(!q.empty())
{
s1=q.top();
q.pop();
if(s1.x==n-&&s1.y==m-) //终点为右下角
{
p=s1.step;
return;
}
for(int i=; i<; i++)
{
s2=s1;
s2.x=s1.x+f[i][];
s2.y=s1.y+f[i][];
if(s2.x>=&&s2.x<n&&s2.y>=&&s2.y<m&&map[s2.x][s2.y]!='X'&&!visit[s2.x][s2.y])
{
visit[s2.x][s2.y]=;
s2.step=s1.step+;
if(map[s2.x][s2.y]>=''&&map[s2.x][s2.y]<='') //遇到怪物的时候需要耗得时间
s2.step=s2.step+map[s2.x][s2.y]-'';
ss[s2.x][s2.y].x=s1.x; //记录走到这一步的上一步坐标
ss[s2.x][s2.y].y=s1.y;
q.push(s2);
}
}
}
p=-; //到不了的标记
return;
} void print(int x,int y) //深搜输出,从终点开始往前搜
{
if(x==&&y==) return; //找到起点,返回上一次
print(ss[x][y].x,ss[x][y].y);
printf("%ds:(%d,%d)->(%d,%d)\n",t++,ss[x][y].x,ss[x][y].y,x,y);
if(map[x][y]>=''&&map[x][y]<='') //如果遇到怪兽,多呆几秒的输出
{
int w=map[x][y]-'';
for(int i=w; i>; i--)
printf("%ds:FIGHT AT (%d,%d)\n",t++,x,y);
}
}
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=; i<n; i++)
scanf("%s",&map[i]);
memset(visit,,sizeof(visit));
BFS();
t=;
if(p==-) //到不了的特定输出
printf("God please help our poor hero.\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",p); //输出一共需要多少秒
print(n-,m-);
}
printf("FINISH\n"); //别忘了需要输出的最后一句话
}
return ;
}
hdu 1026 Ignatius and the Princess I(bfs)的更多相关文章
- HDU 1026 Ignatius and the Princess I (BFS)
题目链接 题意 : 从(0,0)点走到(N-1,M-1)点,问最少时间. 思路 : BFS..... #include <stdio.h> #include <string.h> ...
- 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广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1028 Ignatius and the Princess III(母函数)
题意: N=a[1]+a[2]+a[3]+...+a[m]; a[i]>0,1<=m<=N; 例如: 4 = 4; 4 = 3 + 1; 4 = 2 + 2; 4 = 2 + ...
- 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)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
- HDU 1026 Ignatius and the Princess I (广搜)
题目链接 Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- HDU 1029 Ignatius and the Princess IV(数论)
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...
随机推荐
- 第十章—DOM(二)——Element类型
Element类型用于表现HTML和XML,提供了对元素标签名,子节点和特效的访问.Element节点具有以下特征: 要访问元素的标签名,可以使用nodeName属性,也可以使用tagName属性.这 ...
- Python 五个知识点搞定作用域
Python 五个知识点搞定作用域 1.块级作用域 想想此时运行下面的程序会有输出吗?执行会成功吗? #块级作用域 if 1 == 1: name = "lzl" print(na ...
- 【JZOJ3296】【SDOI2013】刺客信条(assassin)
╰( ̄▽ ̄)╭ Description 故事发生在1486 年的意大利,Ezio 原本只是一个文艺复兴时期的贵族,后来因为家族成员受到圣殿骑士的杀害,决心成为一名刺客.最终,凭借着他的努力和出众的天赋 ...
- 将CMD命令提示符的起始位置进行更改 / CMD起始位置发生改变后如何修改回来
具体步骤如下: 1.首先我们需要先找到命令提示符所在的文件目录.可以在开始运行程序中输入CMD,一般回自动搜索匹配. 2.右键点击命令提示符,在弹出菜单中,选择“打开文件位置”: 3.然后我们就可以进 ...
- tcpdump抓取udp报文
使用tcpdump命令抓取UDP 2000端口报文,并将报文保存到当前目录下的udp.cap文件,命令如下: tcpdump -i 网络接口名称 udp port 2000 -w ./udp.cap ...
- JS判断PC 手机端显示不同的内容
方法一: function goPAGE() { if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android ...
- python判断输入日期是该年的第几天
1.输入日期,判断日期是该年度的第几天 iyear = int(input("请输入年:\n")) imonth = int(input("请输入月:\n")) ...
- iOS 9 学习系列:Split Screen Multitasking
http://www.cocoachina.com/ios/20151010/13601.html iOS 9 的一个重大变化就是增加了多任务,这个多任务允许用户在屏幕上同时运行多个 app.有两种形 ...
- 在MaxCompute中配置Policy策略遇到结果不一致的问题
背景信息: 本文以如下场景为基准进行编写,如下: 用户通过DataWorks-简单模式使用MaxCompute: 用户具有DataWorks默认角色,如DataWorks开发者角色: 用户通过cons ...
- CDH5.13.1安装
文件下载 Cloudera Manager 地址:http://archive.cloudera.com/cm5/cm/5/ 这里下载的是5.13.1的版本,https://archive.cloud ...