Introduction to algorithms the 15th chapter of Exercise 15-1--the problem of traveling quotient of two-tune Euclid

Source: Internet
Author: User

Idea: 1), first of all points plus coordinates, x-axis pointing right, y-axis pointing down. All points are then arranged from small to large in x-axis coordinates.

2) The general idea is to remove a node from a well-ordered node in turn, and decide whether the node should be placed on the first path or the second path. 3) define an array: Double b[8][8]; B[I][J] indicates that the first path searches to the node I, and the second path searches to the shortest path length after the J node. If i==j indicates that two paths converge to I point

If the i==n indicates the search to the end

3) method of finding two-point distance: Double Length (Node x[],int i,int J)

4) M[i][j] The shortest distance between the numbered I point and the number J point is stored. Need to declare is: B[i][j]=b[j][i];

So, the M-matrix is a symmetric matrix. That is, the distance between point I and J is equal to the matrix of J Point and I point. So here we just need to ask for the lower triangular matrix B.

5) Recursive formula from the above ideas: (I>j for the lower triangle)

(I==J): B[i][j]=b[i][j-1]+length[i][j-1]
(i>j+1): b[i][j]= B[i-1][j]+length[i-1][i]
(i=j+1): B[i][j]=min (1<=k<j) (B[k][j]+length[k][i])//j==1 b[i][j]=length (I,J);

From the above analysis can get the following code:

Double Tune Euclid travel quotient problem #include <iostream> #include <math.h> #define M 65536 using namespace std;
	Define node coordinates struct node {int x;
int y;
}N[10];
	Find the length of the node I and J of the node double (node *n,int i,int j) {double L;
	L=sqrt (double ((n[i].x-n[j].x) * (n[i].x-n[j].x) + (N[I].Y-N[J].Y) * (N[I].Y-N[J].Y));
return L;
	} void ShortPath (Node*n, double (*b) [10],int length) {int i,j,k;
	Double num;
	Defines the starting node ordinal of 1 b[1][1]=0;
			for (i=2;i<=length;i++) {for (j=1;j<=i;j++) {//If the end of the two path is I, the length of the total path is one from 1 to I and//a path from 1 to I-1 plus the distance from i-1 to I.
			if (i==j) {b[i][j]=b[i][j-1]+length (n,i,j-1); }//If there is more than one point between I and J, then the path of J points will not change, and the path of I point is//path from 1 to I-1 plus i-1 to I path if (i>j+1) {b[i][j]=b[i-1][j]+length (n,i-1,i)
			;
				} if (i==j+1) {b[i][j]=m;
				if (j==1) {b[i][j]=length (n,i,j);
					} for (k=1;k<j;k++) {num=b[k][j]+length (n,k,i);
					if (b[i][j]>num) {b[i][j]=num;
		}}} B[j][i]=b[i][j];
	}}} int main () {n[1].x=0;
	n[1].y=0;N[2].x=1;
	n[2].y=6;
	n[3].x=2;
	n[3].y=3;
	n[4].x=5;
	n[4].y=2;
	n[5].x=6;
	n[5].y=5;
	n[6].x=7;
	N[6].y=1;
	n[7].x=8;
	n[7].y=4;
	Double b[10][10]={0};
	ShortPath (n,b,7);
	cout<<b[7][7]<<endl;
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.