搜索专题: 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在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...
随机推荐
- Nginx做反向代理时访问端口被自动去除
使用的Nginx版本 : nginx/1.13.10 出现问题的配置文件如下 upstream http-web { server 0.0.0.0:9000; } server { listen 80 ...
- udp拼接传递数据包
1.拼接项少 pl = ["<0112>","<32>","<1024x768>","< ...
- [CF780C]Andryusha and Colored Balloons 题解
前言 完了,完了,咕值要没了,赶紧写题解QAQ. 题意简述 给相邻的三个节点颜色不能相同的树染色所需的最小颜色数. 题解 这道题目很显然可以用深搜. 考虑题目的限制,如果当前搜索到的点为u, 显然u的 ...
- Spring Cloud云架构 - commonservice-sso服务搭建(一)
前面几篇我们已经介绍了Spring Cloud和oauth2的知识点,今天我们要利用Spring Cloud和oauth2进行commonservice-sso服务搭建,本节我们只是搭建commons ...
- eclipse代码自动补全设置
1.说明 eclipse安装好了之后,在编辑框中输入某个英文字符,默认不自动弹出自动代码选择框,需要手动按下 Alt + / 或者输入的字符为 . 才弹出代码自动补全框.其实eclipse是可以设置 ...
- Java 生成二进制加减法题目
日常算数,有益身心健康. int a; int b; int result; int symbol; int count = 50; Random random = new Random(); for ...
- (知识)width、naturalWidth、clientWidth、offsetWidth区别整理
今天在做图片裁剪功能的时候,参考了下网友的资料,发现大家对图片宽度的获取方式不尽相同,于是详细整理下各个属性的区别(详细请参考MDN). 1,HTMLImageElement.width是一个unsi ...
- jsp四种属性范围
在JSP提供了四种属性的保存范围.所谓的属性保存范围,指的就是一个设置的对象,可以在多个页面中保存并可以继续使用.它们分别是:page.request.session.appliction. 1.pa ...
- webpack 自动运行,及打包 img css json 的操作 npm插件的使用方法
没有指令操作的属性生产环境,有指令操作的属于开发环境 webpack:输入指令后,便会自动开启一个浏览器 需要插件:open-browser-webpack-plugin 生产环境 想使用 node. ...
- C#的keyValue