搜索算法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;


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值