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): 9153 Accepted Submission(s): 2697 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
1 #include <stdio.h> 2 #include <string.h> 3 4 typedef struct _QUEUE 5 { 6 int curX, curY; //保存x,y坐标 7 int stepCount; //记录当前点的花费的时间 8 int pre; //保存前一个队列中的下标,用作保存路径 9 }QUEUE; 10 11 int N, M; 12 const int MAX_NUM = 105; 13 char maze[MAX_NUM][MAX_NUM]; //记录迷宫 14 int step[MAX_NUM][MAX_NUM]; //记录起点到某一点的最小花费时间 15 QUEUE qu[MAX_NUM * MAX_NUM * 30]; // 16 int dir[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}}; 17 int path[MAX_NUM * MAX_NUM][2]; //保存路径 18 19 void InitInput() //初始化输入 20 { 21 getchar(); 22 for(int row = 0; row < N; row++) 23 { 24 for(int col = 0; col < M; col++) 25 { 26 scanf("%c", &maze[row][col]); 27 } 28 getchar(); 29 } 30 } 31 32 void printPath(int tail) //打印路径 33 { 34 int pathMark = 0; 35 int head = qu[tail].pre; 36 path[pathMark][0] = N-1; 37 path[pathMark][1] = M-1; 38 pathMark++; 39 while(head != -1) //从后往前找到路径 40 { 41 path[pathMark][0] = qu[head].curX; 42 path[pathMark][1] = qu[head].curY; 43 pathMark++; 44 head = qu[head].pre; 45 } 46 47 int secCount = 0; 48 printf("It takes %d seconds to reach the target position, let me show you the way.\n", step[N-1][M-1]); 49 for(int k = pathMark-1; k >= 0; k--) 50 { 51 char ch = maze[ path[k][0] ][ path[k][1] ]; 52 if(ch >= '1' && ch <= '9') 53 { 54 int fightCount = ch - '0'; 55 while(fightCount--) 56 { 57 printf("%ds:FIGHT AT (%d,%d)\n", ++secCount, path[k][0], path[k][1]); 58 } 59 } 60 if(k != 0) 61 { 62 printf("%ds:(%d,%d)->(%d,%d)\n", ++secCount, path[k][0], 63 path[k][1], path[k-1][0], path[k-1][1]); 64 } 65 } 66 printf("FINISH\n"); 67 } 68 69 bool bfs() 70 { 71 memset(step, 0x7f, sizeof(step)); 72 qu[0].curX = qu[0].curY = 0; 73 qu[0].stepCount = 0; 74 qu[0].pre = -1; 75 76 int correctTail = -1; //记录到出口时的tail值,用作输出路径 77 int front = 0, tail = 0; 78 while(front <= tail) //队列不空时,循环 79 { 80 QUEUE tempP = qu[front]; 81 82 for(int k = 0; k < 4; k++) 83 { 84 QUEUE tempC; 85 tempC.curX = tempP.curX + dir[k][0]; 86 tempC.curY = tempP.curY + dir[k][1]; 87 88 if(tempC.curX<0 || tempC.curX>=N || tempC.curY<0 || tempC.curY>=M) 89 continue; //不在迷宫范围内 90 91 if(maze[tempC.curX][tempC.curY] == 'X') 92 continue; //该点为一个trap 93 94 if(maze[tempP.curX][tempP.curY] == '.') 95 { //前一个点为'.' 96 tempC.stepCount = tempP.stepCount + 1; 97 } 98 else 99 { //前一个点为数字 100 tempC.stepCount = tempP.stepCount 101 + maze[tempP.curX][tempP.curY] - '0' + 1; 102 } 103 if(tempC.stepCount < step[tempC.curX][tempC.curY]) 104 { //若到当前点花费的时间小于step[][],入队列 105 step[tempC.curX][tempC.curY] = tempC.stepCount; 106 qu[++tail] = tempC; 107 qu[tail].pre = front; 108 if(tempC.curX == N-1 && tempC.curY == M-1)//记录队尾的正确值 109 correctTail = tail; 110 } 111 } 112 front++; 113 } 114 if(correctTail > -1) //表明找到出口,打印路径 115 { 116 char ch = maze[N-1][M-1]; //若出口有怪兽,则需要花费时间 117 if(ch >= '1' && ch <= '9') 118 step[N-1][M-1] += ch - '0'; 119 printPath(correctTail); //打印路径 120 return true; 121 } 122 return false; 123 } 124 125 int main() 126 { 127 while(scanf("%d %d", &N, &M) != EOF) 128 { 129 InitInput(); 130 131 bool bResult = bfs(); 132 if(!bResult) 133 printf("God please help our poor hero.\nFINISH\n"); 134 } 135 return 0; 136 }
题目总结:这道题花了我好长时间来修改的,因为我们保存走过的路径,每次入队列时我都要保存当前点是由哪个点
通过上下左右拓展而来的,所以就记录前一个点的front值来实现当前点的前一个点在队列中的位置。但
由于我们保存正确的tail值,以至于打印不出正确的路径。最后我用变量correctTail来保存到出口的
花费最短时间的值在队列中的位置。第二个错误是我一开始在循环的时候,找到了出口就打印路径,实际
上当找到出口时,由于队列还不一定为空,以至于此时找到的值不一定最小,因此,要把所有的路径都搜
索完成后,才能输出路径,不然又要出错。上面就是我遇到的两个错误,只能说自己题目做的有点少,以
至于自己考虑的很不全面吧!以后自己会多多注意总结的!