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

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)就行。

最后递归输出下结果。

  1 //0MS    1688K    1977B    G++
  2 #include<iostream>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<string.h>
  6
  7 using namespace std;
  8 const int MAXN = 0xffffff;
  9
 10 struct Node{
 11     int x;
 12     int y;
 13     int step;
 14     char c;
 15 };
 16 int n,m, ans;
 17 int mov[4][2]={0,1,1,0,0,-1,-1,0};
 18 int nxt[105][105];
 19 int map[105][105];
 20 char g[105][105];
 21
 22 void bfs(int sx, int sy)
 23 {
 24     queue<Node>Q;
 25     Node node;
 26     if(g[sx][sy] != ‘X‘){
 27         node.x=sx;
 28         node.y=sy;
 29         node.step=0;
 30         node.c = g[sx][sy];
 31         Q.push(node);
 32         map[sx][sy]=-1;
 33     }
 34     while(!Q.empty()){
 35         node = Q.front();
 36         Q.pop();
 37         if(node.x == n-1 && node.y==m-1){
 38             if(ans > node.step){
 39                 ans = node.step + ((g[node.x][node.y]==‘.‘)?0:(g[node.x][node.y]-‘0‘));
 40             }
 41             break;
 42         }
 43         node.step += 1;
 44
 45         if(node.c != ‘.‘ && node.c != ‘0‘){
 46             node.c -= 1;
 47             Q.push(node);
 48             continue;
 49         }
 50         for(int i=0;i<4;i++){
 51             int tx = node.x + mov[i][0];
 52             int ty = node.y + mov[i][1];
 53
 54             if(tx>=0 && tx<n && ty>=0 && ty<m && g[tx][ty]!=‘X‘ && map[tx][ty]!=-1){
 55                 Node tnode = {tx, ty, node.step, g[tx][ty]};
 56                 Q.push(tnode);
 57                 map[tx][ty] = -1;
 58                 nxt[tx][ty] = i;
 59             }
 60         }
 61     }
 62 }
 63
 64 void print(int x, int y, int sec)
 65 {
 66
 67     if(sec <= 0) return;
 68     int id = nxt[x][y];
 69
 70     int use = (g[x][y]==‘.‘)?0:(g[x][y]-‘0‘);
 71     print(x-mov[id][0], y-mov[id][1], sec-1-use);
 72
 73     if(sec- use > 0)
 74         printf("%ds:(%d,%d)->(%d,%d)\n", sec-use, x-mov[id][0], y-mov[id][1], x, y);
 75     if(g[x][y]!=‘X‘){
 76         for(int i=use-1;i>=0;i--){
 77             printf("%ds:FIGHT AT (%d,%d)\n", sec-i, x, y);
 78         }
 79     }
 80
 81 }
 82
 83 int main()
 84 {
 85     while(scanf("%d%d",&n,&m)!=EOF){
 86
 87         for(int i=0;i<n;i++){
 88             scanf("%s", &g[i]);
 89         }
 90
 91         memset(map, 0, sizeof(map));
 92         memset(nxt, 0, sizeof(nxt));
 93         ans = MAXN;
 94         bfs(0, 0);
 95
 96         if(ans != MAXN){
 97             printf("It takes %d seconds to reach the target position, let me show you the way.\n", ans);
 98             print(n-1, m-1, ans);
 99         }else{
100             puts("God please help our poor hero.");
101         }
102         puts("FINISH");
103     }
104     return 0;
105 }

时间: 2024-12-08 14:16:38

hdu 1026(Ignatius and the Princess I)BFS的相关文章

HDU 4902 (牛叉的线段树)

Nice boat Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and c

Hdu 2473(并查集删除操作) Junk-Mail Filter

有木有很吊 加强 加强版   啊  ,看了都不敢做了   ,后来先做了食物链这个我还是看过的,但还是A不掉,没明白神魔意思 ,总而言之,大牛的博客是个好东西,我就那么看了一下,还是不懂怎莫办啊,哎,就那样就A掉了....... 今天我们来谈一下这个并查集的删除操作,根据我对大牛的理解啊,这个并查集的删除操作并不是把原来的节点删除掉,而是用一个替身替掉,现在的这个点只是用作桥梁的作用,即是无用的,del  ,,,del  ,,,,删除,那些被删掉的就从n开始给他们一个地址,然后即如下代码所示 #i

HDU 4907 (杭电BC#3 1001)Task schedule(水)

题目地址:HDU 4907 水题...不多说..哈希后从后往前遍历一遍就行了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map> #include<a

hdu 2516(斐波拉切博弈)

题意:容易理解. 分析:通过枚举寻找规律,这就是做1堆或者2堆石子博弈的技巧!当为2或者3时,肯定是第二个人赢,当为4时,先去一个石子,然后当对方面临3,于是第一个人赢, 当为5时,取1时,第二个人赢,取2时也是第二个人赢...,于是为5时也是滴二个人赢...多枚举几个之后就会发现只要满足斐波拉切数列的都是第二个人赢,其它的 则是第一个人赢! 代码实现: #include<stdio.h> #include<string.h> int main() { int n,i,t1,t2,

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

Ignatius and the Princess IV  先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input 本题包含多组数据,请处理到EOF: 每组数据包含两行. 第一行一个数字N(1<=N<=999999) ,保证N为奇数. 第二行为N个用空格隔开的整数. Output 对于每组数据,输出一行,表示要求找到的那个数 Sample Input 5 1 3 2 3 3 11 1 1 1 1 1 5 5 5

hdoj 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): 12750    Accepted Submission(s): 4034 Special Judge Problem Description The Princess has been abducted by the BEelzeb

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 (Java/Others)Total Submission(s): 25929    Accepted Submission(s): 17918 Problem Description "Well, it seems the first problem is too easy. I will let

HDU1026 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): 11710    Accepted Submission(s): 3661 Special Judge Problem Description The Princess has been abducted by the BEelzeb

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

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1010 Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 144191    Accepted Submission(s): 38474 Problem Description The doggie fou