hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10312 Accepted Submission(s): 3125 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.
.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
/* 搜索,输出走的路径。
用一个falg数组保存每一次的方向,所以最后 递归的求出来就可以了。 优先队列的两种不同写法。
在搜索的过程中,不能出现走回头路的情况。这里使用了一个把后路堵死的方法。
“f[x1][y1]=-1;//防止重复访问。”
而且优先队列,对于一个点来说,第一次访问的time就是最小的。 */ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
int n,m,time1,TT;
int f[][];
int cnt[][];
int flag[][];
int map1[][]={ {,},{,},{-,},{,-} };
struct node
{
int x,y;
int t;
};
struct cmp
{
bool operator() (const node &n1,const node &n2) //!!注意有的不是这样写的。
{
return n1.t>n2.t;
}
}; int bfs()
{
int i,x1,y1;
node next,cur;
priority_queue<node,vector<node>,cmp>Q;//!
cur.x=; cur.y=; cur.t=;
f[][]=-;
Q.push(cur);
while(!Q.empty())
{
cur=Q.top();
Q.pop();
for(i=;i<;i++)
{
x1=cur.x+map1[i][];
y1=cur.y+map1[i][];
next.x=x1;
next.y=y1;
if(x1>=&&x1<=n &&y1>=&&y1<=m && f[x1][y1]!=-)
{
next.t=cur.t+f[x1][y1]+;
Q.push(next);
f[x1][y1]=-;//防止重复访问。
flag[x1][y1]=i+;//记录 路径
if(x1==n && y1==m)
return next.t;
}
}
}
return ;
} void print(int x,int y)
{
int x1,y1;
if(flag[x][y]==)return;
x1=x-map1[flag[x][y]-][];
y1=y-map1[flag[x][y]-][]; print(x1,y1);
printf("%ds:(%d,%d)->(%d,%d)\n",TT++,x1-,y1-,x-,y-);
while(cnt[x][y])
{
printf("%ds:FIGHT AT (%d,%d)\n",TT++,x-,y-);
cnt[x][y]--;
}
return;
} int main()
{
int i,j,Sum;
char a[];
while(scanf("%d%d",&n,&m)>)
{
memset(cnt,,sizeof(cnt));
memset(flag,,sizeof(flag));
for(i=;i<=n;i++)
{
scanf("%s",a+);
for(j=;j<=m;j++)
{
if(a[j]=='X')
f[i][j]=-;
else if(a[j]=='.')
f[i][j]=;
else f[i][j]=cnt[i][j]=a[j]-'';
}
}
TT=;
Sum=bfs();
if(Sum)
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",Sum);
print(n,m);
}
else
printf("God please help our poor hero.\n"); printf("FINISH\n");
}
return ;
}
hdu 1026 Ignatius and the Princess I 搜索,输出路径的更多相关文章
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- 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(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
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 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 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 ...
随机推荐
- python中mysql的存储
1. 连接mysql import pymysql db = pymysql.connect(host=', port=3306) cursor = db.cursor() cursor.execut ...
- 关于iframe切换的问题
定位不到元素的另一种问题是有这种iframe,所有我们需要切换到该页面中去 1.首先找到这个iframe的位置,像上图有id属性我们直接 iframe = driver.find_element_b ...
- 基于CH340的一键下载电路
一.CH340简介 CH340 是一个 USB 总线的转接芯片,实现 USB 转串口或者 USB 转打印口.CH340是国产芯片,应用场合居多,市场占有率很高.常用的USB转串口芯片还有CP2102. ...
- 43.oracle同义词
不愿长大,好多人如此,其实这是一种逃避责任没有担当的表象. 同义词 从字面上理解就是别名的意思,和视图的功能类似,就是一张映射关系. 私有同义词:一般是普通用户自己建立的同义词,创建者需要create ...
- FlowPortal-BPM——离线审批(邮箱审批)配置
一.将系统文件复制到安装目录下 二.以管理员身份运行bat安装程序 三.开启邮件离线审批服务 四.创建数据库表(JAVA数据库 或 .Net数据库) 五.配置config文件(发件箱.收件箱.错误问题 ...
- 为项目配置了Bean,结果Spring Boot并没有扫描到
问题如图,而这个问题遇见的场景是因为自己在一个基础项目里面配置cros,按照网上的说法都配置了一边,结果发现前后端的通讯仍然报跨域问题.后来怀疑是否bean并没有被注入进去,导致没有生效,于是在代码中 ...
- Java中静态变量的声明位置
Java中静态变量只能是成员变量,局部方法中的局部变量除final外不能有任何其他修饰符,例如: public class Test { static String x = "1" ...
- 一个好用的ssh终端:MobaXterm
WSL由于没有图形界面,所有操作都是在命令行里执行,平时用来编译和跑CFD代码其实还是挺方便.不过有时候要查看WSL里的文件就比较麻烦,这时可以用SFTP这类工具,连接过去后直接操作文件.试过几个这类 ...
- php 根据子分类循环获取其父级分类
/** * 根据子分类循环获取其父级分类 */ function goodsCatPath($catId, $data = []){ if($catId==0)return $data; $data[ ...
- 【old】Python学习笔记
上学期看视频记得,也没学到多少,目前打算一边通过<Python学习手册 第四版>提高核心语法(太厚了 噗),一边学习Python Web开发 然后这里的多任务编程和网络编程是暑假学的 5. ...