概述
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649
题目:
Rescue
Time Limit: 2 Seconds Memory Limit: 65536 KB
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.)
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.
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
题目描述:
bfs搜索题,可以用优先队列,也可以不用,普通的bfs模板搜索过不了,主要是在杀死守卫时会多耗时,所以步数短的不一定用时短,解决方法是,可以用一个二维数组来存当前用时最短的时间,每次更新即可,这里‘r’不止一个,所以要用‘a’来找‘r’,最后输出最短的‘r’用时。这种方法需重复搜索,所以数组需开的较大。
代码:
#include<stdio.h>
#include<string.h>
int min[210][210];
struct qa
{
int a,b,c;
}qu[1000010];
int main()
{
char s[210][210];
int a,b,i,j,k,T;
int ei,ej;
while(scanf("%d%d",&a,&b)!=EOF){
for(i = 0;i <a ;i++)
scanf("%s",s[i]);
for(i = 0;i < a;i ++){
for(j = 0;j < b;j ++){
if(s[i][j] == 'a')
ei = i,ej = j;
min[i][j] = 99999999;
}
}
//**************************************************************************************
int h = 0,t = 0;
qu[h].a = ei;
qu[h].b = ej;
qu[h].c = 0;
min[ei][ej] =0;
t++;
T = 99999999;
while(h < t){
int ne[4][2] = {0,1,
1,0,
0,-1,
-1,0};
for(i = 0;i < 4;i++){
int tx = qu[h].a + ne[i][0];
int ty = qu[h].b + ne[i][1];
if(tx < 0 || tx >= a || ty < 0 || ty >= b)
continue;
if(s[tx][ty]!='#'){
qu[t].a = tx;
qu[t].b = ty;
qu[t].c = qu[h].c + 1;
if(s[tx][ty] == 'x')
qu[t].c ++;
if(min[tx][ty] > qu[t].c){
min[tx][ty] = qu[t].c;
t ++;
//只有更新了才会加入队列
}
if(s[tx][ty] == 'r' && T > min[tx][ty])
T = min[tx][ty]; //更新用时
}
}
h++;
}
//***************************************************************
if(T<99999999)
printf("%dn",T);
else
printf("Poor ANGEL has to stay in the prison all his life.n");
}
return 0;
}
最后
以上就是漂亮洋葱为你收集整理的ZOJ —— 1649 Rescue(搜索)的全部内容,希望文章能够帮你解决ZOJ —— 1649 Rescue(搜索)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复