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 (Java/Others)
Total Submission(s): 18847 Accepted Submission(s): 6090
Special Judge
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.
/*
Name: hdu--1026--Ignatius and the Princess I
Copyright: ©2017 日天大帝
Author: 日天大帝
Date: 21/04/17 17:30
Description: bfs搜索路径,dfs打印路径
*/
#include<cstring>
#include<queue>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node{
int x,y,steps;
bool operator<(const node&a)const{
return steps>a.steps;
}
};
int bfs();
void dfs(int,int);
;
int map[MAX][MAX];
int to[MAX][MAX];
int cnt[MAX][MAX];
int n,m,ct;
][] = {{,},{,-},{,},{-,}};
int main(){
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
while(cin>>n>>m){
memset(map,,sizeof(map));
memset(to,,sizeof(to));
memset(cnt,,sizeof(cnt));
; i<n; ++i){
; j<m; ++j){
char ch;cin>>ch;
if(ch == '.'){
map[i][j] = ;
};
';
}
}
int ans = bfs();
if(ans){
cout<<"It takes "<<ans<<" seconds to reach the target position, let me show you the way."<<endl;
ct = ;
dfs(n-,m-);
}else printf("God please help our poor hero.\n");
cout<<"FINISH\n";//G++WA无数次,hdu C++ AC了
}
;
}
void dfs(int x,int y){
if(!to[x][y])return ;
int i,j;
i = x - dir[to[x][y]-][];//剪枝
j = y - dir[to[x][y]-][];//剪枝
dfs(i,j);
printf("%ds:(%d,%d)->(%d,%d)\n",ct++,i,j,x,y);
while(cnt[x][y]--) {
printf("%ds:FIGHT AT (%d,%d)\n",ct++,x,y);
}
}
int bfs(){
node start;
start.x = ;
start.y = ;
start.steps = ;
map[][] = -;
priority_queue<node> q;
q.push(start);
while(!q.empty()){
node a,temp = q.top();q.pop();
&& temp.y == m-)return temp.steps;
; i<; ++i){//i<4(n)
a.x = temp.x + dir[i][];
a.y = temp.y + dir[i][];
||a.y< || map[a.x][a.y] == -)continue;
a.steps = temp.steps + map[a.x][a.y] + ;
map[a.x][a.y] = -;
to[a.x][a.y] = i+;//剪枝
q.push(a);
}
}
;
}
hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))的更多相关文章
- hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- HDU 1026 Ignatius and the Princess I(BFS+记录路径)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Description The Prin ...
- hdu 1026 Ignatius and the Princess I【优先队列+BFS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
Ignatius and the Princess I Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I(bfs)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu 1026 Ignatius and the Princess I 搜索,输出路径
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 1026 Ignatius and the Princess I(带路径的BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1026 题意:给出一个迷宫,求出到终点的最短时间路径. 这道题目在迷宫上有怪物,不同HP的怪物会损耗不同的时间,这 ...
随机推荐
- Spring Boot 自动重启(spring-boot-devtools)
原文 https://github.com/x113773/testall/issues/8 1. 首先添加依赖```<dependency><groupId>org.spri ...
- JavaScript函数的各种调用模式
函数是JavaScript世界里的第一公民,换句话来说,就是我们如果可以精通JavaScript函数的使用,那么对JavaScript的运用可以更游刃有余了.熟悉JavaScript的人应该都知道,同 ...
- JavaScript 语言基础
js语言基础 一 基本知识 UniCode编码 区分大小写(HTML不区分/XHTML区分) Unicode转义序列 \uxxxx (\u加4位16进制表示) 注释 单行注释:// 多行注释:/* * ...
- css加载会造成阻塞吗?
终于考试完了,今天突然想起来前阵子找实习的时候,今日头条面试官问我,js执行会阻塞DOM树的解析和渲染,那么css加载会阻塞DOM树的解析和渲染吗?所以,接下来我就来对css加载对DOM树的解析和渲染 ...
- [javascript 实践篇]——那些你不知道的“奇淫巧技”
1. 空(null, undefined)验证 刚开始,我是比较蠢的验证(我还真是这样子验证的) if (variable1 !== null || variable1 !== undefined | ...
- jstack
简介 jstack用于打印出给定的java进程ID的Java堆栈信息,一般用于检查应用的线程问题,死锁问题 常用命令 jstack 输出 $ jstack 11376 2014-01-21 20:36 ...
- windows安装程序无法将windows配置为在此计算机的硬件上运行
关于装windows系统时,出现一些安装中断的处理 该方法适用于 windows安装程序无法将windows配置为在此计算机的硬件上运行 计算机意外地重新启动或遇到错误. Windows 安装无法继续 ...
- 基于PlatinumKit库的DLNA服务端开发
首先,需要特别感谢蓝斯老师的资料,我这里有很大一部分是参考蓝斯老师的,附上传送门:http://blog.csdn.net/lancees/article/details/9178385 一.DLNA ...
- DotNetCore跨平台~服务总线_事件总线的重新设计
理论闲话 之前在.netFramework平台用的好好的,可升级到.net core平台之后,由于不再需要二进制序列化,导致咱们的事件机制遇到了问题,之前大叔的事件一直是将处理程序序列化后进行存储的, ...
- Java 用Freemarker完美导出word文档(带图片)
Java 用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...