HDU (2575) Ocean Currents

Source: Internet
Author: User

Question:
A boat walks in the water and wants to reach its destination. Then there are eight directions. Use 0 ...... 7: North, Northeast, east, southeast ......, But it has a direction at any point, and when its next direction is the same as its own wind direction at the moment (that is, the next direction is the same as its number ), this step does not require energy consumption. Otherwise, the energy consumed is 1 unit of energy. Then the minimum energy required for the boat to reach the destination is required.
Solution:
It seems that the implementation method is relatively easy. The priority queue should be used, but the status mark should be slightly changed. The passing points cannot be simply used with visited [], because one point expands, it is possible that the next point also needs to expand. Because the current point goes to the next point (that is, the point where the previous point expands out) and consumes less energy than the previous one, so we need to use another VST [] to mark the amount of energy that has passed through this point. As long as the energy value reaching this point can be smaller than this point, you can join the team. (Note that each vertex can be taken multiple times. It is important to know the status)

 

# Include <stdio. h>
# Include <string. h>
# Include <queue>
Int dir [8] [2] = },
{1}, {1,-1}, {0,-1}, {-1,-1}; these eight directions are ordered ..
Char map [2100] [2100];
Int VST [2, 2100] [2100];
Using namespace STD;
Int n, m;
Struct point
{
Int X, Y;
Int time;
Friend bool operator <(const point & A, const point & B) // Why does the subsequent Writing Time out? I hope Daniel can explain friend bool operator <(point a, point B)
{
Return A. Time> B. time;
}
};
Int judge (int x, int y)
{
Return x> = 1 & x <= N & Y> = 1 & Y <= m;
}
Int BFS (INT x0, int y0, int X1, int Y1)
{
Int I, j, dx, Dy;
For (I = 1; I <= N; I ++)
For (j = 1; j <= m; j ++)
VST [I] [J] = 999999;
Priority_queue <point> q;
Point In, out;
In. x = x0;
In. Y = y0;
In. Time = 0;
VST [x0] [y0] = 0;
Q. Push (in );
While (! Q. Empty ())
{
Out = Q. Top ();
Q. Pop ();
If (Out. x = x1) & (Out. Y = Y1 ))
Return out. Time;
For (I = 0; I <8; I ++)
{
DX = in. x = out. x + dir [I] [0];
DY = in. Y = out. Y + dir [I] [1];
If (! Judge (dx, Dy ))
Continue;
If (I = (Map [out. x] [out. Y]-'0 '))
In. Time = out. Time;
Else
In. Time = out. Time + 1;
If (in. time <VST [dx] [dy])
{
VST [dx] [dy] = in. time;
Q. Push (in );
}
}
}
Return 0;
}
Int main ()
{
Int I, J, K, h;
Int ex, ey, Sx, Sy;
While (scanf ("% d", & N, & M )! = EOF)
{
For (I = 1; I <= N; I ++)
{
Scanf ("% s", map [I] + 1 );
}
Scanf ("% d", & K );
While (k --)
{
Scanf ("% d", & Ex, & ey, & Sx, & Sy );
Printf ("% d \ n", BFs (ex, ey, Sx, Sy ));
}
}
Return 0;
}

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.