搜索专题: 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在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...
随机推荐
- 18. ClustrixDB 管理CPU资源
作为一个分布式MPP数据库,ClustrixDB能够利用多个节点和核心来比单节点数据库更快地处理查询.有两个可调进程可以促进这一点. Clustrix选择了一组默认参数来控制那些提供最常见工作负载的进 ...
- spring自定义注解实现登陆拦截器
1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...
- UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)
题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...
- 在Sublime Text 3中配置Python3的开发环境/Build System
本文来源:https://www.cnblogs.com/zhangqinwei/p/6886600.html Sublime Text作为一款支持多种编程语言的文本编辑神器,深受广大开发者的喜爱.通 ...
- MySQL_约束
MySQL中约束的作用是对表中的数据进行限定,保证数据的正确性,完整性,有效性. 分类:(1)主键约束 primary key(2)非空约束 not NULL (3)唯一约束 unique (4)外键 ...
- [51nod1383&1048]整数分解为2的幂:DP
算法一 分析 \(f[x]=f[x-1]+f[x/2] \times [x \equiv 0 \mod 2],O(n)\) 代码 n=int(input()) f=[0]*(n+5) f[0]=1 m ...
- Spring Cloud Stream教程(五)编程模型
本节介绍Spring Cloud Stream的编程模型.Spring Cloud Stream提供了许多预定义的注释,用于声明绑定的输入和输出通道,以及如何收听频道. 声明和绑定频道 触发绑定@En ...
- 满减 HRBUST - 2455
https://vjudge.net/problem/HRBUST-2455 有两种优惠方式,一是满400减100,另外一种是商品自带折扣,二者不可叠加 dp[i][j]表示前i种商品,(参与满400 ...
- 冲刺周日 Fighting SunDay
一.SunDay照片 二.项目分工 三.今日份燃尽图 四.项目进展 码云团队协同环境构建完毕 利用Leangoo制作任务分工及生成燃尽图 完成AES加解密部分代码 用代码实现对文件的新建.移动.复制. ...
- @清晰掉 GDB调试器中的战斗机
GDB 的命令很多,本文不会全部介绍,仅会介绍一些最常用的.在介绍之前,先介绍GDB中的一个非常有用的功能:补齐功能.它就如同Linux下SHELL中的命令补齐一样.当你输入一个命令的前几个字符,然后 ...