Calculate the shortest distance from each point to the source point.

Source: Internet
Author: User


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 )?

<

Related Article

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.