我是靠谱客的博主 纯情毛巾,最近开发中收集的这篇文章主要介绍HDOJ - 1242 Rescue,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Rescue
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
一脸懵逼。。。就这么看着“学姐”的代码敲完了。。代码还是很好懂的,主要就是细节 抓狂
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m,ans;
int a,b,c,d;
char map[205][205];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
struct node{
int x,y,time;
};
bool operator<(const node &x,const node &y){
return x.time>y.time;
}
int bfs(){
int i;
node now,next;
now.x=a;now.y=b;now.time=0;
priority_queue<node> q;
q.push(now);
while(!q.empty()){
now=q.top();
q.pop();
if(now.x==c&&now.y==d)
return now.time;
for(i=0;i<4;i++){
next.x=now.x+dx[i];
next.y=now.y+dy[i];
if(next.x>=0&&next.y>=0&&next.x<n&&next.y<m&&map[next.x][next.y]!='#'){
if(map[next.x][next.y]=='.'||map[next.x][next.y]=='a')
next.time=now.time+1;
else
next.time=now.time+2;
map[next.x][next.y]='#';
q.push(next);
}
}
}
return -1;
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++){
getchar();
for(int j=0;j<m;j++){
scanf("%c",&map[i][j]);
if(map[i][j]=='r')
a=i,b=j;
if(map[i][j]=='a')
c=i,d=j;
}
}
ans=bfs();
if(ans==-1)
printf("Poor ANGEL has to stay in the prison all his life.n");
else
printf("%dn",ans);
}
return 0;
}


最后

以上就是纯情毛巾为你收集整理的HDOJ - 1242 Rescue的全部内容,希望文章能够帮你解决HDOJ - 1242 Rescue所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部