搜索专题: 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在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...
随机推荐
- Mybatis-Plus和Mybatis的区别
1.List item 区别一如果Mybatis Plus是扳手,那Mybatis Generator就是生产扳手的工厂.通俗来讲——MyBatis:一种操作数据库的框架,提供一种Mapper类,支持 ...
- Python抽象类(abc模块)
1.抽象类概念 抽象类是一个特殊的类,只能被继承,不能实例化 2.为什么要有抽象类 其实在未接触抽象类概念时,我们可以构造香蕉.苹果.梨之类的类,然后让它们继承水果这个基类,水果的基类包含一个eat函 ...
- webUploader---实现大文件断点续传
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- event.stopPropagation()和event.preventDefault(),return false的区别
我写公司的官网遇到一个问题,轮播图的上一层有一块内容,用鼠标拖动那块内容的时候下一层的轮播图也会跟着拖动,而上面的那层的内容是不会动的,我想这就是冒泡事件在作祟了吧 跟冒泡事件相关的,我想到三个: 1 ...
- CSS实现二维码扫描的效果
扫描二维码的效果,我原以为不好写呢,后来想了想其实挺简单的,几行代码,走起 <div class="code-bg"> <div class="line ...
- Android如何安装系统应用,及自己增加安装系统应用的接口
根据SIM卡安装系统应用 功能: 1:如何安装系统应用,apk放在system/app系统分区下面. 2:根据SIM卡的归属国家选择性的安装应用. 一:本人使用方法: 在开机的服务里面添加接口(Pac ...
- intellij idea中去除@Autowired注入对象的红色波浪线提示
idea中通过@Autowired注入的对象一直有下划线提示. 解决:改变@Autowired的检查级别即可. 快捷键:Ctrl+Alt+s,进入idea设置界面,输入inspections检索
- 20165220课程设计个人报告——Part4-Cortex M4模块
个人报告: 20165220 葛宇豪 1.个人贡献 a.实验环境搭建 b.代码分析与理解 2.设计中遇到的问题以及解决方案 问题1:mdk5每次编译之前都会直接闪退 刚开始以为是环境问题,后来上网查资 ...
- js实现两个从input获取到的数字相加引发的问题
从input中获取到的数据是文本类型的,如果不转化类型直接相加会变成字符串的相加. 使用Number()函数可以解决这个问题,如下 var c = Number(a) + Number(b)
- nginx请求转发配置
以下为无ssl证书配置的请求转发 server { listen ; server_name api.****.com; #以下为指定请求域名匹配到某一个端口 #location ~* /union ...