概述
此题要注意的是多个。。r..还加优先队列。。
才能保证每次到r才是最小时间的。
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5900 Accepted Submission(s): 2193
Problem 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.)
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.
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
Author
CHEN, Xue
Source
ZOJ Monthly, October 2003
Recommend
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <iostream>
using namespace std;
char map[210][210];
struct node
{
int x, y, t;
bool operator < (const node &A) const
{
return A.t < t;
}
}NODE;
int xx[]={0, 1, -1, 0};
int yy[]={1, 0, 0, -1};
int N, M, a, b, c, d, f1;
int judge( int x, int y)
{
if (x < 1 || x > N || y < 1 || y > M)
return 0;
return 1;
}
void BFS( )
{
int i, j, x1, x2, y1, y2,t;
priority_queue<node>q;
f1 = 0;
NODE.x = c, NODE. y = d, NODE. t = 0;
q.push(NODE);
while (!q.empty( ) && !f1)
{
NODE = q.top( );
q.pop( );
x1 = NODE.x, y1 = NODE.y, t = NODE.t;
if (map[x1][y1] == 'r')
{
f1 = 1;
printf("%dn",NODE.t);
break;
}
for(i = 0; i < 4; i++)
{
x2 = x1 + xx[i];
y2 = y1 + yy[i];
NODE.x = x2, NODE.y = y2, NODE.t = t + 1;
if (judge(x2,y2) && map[x2][y2] != '#')
{
if (map[x2][y2] == 'x')
{
NODE.t++;
map[x2][y2] = '#';
q.push(NODE);
}
else
{
if (map[x2][y2] == '.')
{
map[x2][y2] = '#';
q.push(NODE);
}
else
q.push(NODE);
}
}
}
}
}
int main( )
{
int i, j;
while (scanf("%d%d",&N, &M) != EOF)
{
for(i = 1; i <= N; i++)
for(j = 1; j <= M; j++) {
cin>>map[i][j];
if (map[i][j] == 'r')
{
a = i;
b = j;
}
if (map[i][j] == 'a')
{
c = i;
d = j;
}
}
BFS( );
if (!f1)
puts("Poor ANGEL has to stay in the prison all his life.");
}
return 0;
}
转载于:https://www.cnblogs.com/tangcong/archive/2011/08/09/2132124.html
最后
以上就是高挑水壶为你收集整理的Rescue hdu 1242 BFSRescue的全部内容,希望文章能够帮你解决Rescue hdu 1242 BFSRescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复