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 (Java/Others)
Total Submission(s): 17930 Accepted Submission(s): 5755
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.
.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.
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
//0MS 1688K 1977B G++
#include<iostream>
#include<queue>
#include<algorithm>
#include<string.h> using namespace std;
const int MAXN = 0xffffff; struct Node{
int x;
int y;
int step;
char c;
};
int n,m, ans;
int mov[][]={,,,,,-,-,};
int nxt[][];
int map[][];
char g[][]; void bfs(int sx, int sy)
{
queue<Node>Q;
Node node;
if(g[sx][sy] != 'X'){
node.x=sx;
node.y=sy;
node.step=;
node.c = g[sx][sy];
Q.push(node);
map[sx][sy]=-;
}
while(!Q.empty()){
node = Q.front();
Q.pop();
if(node.x == n- && node.y==m-){
if(ans > node.step){
ans = node.step + ((g[node.x][node.y]=='.')?:(g[node.x][node.y]-''));
}
break;
}
node.step += ; if(node.c != '.' && node.c != ''){
node.c -= ;
Q.push(node);
continue;
}
for(int i=;i<;i++){
int tx = node.x + mov[i][];
int ty = node.y + mov[i][]; if(tx>= && tx<n && ty>= && ty<m && g[tx][ty]!='X' && map[tx][ty]!=-){
Node tnode = {tx, ty, node.step, g[tx][ty]};
Q.push(tnode);
map[tx][ty] = -;
nxt[tx][ty] = i;
}
}
}
} void print(int x, int y, int sec)
{ if(sec <= ) return;
int id = nxt[x][y]; int use = (g[x][y]=='.')?:(g[x][y]-'');
print(x-mov[id][], y-mov[id][], sec--use); if(sec- use > )
printf("%ds:(%d,%d)->(%d,%d)\n", sec-use, x-mov[id][], y-mov[id][], x, y);
if(g[x][y]!='X'){
for(int i=use-;i>=;i--){
printf("%ds:FIGHT AT (%d,%d)\n", sec-i, x, y);
}
} } int main()
{
while(scanf("%d%d",&n,&m)!=EOF){ for(int i=;i<n;i++){
scanf("%s", &g[i]);
} memset(map, , sizeof(map));
memset(nxt, , sizeof(nxt));
ans = MAXN;
bfs(, ); if(ans != MAXN){
printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
print(n-, m-, ans);
}else{
puts("God please help our poor hero.");
}
puts("FINISH");
}
return ;
}
hdu 1026(Ignatius and the Princess I)BFS的更多相关文章
- 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))
扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...
- 【HDU - 1029】Ignatius and the Princess IV (水题)
Ignatius and the Princess IV 先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...
- hdu 1028 Sample Ignatius and the Princess III (母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 5517---Triple(二维树状数组)
题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...
- hdu 1010(迷宫搜索,奇偶剪枝)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...
- HDU1026 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 Portal(离线,并查集)
题意:在一张无向图上,已知边权,做q组询问,问小于L的点对共有几组.点对间的距离取=min(两点之间每一条通路上的最大值). 分析:这里取最大值的最小值,常用到二分.而这里利用离线算法,先对边从小到大 ...
- Tunnel Warfare HDU - 1540 (线段树处理连续区间问题)
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- (dfs痕迹清理兄弟篇)bfs作用效果的后效性
dfs通过递归将每种情景分割在不同的时空,但需要对每种情况对后续时空造成的痕迹进行清理(这是对全局变量而言的,对形式变量不需要清理(因为已经被分割在不同时空)) bfs由于不是利用递归则不能分割不同的 ...
随机推荐
- Maven如何添加Oracle驱动包到本地仓库中
2016-05-28 http://note.youdao.com/yws/public/redirect/share?id=54cbe79c8948113ef492f946f7e027c8& ...
- jQuery中关于height,innerWidth与outerWidth的区别
jQuery width() 和 height() 方法 width() 方法设置或返回元素的宽度(不包括内边距.边框或外边距). height() 方法设置或返回元素的高度(不包括内边距.边框或外边 ...
- ABP的第一个程序和遇到的一些问题
ABP在这里就不多介绍了.在这篇文章中主要介绍使用模板生成的ABP项目使用遇到的一些问题. 1.首先在http://www.aspnetboilerplate.com/ 官方网站上创建一个模板项目, ...
- mysql下将分隔字符串转换为数组
MySQL存储过程可以用于分割字符串,下面就为您详细介绍这种MySQL存储过程的用法,供您参考学习之用. 现有一段字符串,如apple,banana,orange,pears,grape,要把它按照逗 ...
- webpack 往右一点之 “模块这个东西”
为什么会考虑模块? webapp,页面初始化和使用过程中会加载越来越多的javascript代码 --- 给前端的开发流程和资源组织带来挑战 ---前端需要模块系统 模块系统是干什么的呢? 模块的定义 ...
- PIC32MZ tutorial -- UART Communication
At this moment, I accomplish the interface of UART communication for PIC32MZ EC Starter Kit. This in ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- [Python] MySQLdb(即 MySQL-python 包)在 OS X 中安装指南
本文参考:http://www.cnblogs.com/ifantastic/archive/2013/04/13/3017677.html 安装环境:OS X 操作系统,Python 2.7.10 ...
- Parse the main function arguments
int main(int argc, char **argv) { std::string reportDir; std::string transURL; std::string visualEle ...
- AngularJS 2 VS Code Linter环境设置
Angular Cli npm install -g angular-cli https://www.npmjs.com/package/angular-cli TSLinter 1.1 ext in ...