概述
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 30765 Accepted Submission(s): 10808
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.)
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.
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 | We have carefully selected several similar problems for you:
1240
1016
1072
1241
1253
题意;求r到a的最短距离。
思路写在注释上了
/* 最近在做dfs剪枝,这个题也可以用bfs做。这里一个有力的剪枝就是 map1【i】【j】,表示到大x行y列的最少步数,这次到达x,y比之前的大,
就没必要深搜下去了,否则,则继续深搜*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int n,m;
char map[210][210];
int map1[210][210];
int x,y,zx,zy,ans;
int vis[210][210];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int jude(int x,int y)
{
if(x<=n&&x>=1&&y>=1&&y<=m) return 1;
return 0;
}
void dfs(int x,int y,int count)
{ // printf("%d %d %dn",x,y,count);
if(x==zx&&y==zy)
{
if(ans>count) ans=count;
return ;
}
for(int i=0;i<4;i++)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(!jude(xx,yy)) continue;
if(!vis[xx][yy]&&map[xx][yy]!='#')
{ vis[xx][yy]=1;
if(map[xx][yy]=='x')
{ if(count+2<map1[xx][yy])//当这次到达xx,yy比之前的小
{ map1[xx][yy]=count+2;
dfs(xx,yy,count+2);
}
}
else
{if(count+1<map1[xx][yy])
{
map1[xx][yy]=count+1;
dfs(xx,yy,count+1);
}
}
vis[xx][yy]=0;
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{ getchar();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='a') {zx=i,zy=j;}
if(map[i][j]=='r'){x=i,y=j;}
map1[i][j]=999999999;//因为是求小的所以这里保存一个大的值。
}
getchar();
}
/*for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
printf("%c",map[i][j]);
printf("n");
}*/
// printf("%d %dn",zx,zy);
ans=999999999;
memset(vis,0,sizeof(vis));
vis[x][y]=1;
dfs(x,y,0);
if(ans==999999999) printf("Poor ANGEL has to stay in the prison all his life.n");
else printf("%dn",ans);
}
}
最后
以上就是飞快铃铛为你收集整理的hdu1242 dfs剪枝Rescue的全部内容,希望文章能够帮你解决hdu1242 dfs剪枝Rescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复