Now let's look at the priority queue again. It's not as confused as it was just getting started! This may be the result of the training!
Come on !!! Priority queue is required!
This question is very simple! Customize priority!
++ ++ ++
========================================================== ========================================================== ===
++ ++ ++
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace STD;
Int n, m, dir [4] [2] = {,-, 0,-1 };
Char s [1, 1010] [2, 1010];
Struct Node
{
Int X, Y, step;
Friend bool operator <(node A, Node B)
{
Return A. Step> B. step;
}
/*
Bool operator <(const node & B) const
{
Return Step> B. step;
}*/
};
Bool judge (int x, int y)
{
If (x> = 0 & x <n & Y> = 0 & Y <M & S [x] [Y]! = '#')
Return true;
Return false;
}
Int BFS (INT x0, int y0)
{
Int K, X, Y;
Priority_queue <node> q;
Node cur, next;
Cur. x = x0; cur. Y = y0; cur. Step = 0;
Q. Push (cur );
S [x0] [y0] = '#';
While (! Q. Empty ())
{
Cur = Q. Top ();
Q. Pop ();
For (k = 0; k <4; k ++)
{
X = next. x = cur. x + dir [k] [0];
Y = next. Y = cur. Y + dir [k] [1];
Next. Step = cur. Step + 1;
If (Judge (x, y ))
{
If (s [x] [Y] = 'R ')
Return next. step;
Else if (s [x] [Y] = '.')
Q. Push (next );
Else
{
Next. Step ++;
Q. Push (next );
}
S [x] [Y] = '#';
}
}
}
Return-1;
}
Int main ()
{
Int I, j, X, Y, ans;
While (scanf ("% d", & N, & M )! = EOF)
{
For (I = 0; I <n; I ++)
Scanf ("% s", s [I]);
For (I = 0; I <n; I ++)
{
For (j = 0; j <m; j ++)
If (s [I] [J] = 'A ')
{
X = I; y = J;
}
If (j <m)
Break;
}
Ans = BFS (x, y );
If (ANS =-1)
Printf ("Poor Angel has to stay in the prison all his life. \ n ");
Else
Printf ("% d \ n", ANS );
}
Return 0;
}
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1242