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

Problem Description
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth 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.

 
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
 
Output
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum 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.
 
Sample Input
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.
 
Sample Output
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
 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for you:  1072 1175 1180 1043 1254 
 
 
 
广搜题目,代码写得有点烂。。将就下吧
 
开始用深搜试了下,果然是超时的。
后来用广搜,路径输出有点麻烦,看了下别人的思路,可以用个二维数组记下每到下个点该往哪走(四个方位,记下0~3)就行。
最后递归输出下结果。
 
 //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的更多相关文章

  1. 线段树扫描线(一、Atlantis HDU - 1542(覆盖面积) 二、覆盖的面积 HDU - 1255(重叠两次的面积))

    扫描线求周长: hdu1828 Picture(线段树+扫描线+矩形周长) 参考链接:https://blog.csdn.net/konghhhhh/java/article/details/7823 ...

  2. 【HDU - 1029】Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV  先搬中文 Descriptions:   给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...

  3. 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 ...

  4. HDU 5517---Triple(二维树状数组)

    题目链接 Problem Description Given the finite multi-set A of n pairs of integers, an another finite mult ...

  5. hdu 1010(迷宫搜索,奇偶剪枝)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Ja ...

  6. 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 ...

  7. hdu Portal(离线,并查集)

    题意:在一张无向图上,已知边权,做q组询问,问小于L的点对共有几组.点对间的距离取=min(两点之间每一条通路上的最大值). 分析:这里取最大值的最小值,常用到二分.而这里利用离线算法,先对边从小到大 ...

  8. Tunnel Warfare HDU - 1540 (线段树处理连续区间问题)

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  9. (dfs痕迹清理兄弟篇)bfs作用效果的后效性

    dfs通过递归将每种情景分割在不同的时空,但需要对每种情况对后续时空造成的痕迹进行清理(这是对全局变量而言的,对形式变量不需要清理(因为已经被分割在不同时空)) bfs由于不是利用递归则不能分割不同的 ...

随机推荐

  1. C#设计模式——解释器模式(Interpreter Pattern)

    一.概述 在软件开发特别是DSL开发中常常需要使用一些相对较复杂的业务语言,如果业务语言使用频率足够高,且使用普通的编程模式来实现会导致非常复杂的变化,那么就可以考虑使用解释器模式构建一个解释器对复杂 ...

  2. Spring 之注解事务 @Transactional

    众所周知的ACID属性:  原子性(atomicity).一致性(consistency).隔离性(isolation)以及持久性(durability).我们无法控制一致性.原子性以及持久性,但可以 ...

  3. cmd执行mysql操作

    (以下已安装到本机的mysql为例) 登录mysql数据库,如果没有在环境变量配置path到mysql中的bin目录,需要手动进入该目录中 执行:mysql -u用户名 -p密码 (注意:只要进入了m ...

  4. DMA控制

    1.S3C6410中DMA操作步骤 S3C6410中DMA操作步骤:1.决定使用安全DMAC(SDMAC)还是通用DMAC(DMAC):2.开始相应DMAC的系统时钟,并关闭另外一组的时钟(系统默认开 ...

  5. JAVA获取客户端IP地址

    在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...

  6. Spark Streaming源码解读之No Receivers彻底思考

    本期内容 : Direct Acess Kafka Spark Streaming接收数据现在支持的两种方式: 01. Receiver的方式来接收数据,及输入数据的控制 02. No Receive ...

  7. 《算法导论》 调用RANDOM(0,1),实现RANDOM(a,b)的过程

    描述RANDOM(a,b)的过程的一种实现,它只调用RANDOM(0,1).作为a和b的函数,你的程序的期望运行时间是多少?(RANDOM(0,1)以等概率输出0或者1,RANDOM(a,b)以等概率 ...

  8. 简单三个表之间关联 与 case when语句的应用

    select p.conttract_id,l.order_code,jz.cur_branch from wy_auto_workflow_log l,wg_pjhsb p,wg_jzmb jz w ...

  9. python读取excel数据,并可视化展现

    #-*- coding: utf-8 -*- import pandas as pda import matplotlib.pyplot as pyl import matplotlib.font_m ...

  10. 为什么C语言中的数组序号都是从0开始

    这个规则并不是在所有计算机语言上通行的,例如Matlab上就是从1开始. 这个规则是从内存寻址设计上继承来的,因为在如100个元素的数组对应的内存单元中,从内存地址位0开始到内存地址为99,总共记录9 ...