HDU 1242 Rescue(BFS)
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11252 Accepted Submission(s): 4112
Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
1 #include <stdio.h> 2 #include <string.h> 3 4 typedef struct _Q 5 { 6 int x, y; 7 int stepCount; 8 }Q; 9 10 int M, N; //记录迷宫的行数和列数 11 int sX, sY, eX, eY; //记录要搜索的开始位置和结束位置 12 const int MAX_NUM = 205; 13 char maze[MAX_NUM][MAX_NUM]; //记录迷宫 14 int step[MAX_NUM][MAX_NUM]; //记录到达某一个点的最小时间 15 bool bResult; 16 Q qu[MAX_NUM*MAX_NUM]; //定义队列 17 int recRow[MAX_NUM]; //记录多个朋友所在的行数 18 int recCol[MAX_NUM]; //记录多个朋友所在的列数 19 int dir[4][2] = {{-1,0}, {1,0}, {0,-1}, {0,1}}; 20 21 bool bfs() 22 { 23 qu[0].x = sX; 24 qu[0].y = sY; 25 qu[0].stepCount = 0; 26 memset(step, 0x7f, sizeof(step)); 27 28 int front = -1, tail = 0; 29 while(front < tail) 30 { 31 Q tempP = qu[++front]; 32 if(tempP.x == eX && tempP.y == eY) 33 { 34 return true; 35 } 36 for(int k = 0; k < 4; k++) 37 { 38 Q tempC; 39 tempC.x = tempP.x + dir[k][0]; 40 tempC.y = tempP.y + dir[k][1]; 41 if(tempC.x < 0 || tempC.x >= N)continue; 42 if(tempC.y < 0 || tempC.y >= M)continue; 43 if(maze[tempC.x][tempC.y] == '#')continue; 44 45 if(maze[tempP.x][tempP.y] == '.') 46 tempC.stepCount = tempP.stepCount + 1; 47 else if(maze[tempP.x][tempP.y] == 'x') 48 tempC.stepCount = tempP.stepCount + 2; 49 if(tempC.stepCount < step[tempC.x][tempC.y]) 50 { 51 step[tempC.x][tempC.y] = tempC.stepCount; 52 qu[++tail] = tempC; 53 } 54 } 55 } 56 return false; 57 } 58 59 int main() 60 { 61 while(scanf("%d %d", &N, &M) != EOF) 62 { 63 int cnt = 0; 64 getchar(); 65 for(int row = 0; row < N; row++) 66 { 67 for(int col = 0; col < M; col++) 68 { 69 scanf("%c", &maze[row][col]); 70 if(maze[row][col] == 'r') //将朋友的位置保存起来 71 { 72 recRow[cnt] = row; 73 recCol[cnt++] = col; 74 maze[row][col] = '.'; 75 } 76 if(maze[row][col] == 'a') 77 { 78 eX = row; eY = col; 79 maze[row][col] = '.'; 80 } 81 } 82 getchar(); 83 } 84 85 bResult = false; 86 int min_step = 0x7ffffff; 87 for(int k = 0; k < cnt; k++) //对每个朋友的位置进行搜索 88 { 89 sX = recRow[k]; 90 sY = recCol[k]; 91 bResult = bfs(); 92 if(bResult) 93 { 94 if(step[eX][eY] < min_step)//找到最小的位置 95 min_step = step[eX][eY]; 96 } 97 } 98 if(bResult) 99 printf("%d\n", min_step); 100 else 101 printf("Poor ANGEL has to stay in the prison all his life.\n"); 102 } 103 104 return 0; 105 }