The training reference for the retrospective method of the Violence solution method
Please poke http://luckycat.kshs.kh.edu.tw/
Thinking of solving problems
Backtracking.
You can store an intermediate path with an array,
If the link is complete and less than the minimum value, the intermediate path is saved to the final path.
See the code.
Code
#include <stdio.h>#include<math.h>#include<string.h>Const intMaxLen =Ten; typedefstructPoint {intx, y;} Point; Point Setp[maxlen]; Point Path[maxlen]; Store "Middle" path point Final[maxlen]; Store "final" pathBOOLConnet[maxlen];intN;Doublemin;DoubleDist (Point A, point B) {returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (a.y-b.y));}voidSearch (intStartintLeftDoubleRes) { if(res>min)return ; Else if(left = =0) {min=Res; for(intI=0; i<n; i++) Final[i] =Path[i]; return ; } for(intI=0; i<n; i++)if(!Connet[i]) { DoubleMID =Res; Mid+ = (Dist (Setp[start], setp[i]) +16.0); Connet[i]=true; Path[n-left] =Setp[i]; Search (i, left-1, mid); Connet[i]=false; }}intMain () {scanf ("%d", &N); inttot =0; while(n! =0) {memset (connet,0,sizeof(connet)); Min=1e9; for(intI=0; i<n; i++) { intx, y; scanf ("%d%d", &x, &y); Point temp; Temp.x=x; temp.y=y; Setp[i]=temp; } for(intI=0; i<n; i++) {path[0] =Setp[i]; Connet[i]=true; Search (i, N-1,0); Connet[i]=false; } tot++; printf ("**********************************************************\n"); printf ("Network #%d\n", tot); for(inti=n-1; I>0; i--) {printf ("Cable requirement to connect (%d,%d) to (%d,%d) is%.2LF feet.\n", final[i].x, Final[i].y, Final[i-1].x, final[i-1].Y, Dist (Final[i], final[i-1])+16.0); } printf ("Number of feet of cable required is%.2lf.\n", Min); scanf ("%d", &N); } return 0;}
uva216-Network Connection