It's almost the same as the previous 2757, hey !!! Once !!!
It's worth it !!!
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace STD;
# Define INF 999999999
Struct Node
{
Int X, Y, time;
Friend bool operator <(node A, Node B)
{
Return A. Time> B. time;
}
};
Char map [1010] [1010];
Int visit [1010] [1010];
Int dir [4] [2] = {,-, 0,-1 };
Int n, m;
Int judge (int x, int y)
{
If (x> = 1 & x <= N & Y> = 1 & Y <= m)
Return 1;
Return 0;
}
Int BFS (INT Sx, int Sy, int dx, int Dy)
{
Priority_queue <node> q;
Node In, out;
Int I;
Visit [SX] [sy] = 0;
In. x = SX;
In. Y = sy;
In. Time = 0;
Q. Push (in );
While (! Q. Empty ())
{
In = Q. Top ();
Q. Pop ();
If (in. x = DX & in. Y = Dy)
Return in. time;
For (I = 0; I <4; I ++)
{
Out. x = in. x + dir [I] [0];
Out. Y = in. Y + dir [I] [1];
If (Judge (Out. X, out. y ))
{
If (Map [out. x] [out. Y] = 'X ')
Out. Time = in. time;
Else
Out. Time = in. Time + 1;
If (visit [out. x] [out. Y]> out. Time)
{
Visit [out. x] [out. Y] = out. Time;
Q. Push (out );
}
}
}
}
Return 1;
}
Int main ()
{
Int x0, y0, X1, Y1, I, j, ans;
While (scanf ("% d", & N, & M), N + M)
{
For (I = 1; I <= N; I ++)
Scanf ("% s", map [I] + 1 );
For (I = 1; I <= N; I ++)
For (j = 1; j <= m; j ++)
Visit [I] [J] = inf;
Scanf ("% d", & x0, & y0, & X1, & Y1 );
Ans = BFS (x0, y0, X1, Y1 );
Printf ("% d \ n", ANS );
}
Return 0;
}
Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2822