您的位置:首页 > 产品设计 > UI/UE

HDU  1242  Rescue

2012-12-05 17:10 351 查看
Rescue

Time Limit: 2000/1000 MS
(Java/Others)    Memory Limit:
65536/32768 K (Java/Others)
Total Submission(s): 5802
   Accepted Submission(s):
2153

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.)
 

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
 

Author
CHEN, Xue
 

Source
ZOJ Monthly, October 2003
 

Recommend
Eddy

这题刚开始一看,很容易把他当成简单的BFS,可是一提交就错,让我也着实郁闷了好久,郁闷的童鞋们可以试一下我的测试数据,这题,做的时候稍微改下代码,不把已经走过的路径覆盖,换成用一个二维数组几下走到当前的最短路径。当有一个走到的时候,与当前的数据进行对比,如果大于的话,就把数组中的数据覆盖,否则,跳过,这样,一旦走到含有r的点时,用一个变量几下最小值,最后的值就是所求的值了,
注意以下几点:
以7 8
#....##.
#.a#....
##.#x.r.
.....###
#....#..
.#......
........为例,
1、要从a开始搜索,因为r可能含有多个。
2、找到r的时候不要急着退出结束,这个r有可能不是最短的路径(因为有x)
3、不要把走过的路径覆盖,用二位数组几下,走到某个位置的最短路径,不断覆盖,直到没有一个值可以比他小为止。
代码和测试数据:

#include<stdio.h>
typedef struct point{
int x,y,step;
}target;
int m,n,dir[4][2]={0,1,0,-1,1,0,-1,0},ax,ay;
int flag[202][202];
char map[302][302];
target que[40005];
int BFS(target start)
{
int end,top,i;
int min=1000000;
target in,next;
end=top=0;
que[top]=start;
while(top>=end)
{
in=que[end];
end=(end+1)@000;
for(i=0;i<4;i++)
{
next.x=in.x+dir[i][0];
next.y=in.y+dir[i][1];
if(map[next.x][next.y]=='r')
{
if(min>in.step+1)
min=in.step+1;
}

if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n&&map[next.x][next.y]!='#')
{
if(flag[next.x][next.y]>in.step+1)
{
next.step=in.step+1;
if(map[next.x][next.y]=='x')
next.step++;
flag[next.x][next.y]=next.step;
top=(top+1)@000;
que[top]=next;
}
}
}
}
if(min!=1000000)return min;
else
return -1;
}
int main()
{
int i,j,num;
target start;

while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
{
scanf("%s",map[i]);
for(j=0;j<n;j++)
{
flag[i][j]=1000000;
if(map[i][j]=='a')
{
map[i][j]='.';
ax=i;
ay=j;
}
}
}
start.x=ax;start.y=ay;start.step=0;
map[ax][ay]='.';
num=BFS(start);
if(num==-1)
printf("Poor ANGEL has to stay in the prison all his
life.\n");
else
printf("%d\n",num);
}
return 0;
}

测试数据:

7 8
#....##.
#.a#....
##.#x.r.
.....###
#....#..
.#......
........
7
5 5
a....
###..
rx...
.....
r...r

8

5 5
a....
.....
xr...
r....
....r

3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: