HDU 1242 rescue【广搜】

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
方法一:求出各个点到‘a’的最近距离,然后从‘r’里面选出能达到的最短距离,考虑‘x’的情况时要保证其他点的最短路不能被破环
code:
View Code
#include<stdio.h>
#include<string.h>
char map[201][201];
char str[201];
int step[201][201];
int f[8]={-1,0,0,1,0,-1,1,0};
struct node
{
int x;
int y;
}q[40500];
int main()
{
int n,m,i,j,x0,y0,x1,y1,k,p,r1,r2;
int front,rear;
//int r[401]; //可能不止一个朋友
while(scanf("%d%d",&n,&m)!=EOF)
{
front=rear=p=0;
memset(step,0,sizeof(step));
for(i=0;i<n;i++)
{
scanf("%s",str);
for(j=0;j<m;j++)
{
map[i][j]=str[j];
if(str[j]=='a')
{q[rear].x=i;
q[rear++].y=j;}
if(str[j]=='r')
{r1=i;r2=j;}
}
}
while(front<rear)
{
x0=q[front].x;
y0=q[front].y;
for(i=0;i<4;i++)
{
x1=x0+f[i];
y1=y0+f[i+4];
if(x1>=0&&x1<n&&y1>=0&&y1<m&&map[x1][y1]!='#')
{
if(map[x1][y1]=='x')
{
if(step[x1][y1]==0||step[x0][y0]+2<step[x1][y1])
{
step[x1][y1]=step[x0][y0]+2;
q[rear].x=x1;
q[rear++].y=y1;
}
}
else
{
if(step[x1][y1]==0||step[x0][y0]+1<step[x1][y1])
{
step[x1][y1]=step[x0][y0]+1;
q[rear].x=x1;
q[rear++].y=y1;
}
}
}
}
front++;
}
if(step[r1][r2])
printf("%d\n",step[r1][r2]);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return0;
}
方法二:对于x,第一次存入数组时不必标记,第一次对它搜索时存入下次新数组,并标记。
code:
View Code
#include<stdio.h>
int a[80010];
char b[210],s[40010];
int main()
{
int i,j,m,n,k,p,q,w,e;
while(scanf("%d%d",&m,&n)!=EOF){
for(i=0;i<m;i++){
scanf("%s",b);
for(j=0;j<n;j++){
s[i*n+j]=b[j];
if(s[i*n+j]=='a'){
a[0]=i*n+j;
s[a[0]]='#';
}
}
}
i=p=0;e=w=q=k=1;
while(k&&e){
i++;
for(j=p;j<q;j++){
if(s[a[j]]=='x'){
s[a[j]]='#';
a[w++]=a[j];
}
else{
if(a[j]>=n&&s[a[j]-n]!='#'){
if(s[a[j]-n]=='.')s[a[j]-n]='#';
else if(s[a[j]-n]=='r'){
k=0;break;
}
a[w++]=a[j]-n;
}
if(a[j]<(m-1)*n&&s[a[j]+n]!='#'){
if(s[a[j]+n]=='.')s[a[j]+n]='#';
else if(s[a[j]+n]=='r'){
k=0;break;
}
a[w++]=a[j]+n;
}
if(a[j]%n>0&&s[a[j]-1]!='#'){
if(s[a[j]-1]=='.')s[a[j]-1]='#';
else if(s[a[j]-1]=='r'){
k=0;break;
}
a[w++]=a[j]-1;
}
if(a[j]%n<n-1&&s[a[j]+1]!='#'){
if(s[a[j]+1]=='.')s[a[j]+1]='#';
else if(s[a[j]+1]=='r'){
k=0;break;
}
a[w++]=a[j]+1;
}
}
}
if(k==0)break;
if(q==w)e=0;
p=q;q=w;
}
if(e)printf("%d\n",i);
else puts("Poor ANGEL has to stay in the prison all his life.");
}
return 0;
}

posted @ 2012-03-15 12:51  'wind  阅读(167)  评论(0编辑  收藏  举报