hdu 1026(Ignatius and the Princess I)BFS
Posted heaventouch
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
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
广搜题目,代码写得有点烂。。将就下吧
开始用深搜试了下,果然是超时的。
后来用广搜,路径输出有点麻烦,看了下别人的思路,可以用个二维数组记下每到下个点该往哪走(四个方位,记下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 }
以上是关于hdu 1026(Ignatius and the Princess I)BFS的主要内容,如果未能解决你的问题,请参考以下文章
hdu 1026(Ignatius and the Princess I)BFS
HDU_1026_Ignatius and the Princess I_BFS(保存路径)
HDU 1026 Ignatius and the Princess I(BFS+记录路径)
HDU 1026 Ignatius and the Princess I(BFS+路径输出)