搜索算法13之1012

1題目編號:1012

2 題目內容:

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 &lt;= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.<br><br>Angel's friends want to save Angel. Their task is: approach Angel. We assume that &quot;approach Angel&quot; 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.<br><br>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.)<br>
 

Input
First line contains two integers stand for N and M.<br><br>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. <br><br>Process to the end of the file.<br>
 

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." <br>
 

Sample Input
7 8<br>#.#####.<br>#.a#..r.<br>#..#x...<br>..#..#.#<br>#...##..<br>.#......<br>........<br>
 

Sample Output
13<br>
 

Author
CHEN, Xue
 

Source
ZOJ Monthly, October 2003
 

3 解題思路形成過程:BFS。之前理解錯題意,以爲每次只有一個‘r’,所以WA了,後來想起了上週上課講的例題,和這個題非常相似,思路也應該是一樣的,即可以有多個‘r’的,這樣按原來把‘r’作爲起始點開始搜索的思路就錯誤了,因爲不知道從哪個‘r’開始會取得最短時間,而且多個‘r’儲存和操作起來也比較麻煩,所以後來換了一個思路:如果某一個r能找到a,那麼a一定能找到這個r 。這樣以來我們不必從多個r同時出發去找a,可以直接讓a去找離他最近的就行了!按照這思路就容易多了…

4 代碼:#include<stdio.h>


#include<queue>


using namespace std;


typedef struct


{


int i, j, time;


}point;


int n, m;


int dir[4][2] = { { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 } };


char map[201][201];


point start;


int bfs(point start)


{


queue<point>q;


int i;


point cur, next;


start.time = 0;


map[start.i][start.j] = '#';


q.push(start);


while (!q.empty())


{


cur = q.front();


q.pop();


for (i = 0; i<4; i++)


{


next.i = cur.i + dir[i][0];


next.j = cur.j + dir[i][1];


if (map[next.i][next.j] == 'r')


{
return cur.time + 1;
}


if (next.i >= 0 && next.i<n&&next.j >= 0 && next.j<m)


if (map[next.i][next.j] != '#')


{


if (map[next.i][next.j] == 'x')


{


map[next.i][next.j] = '#';


next.time = cur.time + 2;


q.push(next);


}


else


{


map[next.i][next.j] = '#';


next.time = cur.time + 1;


q.push(next);


}


}






}






}


return -1;


}






int main()


{


int i, j;


while (scanf("%d %d", &n, &m) != EOF)


{


getchar();


bool flag = true;


for (i = 0; i<n; i++)


{


for (j = 0; j<m; j++)


{


scanf("%c", &map[i][j]);


if (map[i][j] == 'a')


{


start.i = i;


start.j = j;


}


}


getchar();






}


int T = bfs(start);


if (T >= 0)


printf("%d\n", T);


else


printf("Poor ANGEL has to stay in the prison all his life.\n");






}


return 0;


}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章