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由于不是利用递归则不能分割不同的 ...
随机推荐
- mac-android-虚机加速
mac下进行android编程不可避免会碰到android虚拟机问题,macbook pro启动虚机时报错: Starting emulator for AVD 'NEW' emulator: ERR ...
- 编译安装带ssl 模块指定版本Python
出现这个或者fatal error: openssl/名单.h: No such file or directory.都是没有安装libssl-dev- libssl-dev包含libraries ...
- 【小错误】SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled
1.今天在scott用户下执行语句跟踪时报了如下错误: SCOTT@ORA11GR2>set autotrace on SP2: Cannot find the Session Identifi ...
- adb server无法终止问题
这两天通过python去连接Android手机时,一直提示:Adb connection Error:An existing connection was forcibly closed by the ...
- python2.7.9基础学习
一. 基 础 python python开头两行注释代码意义: #!/usr/bin/python 是用来说明脚本语言是python的,是要用/usr/bin下面的程序(工具)py ...
- Mysql 数据库创建基本步骤
1.创建数据库 create database school; 2.使用数据库 Use school; 3.创建用户 create user jame@localhost identified by ...
- jquery让滚动条跳到最底部
selector.scrollTop(50000); 添加一个最大的数值: 或者 公式:内容器的高度加上外层容器的padding,再减去外层容器的高度: var tableHeight = $(' ...
- encodeURI来解决URL传递时的中文问题
在AJAX浏览器来进行发送数据时,一般它所默认的都是UTF-8的编码. 使用JQUERY中所提供的方法来做操作 encodeURI function verify() { //解决中文乱麻问题的 ...
- 19.dnw打不开
用dnw.exe烧写文件时,突然出现电脑死机,重启后,dnw就打不开了,dnw.ini的原因,该文件在c盘目录下,只需要删除它,就可以继续使用dnw了.
- myeclipse中将整块的代码所选中的代码左右移动的快捷键
myeclipse中将整块的代码/所选中的代码左右移动的快捷键选择你要移动的代码,TAB 右移ctrl+TAB左移(我的使用Shift+TAB管用)要是不使用快捷键就是:选中代码,点击右键选中Shif ...