搜索专题: HDU1026Ignatius 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): 19514 Accepted Submission(s): 6320
Special Judge
is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them,
he has to kill them. Here is some rules:
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.
labyrinth. The input is terminated by the end of file. More details in the Sample Input.
seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
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
Accepted
RunId : 21244726 Language : G++ Author : hnustwanghe
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
using namespace std;
const int MaxSize = 300000+5;
const int INF = (1<<30);
const int N = 200 + 5;
typedef struct node{
int x,y,val,mo,hp,u;
bool operator < (const node x) const {
return val > x.val;
}
}Node;
char mat[N][N];
int n,m;
bool visit[N][N];
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
vector<Node> State;
vector<Node> p;
void Init(){
memset(visit,0,sizeof(visit));
State.clear();
p.clear();
}
void print_ans(Node t){
printf("It takes %d seconds to reach the target position, let me show you the way.\n",t.val);
int time = 1,lastx=0,lasty=0;
p.push_back(t);
while(t.u > 0){
p.push_back(State[t.u]);
t = State[t.u];
}
for(int i=p.size()-1;i>=0;i--){
if(p[i].mo){
printf("%ds:(%d,%d)->(%d,%d)\n",time++,lastx,lasty,p[i].x,p[i].y);
while(p[i].hp--){
printf("%ds:FIGHT AT (%d,%d)\n",time++,p[i].x,p[i].y);
}
lastx = p[i].x,lasty = p[i].y;
}
else{
printf("%ds:(%d,%d)->(%d,%d)\n",time++,lastx,lasty,p[i].x,p[i].y);
lastx = p[i].x,lasty = p[i].y;
}
}
}
bool BFS(){
priority_queue<Node>Q;
int newx,newy,val,cnt = 0;
Node t,s;
t.x = t.y = t.mo = 0;
t.u = -1,t.val = 0;
Q.push(t);
while(!Q.empty()){
t = Q.top();
State.push_back(t);
cnt++;
Q.pop();
if(t.x == n-1 && t.y == m-1) { print_ans(t);return true;}
for(int d=0;d<4;d++){
newx = t.x + dir[d][0];
newy = t.y + dir[d][1];
if(newx>=0 && newx<n && newy>=0 && newy<m && mat[newx][newy]!='X'){
val = mat[newx][newy]=='.'?1:mat[newx][newy]-'0'+1;
if(!visit[newx][newy]){
visit[newx][newy] = true;
s.x = newx ,s.y = newy;
s.val = t.val + val;
s.u = cnt-1;
if(val > 1)
s.mo = 1,s.hp = val - 1;
else
s.mo = 0;
Q.push(s);
}
}
}
}
return false;
}
int main(){
while(scanf("%d %d",&n,&m)==2){
Init();
for(int i=0;i<n;i++)
scanf("%s",mat[i]);
if(!BFS())
printf("God please help our poor hero.\n");
printf("FINISH\n");
}
}
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
using namespace std;
const int MaxSize = 300000+5;
const int INF = (1<<30);
const int N = 200 + 5;
typedef struct node{
int x,y,val,mo,hp,u;
bool operator < (const node x) const {
return val > x.val;
}
}Node;
char mat[N][N];
int n,m;
bool visit[N][N];
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
vector<Node> State;
vector<Node> p;
void Init(){
memset(visit,0,sizeof(visit));
State.clear();
p.clear();
}
void print_ans(Node t){
printf("It takes %d seconds to reach the target position, let me show you the way.\n",t.val);
int time = 1,lastx=0,lasty=0;
p.push_back(t);
while(t.u > 0){
p.push_back(State[t.u]);
t = State[t.u];
}
for(int i=p.size()-1;i>=0;i--){
if(p[i].mo){
printf("%ds:(%d,%d)->(%d,%d)\n",time++,lastx,lasty,p[i].x,p[i].y);
while(p[i].hp--){
printf("%ds:FIGHT AT (%d,%d)\n",time++,p[i].x,p[i].y);
}
lastx = p[i].x,lasty = p[i].y;
}
else{
printf("%ds:(%d,%d)->(%d,%d)\n",time++,lastx,lasty,p[i].x,p[i].y);
lastx = p[i].x,lasty = p[i].y;
}
}
}
bool BFS(){
priority_queue<Node>Q;
int newx,newy,val,cnt = 0;
Node t,s;
t.x = t.y = t.mo = 0;
t.u = -1,t.val = 0;
Q.push(t);
while(!Q.empty()){
t = Q.top();
State.push_back(t);
cnt++;
Q.pop();
if(t.x == n-1 && t.y == m-1) { print_ans(t);return true;}
for(int d=0;d<4;d++){
newx = t.x + dir[d][0];
newy = t.y + dir[d][1];
if(newx>=0 && newx<n && newy>=0 && newy<m && mat[newx][newy]!='X'){
val = mat[newx][newy]=='.'?1:mat[newx][newy]-'0'+1;
if(!visit[newx][newy]){
visit[newx][newy] = true;
s.x = newx ,s.y = newy;
s.val = t.val + val;
s.u = cnt-1;
if(val > 1)
s.mo = 1,s.hp = val - 1;
else
s.mo = 0;
Q.push(s);
}
}
}
}
return false;
} int main(){
while(scanf("%d %d",&n,&m)==2){
Init();
for(int i=0;i<n;i++)
scanf("%s",mat[i]);
if(!BFS())
printf("God please help our poor hero.\n");
printf("FINISH\n");
}
}
搜索专题: HDU1026Ignatius and the Princess I的更多相关文章
- 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(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告
前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...
- NOIP2018提高组金牌训练营——搜索专题
NOIP2018提高组金牌训练营——搜索专题 1416 两点 福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”.基础级别的时候是在一个n×m单元上玩的.像这样: 每一个单元有包含一个有色点.我们将用不 ...
- 搜索专题:Balloons
搜索专题:Balloons 这道题一看与时间有关,第一想到的就是BFS,定义一个状态,包含每一个状态的剩余气球数,已经进行的时间和每一个志愿者上一次吹气球的时间: 每一次状态转换时,检查是否有没有使用 ...
- 搜索专题: HDU1027Ignatius and the Princess II
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- 2014 UESTC暑前集训搜索专题解题报告
A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...
- 【PHP高效搜索专题(2)】sphinx&coreseek在PHP程序中的应用实例
PHP可以通过三种途径来调用sphinx 通过Sphinx官方提供的API接口(接口有Python,Java,Php三种版本) 通过安装SphinxSE,然后创建一个中介sphinxSE类型的表,再通 ...
- 【PHP高效搜索专题(1)】sphinx&Coreseek的介绍与安装
我们已经知道mysql中带有"%keyword%"条件的sql是不走索引的,而不走索引的sql在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...
随机推荐
- SQL Server 2012 sa 用户登录 18456 错误 (转)
转自:http://blog.csdn.net/waterxcfg304/article/details/40617475 最近想研究下SQL SERVER2012 Enterprise版本的数据库, ...
- JMS学习十(ActiveMQ支持的传输协议)
ActiveMQ提供了一种连接机制,这种连接机制使用传输连接器(TransportConnector)实现客户端与代理(client - to - broker)之间的通信. 网络连接器(networ ...
- lombok效率神奇使用
Lombok效率神器 标签(空格分隔): Java Lombok简介及使用 Lombok 是一种 Java实用工具,可用来帮助开发人员消除Java的冗长,尤其是对于简单的Java对象(POJO), 它 ...
- sqli-labs(17)
0x01简介 百度翻译了一下 基于错误的更新查询 字符型 第一次遇到这种情况 那我们先看看源代码行吧 不懂函数的百度 $result = mysql_query($sql);//返回查询的数据的一个结 ...
- 数据库MySQL(课下作业,必做) 20175225
作业要求: 1.下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功 ...
- IntelliJ IDEA2018破解教程
破解方法:下载破解补丁→修改配置文件→输入激活码→激活成功 由于JetBrains封杀,大部分激活服务器已经不能使用,使用下面的比较麻烦的方法也可以进行破解,但是有效期是到2100年(emmmm,也算 ...
- Eigen中的矩阵及向量运算
Eigen中的矩阵及向量运算 ,[+,+=,-,-=] ,[\*,\*=] ,[.transpose()] ,[.dot(),.cross(),.adjoint()] ,针对矩阵元素进行的操作[.su ...
- 洛谷P1190 接水问题
题目名称:接水问题 题目来源 [洛谷P1190] (https://www.luogu.org/problemnew/show/P1190) 题目描述 学校里有一个水房,水房里一共有\(m\)个龙头 ...
- 转 HTTP请求报文格式 GET和POST
https://blog.csdn.net/h517604180/article/details/79802914 最近在做安卓客户端图片上传插件功能,供后台调用.其中涉及到了拼接HTTP请求报文,所 ...
- Linux监控命令之==>vmstat
一.使用说明 vmstat 可以对操作系统的内存信息.进程状态.CPU 活动.磁盘等信息进行监控,不足之处是无法对某个进程进行深入分析. 二.用法及参数说明 -a:显示活跃和非活跃内存 -f:显示从系 ...