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

HDOJ-1242 Rescue

2016-07-29 15:00 337 查看

Rescue

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

Total Submission(s): 26321    Accepted Submission(s): 9304

[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

介个题在什么时候救不出来真的难,判断了n个测试样例,经常出错,好像要用优先队列.........然后我就照葫芦画瓢,果然可以求出最短时间,但是什么时候救不出来,还是不好判断,最终  还是做完了,wa了好几十回!!!!

代码在下边(经过千修万改):
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <
4000
queue>
#define M 210
using namespace std;
struct node{
int x,y,step;
friend bool operator<(node a,node b)
{
return a.step>b.step;          //用时少的先出队列
}
};
node s,e;
int n,m,ans;
char map[M][M];
int vis[M][M];
int fx[4]={1,-1,0,0};
int fy[4]={0,0,1,-1};
int BFS(node s)
{
vis[s.x][s.y]=0;
priority_queue<node> q;
q.push(s);
while(!q.empty())
{
node now=q.top();
q.pop();
for(int i=0;i<4;i++)
{
node next;
next.x=now.x+fx[i];
next.y=now.y+fy[i];
next.step=now.step;
if(vis[next.x][next.y]==1&&next.x>=0&&next.x<n&&next.y>=0&&next.y<m)
{
if(map[next.x][next.y]=='x')
next.step+=1;
vis[next.x][next.y]=0;
next.step+=1;
q.push(next);
if(next.x==e.x&&next.y==e.y) return next.step;
}
}
}
return 0;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
getchar();
for(int j=0;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='.'||map[i][j]=='x') vis[i][j]=1;
if(map[i][j]=='r'){s.x=i;s.y=j;vis[i][j]=0;}
if(map[i][j]=='a'){e.x=i;e.y=j;vis[i][j]=1;}
}
}
ans=0;
s.step=0;
ans=BFS(s);
if(ans) printf("%d\n",ans);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: