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

HDU 1242 Rescue(营救)(优先队列+BFS)

2014-07-29 20:56 295 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242

Rescue

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

Total Submission(s): 15880    Accepted Submission(s): 5756


[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

刚开始一直在超时,原因是输入哪里没有加一个~这个符号,弄了好久,=.=,好无语

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
int m,n;
char mp[220][220];
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
typedef struct node
{
int x,y, step;

};
bool operator < (const node &p,const node &q)
{
return p.step > q.step;
}
int judge(int x,int y)
{
if(x<0 || y<0 ||x>=m ||y>=n || mp[x][y]=='#')
return 0;
return 1;
}

int bfs(int row,int col)
{
priority_queue <node> qu;

node s,out;

s.x = row;
s.y = col;
s.step = 0;

qu.push(s);

mp[row][col]='#';

while(!qu.empty())
{
out = qu.top();
qu.pop();

// printf("x= %d,y= %d ,step = %d  %c\n",out.x,out.y,out.step,mp[out.x][out.y]);

for(int i=0; i<4; i++)
{

int x = out.x+dir[i][0];
int y = out.y+dir[i][1];

if(judge(x,y))
{
if(mp[x][y] == 'a')
{
printf("%d\n",out.step+1);
return 1;
}

s.x = x;
s.y = y;

s.step = out.step+1;

if(mp[x][y]=='x')
s.step++;

qu.push(s);

mp[x][y]='#';
}
}
}
return 0;
}
/*
7 8
#.#####.
#..#..r.
#.a#x...
..#..#.#
#...##..
.#......
........

*/
int main()
{
while(~scanf("%d %d",&m,&n))
{
int c,d;
int i,j;
for(i=0; i<m; i++)
{
scanf("%s",mp[i]);
for(j=0; j<n; j++)
{
if(mp[i][j]=='r')
{
c=i;
d=j;
}
}
getchar();
}

// printf("mp[1][2]=%c\n",mp[1][2]);
int flag = bfs(c,d);
if(flag==0)
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}

//http://acm.hdu.edu.cn/showproblem.php?pid=1242
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: