Good. Clever algorithms. Let's take a look. It's pretty good !! ^ _ ^
# Include <iostream>
# Include <deque>
Using namespace std;
# Include <memory. h>
# Include <math. h>
// Macro definition
# Define MAZE (m, n) maze [(m) * N + (n)]
# Define DISTANCE (m, n) distance [(m) * N + (n)]
// Structure Definition
Typedef struct _ node
{
Int x, y, layernum;
} Node;
Void main ()
{
// Data definition and input. The input matrix is stored in maze, and the shortest distance from each node of the Matrix to the starting point is stored in distance (initialized to-1)
Char * maze;
Int * distance;
Int N, K, start [2], end [2];
Cout <"Input The numbers:" <endl;
Cin> N> K;
Maze = new char [N * N];
Distance = new int [N * N];
Int temp;
For (int I = 0; I <N * N; ++ I ){
Distance [I] =-1;
Cin> temp;
Maze [I] = temp;
}
// Locate the start point and end point
Temp = (char *) memchr (maze, 2, N * N)-maze;
Start [0] = temp/N; start [1] = temp % N;
Temp = (char *) memchr (maze, 3, N * N)-maze;
End [0] = temp/N; end [1] = temp % N;
/*************** Calculate the shortest distance between each point and the source point **************** **********************************
Algorithm principle: traverses graphs composed of all Oasis and start/end points using breadth search,
In addition, the distance matrix data items in the current traversal node and Its subnodes are dynamically updated,
After traversal, the distance matrix stores the optimal route distance from each node to the start point.
**************************************** **************************************** **********/
Deque <node> nodequeue;
Node curnode, newnode;
Int curlayer = 0;
Int ibegin, iend, jbegin, jend, j, delt;
DISTANCE (start [0], start [1]) = 0;
Curnode. x = start [0]; curnode. y = start [1]; curnode. layernum = 0;
Nodequeue. push_back (curnode );
While (! Nodequeue. empty ())
{
Curnode = nodequeue. front ();
While (curnode. layernum = curlayer)
{
Nodequeue. pop_front ();
Ibegin = (curnode. x <K )? 0: (curnode. x-K );
Iend = (curnode. x + K> N-1 )?
<