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. 转载:最近有两款路由器D-link , Tenda分别被爆出固件中存在后门

    最近有两款路由器分别被爆出固件中存在后门. D-link D-link是台湾公司,成立于1986年,『公司致力于高级网络.宽带.数字.语音和数据通信解决方案的设计.制造和营销,是业界的全球领导者』(官 ...

  2. 关于iOS地图定位中点击设置->隐私->定位服务 闪退问题

    iOS8之后,如果应用中用到了地图定位,那么点击设置->隐私->定位服务 再点击该应用有时候会出现闪退问题,其原因是iOS8之后定位中添加了 NSLocationWhenInUseDesc ...

  3. 【MongoDB】MongoDB 3.2 SCRAM-SHA-1验证方式

    新版本已取消addUser方法,改使用createUser方法 官方地址:https://docs.mongodb.com/manual/tutorial/create-users/ 官方地址:htt ...

  4. android开发--下载图片

    1.背景介绍 网络上图片的请求,是我们最常见的网络请求之一,不亚于对json/xml数据的请求.一般要展示给用户看的,都不会是纯粹的文字,往往都是图文信息.而在移动互联网时代,图文又往往需要最新的资讯 ...

  5. NK3C程序资源占用分析

    1.程序放在一个Tomcat下最低配置推荐:最大堆:768M,最大PermGen:160M(-Xmx768m -XX:MaxPermSize=160m) 2.机器最低配置推荐:最小内存2G 3.正式运 ...

  6. Android 5.0属性

    //水波纹效果//v 指定控件 x屏幕的 x轴 y轴 endRadio 起始位置 水波半径Animator circularReveal = ViewAnimationUtils.createCirc ...

  7. 解析json

    String json = "{\"elements\":[{\"distance\":{\"text\":\"1364 ...

  8. SqlLocalDB使用笔记

    一.介绍 SqlLocalDB是VS安装时附带的数据库软件,相当于精简版的SQL Express. 二.使用 VS版本为2015,默认安装位置为:C:\Program Files\Microsoft ...

  9. Win8.1无法安装.NET Framework 3.5的解决办法

    这个问题纠结了我很多天,恢复系统也没用,差点儿就重装Win8,现在终于解决了,你也来试试吧! 机型:台电X89 系统:Win8.1 with bing 故障:在未安装.NET Framework 3. ...

  10. 省市县三级联动(webFrom...DropdownList)

    编辑页面 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"&g ...