Hdoj 1242 Rescue (BFS)

Source: Internet
Author: User

Rescue http://acm.hdu.edu.cn/showproblem.php?pid=1242
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 18962 Accepted Submission (s): 6771


problem DescriptionAngel 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.)
 
InputFirst line contains-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 the Angel ' s friend.

Process to the end of the file.
 
Outputfor 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
 
AuthorCHEN, Xue 
SourceZOJ Monthly, October 2003 
recommendEddy | We have carefully selected several similar problems for you:1240 1016 1010 1072 1253  /**/The first time to write BFS

Main topic:

1. In the maze of obstacles, ask for the shortest time or step from one point to the next,

2. In this topic, there are more than one angel friend, so the starting point of the search should be from the angel began to search friends,

3. The question asks is the shortest time, therefore should only use the priority queue, the small step number first.

--------------------------------------------------------------------------------------------------------------- -------------------------

/**/BFS:

1. The beginning of a V0, first visit V0;

2. Access all vertices adjacent to V0 v1, v2 、、、、、、 Vt;

3. Access all the vertices that have not been visited adjacent to V1, v2 、、、、、、 Vt;

4. In the past, until all the vertices have been visited;


BFS Specific operation:

Define a queue; queue<node> Q;

The starting point joins the queue; Q.push (P);

While (queue is not empty) while (! Q.empty ())

{                                        {

Take out the head knot of the team; p=q.front; q.pop;

If the desired target state if (satisfies the condition)

Jump out of the loop; return;

otherwise else

From it, the child nodes are expanded.

Add all to the end of the team Q.push ();
}                                         }

If the target is found in the loop, the output is not solved;

--------------------------------------------------------------------------------------------------------------- -------------------------

#include <cstdio>

#include <cmath>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
using namespace Std;
const int m=205;
int n,m;
int start_x,end_x;
int start_y,end_y;



Char Map[m][m];
int cost[m][m];
int fx[4]={1,0,-1,0};
int fy[4]={0,1,0,-1};


typedef struct
{
int x, y;
int time;
}point;


void init ()//
{
int i,j;
for (i=0;i<n;i++)
for (j=0;j<m;j++)
{
if (map[i][j]== ' r ')//Access port
{
Start_x=i;
Start_y=j;
}
if (map[i][j]== ' a ')//output
{
End_x=i;
End_y=j;
}
Cost[i][j]=-1;
}
}


int isvalid (int x,int y)
{
if (x>=0&&x<n&&y>=0&&y<m)
return 1;
Else
return 0;
}




void BFS ()
{
Point p1,p2;
queue<point> q;//defining a queue
int i;
    
p1.x=start_x;//Initialization
p1.y=start_y;
p1.time=0;
cost[p1.x][p1.y]=0;
    
While (! Q.empty ())//Initialize
Q.pop ();
 
Q.push (p1);//start point join queue
While (! Q.empty ())//When the queue is not empty
    {
    P1=q.front ();//Remove Team head node information
    Q.pop ();
   
    For (i=0;i<4;i++)//Meet condition
    {
    P2.x=p1.x+fx[i];
    P2.y=p1.y+fy[i];
p2.time=p1.time+1;

if (map[p2.x][p2.y]== ' # ' | |! IsValid (P2.X,P2.Y))//Meet the conditions
Continue ;
if (map[p2.x][p2.y]== ' x ')
p2.time++;
if (cost[p2.x][p2.y]==-1| | P2.TIME<COST[P2.X][P2.Y])
{
Q.push (p2);//Queue up
Cost[p2.x][p2.y]=p2.time;

}
}
}





int main ()
{
while (~SCANF ("%d%d", &n,&m))
{
int i,j;
for (i=0;i<n;i++)
scanf ("%s", Map[i]);

Init ();
BFS ();
if (cost[end_x][end_y]==-1)
printf ("Poor ANGEL have to stay in the prison all his life.\n");
Else
printf ("%d\n", cost[end_x][end_y]);

}
return 0;
}

Hdoj 1242 Rescue (BFS)

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.