HDU 2822 dogs (BFS + priority queue)

Source: Internet
Author: User

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

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.