您的位置:首页 > 产品设计 > UI/UE

HDOJ-1242-Rescue

2015-08-13 15:37 253 查看

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 21644    Accepted Submission(s): 7721


[align=left]Problem Description[/align]
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.)

 

[align=left]Input[/align]
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.

 

[align=left]Output[/align]
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."

 

[align=left]Sample Input[/align]

7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

 

[align=left]Sample Output[/align]

13

 
 
 
第一个完整理解写下来的bfs,曾经怎么写都不对的题,终于解决了.
题意:a 被抓走了, r 来救她(他), # 是墙, . 是路,走一步一分钟, x 是守卫,遇见一个多花一分钟,求最短需要多长时间
思路:基本是个模板题,不过按题意细节来看,friends来救她,有可能有多个 r ,遇见最近的一个就得救,因此逆向思维, a 只有一个,所以让 a 去找最近的 r 即可(最后发现r去找a也会AC,想多了,不过严谨一些比较好).

#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 220
char map[MAX][MAX];
int d[4][2]{0,1,0,-1,1,0,-1,0};
int vis[MAX][MAX],N,M,sx,sy,ans;
struct node{
int x,y,step;
};
bool operator <(const node &a,const node &b){
if(a.step==b.step)
return a.x<b.x;
return b.step<a.step;
}
node now;
node then;
bool juge(int a,int b){
if(a>N||a<1||b>M||b<1||vis[a][b]||map[a][b]=='#')
return false;
return true;
}
void bfs(){
now.x=sx;
now.y=sy;
now.step=0;
memset(vis,0,sizeof(vis));
priority_queue<node>q;
q.push(now);
vis[sx][sy]=1;
while(!q.empty()){
now=q.top();
q.pop();
for(int v=0;v<4;v++){
then.x=now.x+d[v][0];
then.y=now.y+d[v][1];
if(juge(then.x,then.y)){
if(map[then.x][then.y]=='x')
then.step=now.step+2;
else
then.step=now.step+1;
if(map[then.x][then.y]=='r'){
ans=then.step;
return;
}
vis[then.x][then.y]=1;
q.push(then);
}
}
}
}
int main(){
while(scanf("%d%d",&N,&M)!=EOF){
for(int n=1;n<=N;n++){
getchar();
for(int m=1;m<=M;m++){
scanf("%c",&map
[m]);
if(map
[m]=='a'){
sx=n;sy=m;//记录下a的坐标为起点
}
}
}
ans=INF;
bfs();
if(ans!=INF)//ans值不变表示没有可解救的通路
printf("%d\n",ans);
else
puts("Poor ANGEL has to stay in the prison all his life.");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: