TOJ 1335 HDU 1242 ZOJ 1649 rescue Angel/Wide search + priority queue

Source: Internet
Author: User
Tags min time limit
rescue Angel time limit (normal/java): 1000ms/10000ms Run memory limit: 65536KByte


Description



Angel was caught by the moligpy! He was put into prison by Moligpy. The prison is described as a n * M (n, M <=) matrix. There is WALLs, ROADs, and guards in the prison.

Angel ' s friends want to save Angel. Their task Is:approach Angel. We assume that "approach Angel" are to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or his?) 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 had 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-integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, "X" stands for a guard, and "R" stands for each of the 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 have to stay in the prison all he life."



Sample Input






7 8
#.#####.
#.a#. R.
#.. #x .....
#.. #.#
#...##..
. #......
........











Sample Output





13




#include <stdio.h>
#include <string>
#include <iostream>
#include <queue>
#define MAX 210
#define inf 0x7fffffff
using namespace std;

char a [MAX] [MAX];
int map [MAX] [MAX];
int dir [4] [2] = {{-1,0}, {1,0}, {0, -1}, {0,1}}; // Represents up down left right
int n, m;
int Min;
struct point
{
int x;
int y;
int step; // steps
friend bool operator <(point a, point b)
{
return a.step> b.step; // Priority queue
}
} s, e;


void bfs ()
{
priority_queue <point> q;
memset (map, 0, sizeof (map));
q.push (s);
while (! q.empty ())
{
point p = q.top ();
q.pop ();
if (p.x == e.x && p.y == e.y)
{
if (p.step <Min)
Min = p.step;
return;
}
for (int i = 0; i <4; i ++)
{
point t;
t.x = p.x + dir [i] [0];
t.y = p.y + dir [i] [1];
if (t.x> = 0 && t.x <n && t.y> = 0 && t.y <m && a [t.x] [t.y]! = '#' && map [t.x] [t.y] == 0)
{
t.step = p.step + 1;
map [t.x] [t.y] = 1; // Mark past and stop
if (a [t.x] [t.y] == 'x')
{
t.step ++;
a [t.x] [t.y] = '.'; // whatever that person killed and died
}
q.push (t);
}
}
}
}
main ()
{
int i, j;
while (scanf ("% d% d% d", & n, & m)! = EOF)
{
Min = inf;
for (i = 0; i <n; i ++)
{
scanf ("% s", a [i]);
for (j = 0; j <m; j ++)
{
if (a [i] [j] == 'a')
{
e.x = i;
e.y = j;
}
}
}
for (i = 0; i <n; i ++)
{
for (j = 0; j <m; j ++)
{
if (a [i] [j] == 'r')
{
s.x = i;
s.y = j;
bfs ();
}
}
}
if (Min! = inf)
printf ("% d \ n", Min);
else
puts ("Poor ANGEL has to stay in the prison all his life.");
}
} 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.