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

HDU 1242 Rescue(BFS优先队列)

2017-01-14 16:20 302 查看

Rescue

[align=center]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 28234    Accepted Submission(s): 10002

[/align]

[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...
..#..#.#
#...##..
.#......
........

 

Sample Output

13


题解:大意为天使被魔头抓走了,关在了监狱内,监狱为二维字符数组,其中,r为营救者,a为天使,x为警卫,#为墙,营救者每走一格花费一分钟,且它足够强大可以杀死所有的守卫,但必须多花费一分钟……
用优先队列来做……

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<queue>;
#include<stack>;
#include <iostream>
#include <queue>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
char map[201][201];                          /*储存监狱……*/
bool v[201][201];                            /*标记……*/
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};     /*营救者的四种走法……*/
int n,m,a,b,aa,bb;
struct node                                  /*定义优先队列*/
{
int x,y,s,time;
bool operator<(const node &z)const
{
return time>z.time;
}
};
int check(int x,int y)                           /*判断此时的坐标是否可走*/
{
if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='#'&&!v[x][y])
return 1;
return 0;
}
void bfs()
{
priority_queue<node>q;                     /*优先队列……*/
node start,next;
start.x=a,start.y=b,start.time=0;
q.push(start);
while(!q.empty())
{
start=q.top();
q.pop();
if(start.x==aa&&start.y==bb){printf("%d\n",start.time);return;}     /*找到,结束*/
for(int i=0;i<4;i++)
{
next.x=start.x+d[i][0],next.y=start.y+d[i][1];
if(check(next.x,next.y))
{
if(map[next.x][next.y]=='x')
next.time=start.time+2;
else
next.time=start.time+1;
v[next.x][next.y]=true;
q.push(next);
}
}
}
printf("Poor ANGEL has to stay in the prison all his life.\n");return;   /*未找到……*/
}
int main()
{
while(~scanf("%d%d",&n,&m)&&(n||m))
{
getchar();
mem(v);
for(int i=0;i<n;i++)
{
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')
aa=i,bb=j;
}
getchar();
}
v[a][b]=true;
bfs();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bfs