搜索专题: 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在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...
随机推荐
- python中的时间模块
废话不多说,看代码 import datetime,time import calendar #时间戳 t1 = time.time() print('当前时间戳是{}'.format(t1)) #格 ...
- Java面试题系列(七)锁的原理
redis实现分布式锁 synchronized 和 reentrantlock的区别,偏向锁/轻量级锁/重量级锁的原理,能否从偏向锁直接升级成重量级锁
- 以Emacs Org mode为核心的任务管理方案
前言 如今用于任务管理的方法与工具越来越多,如纸笔系统.日历与任务列表.Emacs Org mode系统,以及移动设备上的诸多应用.这些解决方案各具特色,在一定程度上能够形成互补作用.但是,它们彼此之 ...
- pyinstaller打包的exe太大?你需要嵌入式python玄学 惊喜篇
上篇讲到 pyinstaller打包exe太大的问题 CodingDog:pyinstaller打包的exe太大?你需要嵌入式python玄学 前提篇zhuanlan.zhihu.com 那既然py ...
- webpack插件之htmlWebpackPlugin
webpack插件之htmlWebpackPlugin webpack插件 自动化 htmlWebpackPlugin 由于webpack已经帮我们处理好js之间的依赖关系,现在我们可以忽略js的加 ...
- leetcode-easy-array-1 two sum
mycode 33.91% class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i ...
- leetcode-mid-sorting and searching - 33. Search in Rotated Sorted Array
mycode class Solution(object): def search(self, nums, target): """ :type nums: List[i ...
- 修改win10 capslock键成esc键 vim
桌面编辑一个文件CapsLock2Esc.reg Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentCont ...
- 1、Shiro简介以及整体架构
1.Shiro概念和作用: 利用shiro可以快速完成权限管理模块的开发 Spring的官网也是用Shiro做安全管理的... Shiro整体架构: 可能你感觉上面的图片很乱,但是你一定要先大体有个印 ...
- docker-容器完整构建过程
container 代码app,构建,运行,分享(推送)image mkdir img1 cd img1 [root@cu-tmp-201 img1]# ls app.py Dockerfile re ...