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 ...
随机推荐
- 一篇文章搞懂Linux安全!
Linux是开放源代码的免费正版软件,同时也是因为较之微软的Windows NT网络操作系统而言,Linux系统具有更好的稳定性.效率性和安全性. 在Internet/Intranet的大量应用中,网 ...
- HTML5语义化标签总结
1.语义化标签总结 基础布局标签 <header></header> <nav></nav> <main></main> < ...
- x == (x = y) 不等于 (x = y) == x ?
简评:不瞒你说,我现在数数都是从 0 开始数的,整数是 1024. 有这么一个 Java 程序: class Quirky { public static void main(String[] arg ...
- python-----读写操作
1. 文件的读取 注意:在windows中文件的路径是这样:C:\Users\name\mystuff .由于\u在python中表示转义如果使用此文件路径就会报错.解决方法: a.把斜杠\改为反斜杠 ...
- 「PKUWC2018」Slay the Spire
题目链接 题意分析 这个题其实不是期望 就是一共有\(C_{2n}^m\)种情况 每一种情况选择\(k\)张牌 然后求最大攻击值的总和 我们考虑 当前抽出了选出了\(i\)张强化牌 \(m-i\)张攻 ...
- pymongo认证连接
有的MongoDB数据库使用了认证功能,需要认证连接才能正常登录. mongoDB有不同的认证机制,3.0版本以后采用的是'SCRAM-SHA-1', 之前的版本采用的是'MONGODB-CR'.所以 ...
- slatstack高效运维
一.简介 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. 二.诞生的背景 系统管理员日常会进行大量的重复性操作,例如安装软件, ...
- MD5 工具类
package com.payease.chains.risk.utils; /** * md5密码加密工具类 * Created by liuxiaoming on 2017/8/28. */ pu ...
- GitLab 项目创建后地址由Localhost改为实际IP的方法
进入终端修改以下文件即可. vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml ## GitLab settings git ...
- lambda modern C++
Lambda expressions (since C++11) Syntax [ captures ] <tparams>(optional)(c++20) ( params ) s ...