概述
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
【题意】你现在在监狱里面,你想要营救你的伙伴。但是你的伙伴被关押了,所以只能是你去见他,监狱里面自然是有狱警的,因为你是劫狱的,所以你要干掉狱警,现在你很厉害,没有一个狱警比你强,但是干掉狱警还是要花时间的,现在的你只能上下左右的移动,狱警和你的伙伴不能移动,你每次移动都需要一分钟,但是干掉狱警也需要一分钟,问你救出朋友需要的最少时间,如果就不出来就输出Poor ANGEL has to stay in the prison all his life.
【分析】简单的搜索题,就是给某些地方一个不一样的权值而已,加一个复合判断就行。
【代码】
#include <bits/stdc++.h>
using namespace std;
struct P
{
int x,y,val;
};
char mat[205][205];
int val[205][205];
int changex[]= {0,0,1,-1};
int changey[]= {1,-1,0,0};
int c,d,start_x,start_y,end_x,end_y;
bool in_map(int x,int y)//防止越界
{
if(x<0||x>=c||y<0||y>=d)
return false;
return true;
}
int main()
{
while(~scanf("%d%d",&c,&d))//注意是多组输入
{
memset(val,0x3f3f3f3f,sizeof(val));
for(int i=0; i<c; i++)
for(int j=0; j<d; j++)
{
scanf(" %c",&mat[i][j]);
if(mat[i][j]=='r')
{
start_x=i;
start_y=j;
}
else if(mat[i][j]=='a')
{
end_x=i;
end_y=j;
}
}
P a,b;
a.x=start_x;
a.y=start_y;
val[a.x][a.y]=0;
a.val=0;
queue<P> ans;
ans.push(a);
while(!ans.empty())
{
a=ans.front();
ans.pop();
for(int i=0; i<4; i++)
{
b.x=a.x+changex[i];
b.y=a.y+changey[i];
if(!in_map(b.x,b.y))
continue;
if(mat[b.x][b.y]=='#')
continue;
if((mat[b.x][b.y]=='a'||mat[b.x][b.y]=='.')&&val[b.x][b.y]>a.val+1)
{
val[b.x][b.y]=a.val+1;
b.val=a.val+1;
ans.push(b);
}
else if(mat[b.x][b.y]=='x'&&val[b.x][b.y]>a.val+2)
{
val[b.x][b.y]=a.val+2;
b.val=a.val+2;
ans.push(b);
}
}
}
if(val[end_x][end_y]<80005)//这里需要判断是否救出来了
printf("%dn",val[end_x][end_y]);
else
printf("Poor ANGEL has to stay in the prison all his life.n");
}
return 0;
}
最后
以上就是欣慰导师为你收集整理的hdu 1242的全部内容,希望文章能够帮你解决hdu 1242所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复