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",& ...
随机推荐
- 【CS Round #44 (Div. 2 only) A】Frequent Numbers
[链接]h在这里写链接 [题意] 在这里写题意 [题解] 大水题 [错的次数] 0 [反思] 在这了写反思 [代码] /* */ #include <cstdio> #include &l ...
- xml-apis.jar getTextContent() jar包冲突解决(getTextContent()方法无法找到)
1.引用包: import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList; 2.方法中应用: ...
- 【51nod1563】坐标轴上的最大团 贪心
题面 坐标轴上有n个点,每个点有一个权值.第i个点的坐标是 xi ,权值是 wi .现在对这些点建图.对于点对 (i,j) ,如果 \(|xi−xj|≥wi+wj\) ,那么就给第i个点和第j个点之间 ...
- TCP keepalive的机理及使用
TCP 是面向连接的 , 在实际应用中通常都需要检测对端是否还处于连接中.如果已断开连接,主要分为以下几种情况: 1.连接的对端正常关闭,即使用 closesocket 关闭连接. 2.连接的对端非正 ...
- 解决Dynamic Web Module 3.0 requires Java 1.6 or newer.问题
在项目的pom.xml的<build></build>标签中增加: <plugins> <plugin> <gro ...
- day39-Spring 08-Spring的AOP:基于AspectJ的注解
基于AspectJ的注解的开发要重点掌握. 这些表达式肯定要应用在我们的某些个增强上. 学习AspectJ也是两种形式:一种是XML,一种是注解.AspectJ的增强,就是那些通知的类型.Aspect ...
- PHPCMS快速建站系列之pc:get标签的应用
GET标签使用方式如下: {pc:get sql="SELECT * FROM phpcms_member" cache="3600" page="$ ...
- win7 powershell版本过低问题
那台win8系统的笔记本电脑 硬盘坏掉后 在win7系统的台式机上使用 vagrant up 提示版本过低 The version of powershell currently installed ...
- SQLServer —— 用户权限操作
说明 以下操作都是基于SQLServer登陆验证方式登陆.而且操作员都是 sa. 一.添加登陆账号 use master go ' 第一个(xu)是登陆名,第二个(123456)是登陆密码. 执行语句 ...
- python 利用抛出异常并处理的优点