概述
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27582 Accepted Submission(s): 9776
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则需要多花费一个单位时间,说白了就是dfs到这个点时步数加一,,,
然后就是dfs模板了。。。
以下AC代码:
#include<stdio.h>
int m,n;
int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
char s[205][205];
int flag[205][205];
int min;
void dfs(int x,int y,int count)
{
if(s[x][y]=='r')
{
if(min>count)
min=count;
return;
}
if(count>=min)
return;
if(s[x][y]=='x')
{
count++;
}
int i;
for(i=0;i<4;i++)
{
int sx=x+dir[i][0];
int sy=y+dir[i][1];
if(sx>=0 && sx<m && sy>=0 && sy<n && s[sx][sy]!='#' && flag[sx][sy]==0)
{
flag[sx][sy]=1;
dfs(sx,sy,count+1);
flag[sx][sy]=0;
}
}
}
int main()
{
int i,j;
int sx,sy;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<205;i++)
for(j=0;j<205;j++)
flag[i][j]=0;
min=10000000;
for(i=0;i<m;i++)
{
scanf("%s",s[i]);
for(j=0;j<n;j++)
{
if(s[i][j]=='a')
{
sx=i;
sy=j;
}
}
}
dfs(sx,sy,0);
if(min!=10000000)
printf("%dn",min);
else
printf("Poor ANGEL has to stay in the prison all his life.n");
}
return 0;
}
最后
以上就是缥缈爆米花为你收集整理的HDU 1242 Rescue(DFS)的全部内容,希望文章能够帮你解决HDU 1242 Rescue(DFS)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复