Data Structure _ Course Design -- Minimum Spanning Tree: indoor cabling
~~~~ Minimum Spanning Tree: indoor cabling ~~~~
Question requirements:
Building a new house is a complicated project. Now you need to write a program to help the homeowner design the layout of indoor wires.
First, the wall socket is fixed. The sockets must be connected with wires and arranged neatly and beautifully. Each line must be parallel to at least one wall and embedded into four walls or floors (not on the roof ).
The homeowner asks to know the shortest length of the wire he needs to buy to connect all the sockets.
In addition, do not forget that each room has a door, and the wires cannot pass through the door. The layout of a room with 4 sockets is provided.
Input requirements:
The input consists of several groups of test data.
The 1st rows of data in each group contain the room length, width, height, and number of sockets N (N is a positive integer not greater than 20 ).
In the next N rows, line I provides the coordinates (xi, yi, zi) of the I socket. the last row contains four 3 tuples (x1, y1, z1)... (X4, y4, z4), which are the three-dimensional coordinates of the four corners of the rectangular door frame. If all four digits are 0, all tests are completed. Do not process the data.
Note: assume that the rectangular room is located in the first quadrant of the three-dimensional Cartesian coordinate system, and there is a corner on the origin. The floor is located in the x-y plane. Subject data guarantee: Each socket only belongs to one side of the four walls, and there is no socket on the door. The two ends of each wire segment must be connected only to the socket, and the wires cannot be cross-welded.
Output requirements:
For each group of tests, output the shortest integer length to connect all sockets to the desired wire in one row.
Input example:
10 10 10 4
0 1 3.3
2.5 0 2
5 0 0.8
5 10 1
0 0 0 0 0 3 1.5 0 1.5 0 3
0 0 0 0
Output example:
21
Pay attention to the following points for this question:
① The wiring should be parallel to the wall
② Relationship between two outlet locations
3 lines can go through the ground, not the roof or door
④ Final data, rounded up
Then, the question is the minimum spanning tree, but I spent a lot of time finding the distance between two sockets = ..
In my code comments, there are four faces, namely, 1, 3, and 4. We are facing face 1, and its left side is face 2, the right side of Area 1 is Area 3, and area 1 is facing area 4.
Then, the program is:
/*************************************** **************************************** * ******** Author: tree ** From: blog.csdn.net/lttree ** Title: Minimum Spanning Tree: indoor cabling ** Source: Data Structure _ Course Design ** Hint: minimum Spanning Tree ************************************* **************************************** * **********/# include
# Include
Using namespace std;/******* definition of some related variables ******* // Number of sockets, up to 20. Therefore, A maximum of 400 edges # define MAX 401 // struct Node {double x, y, z;} nd [21], door [4]; // struct of Edge with Weight Value: struct Edge {int u; int v; double quan;} eg [MAX]; // N-Number of sockets, len, wid, hei-length, width, and height of the room. The position of the pos_door door is int N, len, wid, hei, pos_door; int father [21]. /****** determine the relationship between the two sockets ****** // determine whether the two sockets are in the bool isTogether (Node a, Node B) {if (. x = B. x & (. x = 0 |. x = len) | (. y = B. y & (. y = 0 |. y = wid) return true; return false;} // determine whether the bool isBeside (Node a, Node B) {if (. x = 0 |. x = len) {if (B. y = 0 | B. y = wid) return true; elsereturn false;} else if (. y = 0 |. y = wid) {if (B. x = 0 | B. x = len) return true; elsereturn false; }}// whether the bool is0000ss (Node a, Node B) {if (. x = 0 & B. x = len) | (. y = 0 & B. y = wid) | (. x = len & B. x = 0) | (. y = wid & B. y = 0) return true; elsereturn false;}/****** a series of judgments ****** // evaluate the minimum value of double Min (double, double B) {return
= Door [3]. z | n2.z> = door [3]. z) return false; if (pos_door = 1) {if (n1.y! = 0 & n2.y! = 0) return false; if (n1.x> = door [3]. x & n2.x> = door [3]. x) | (n1.x <= door [0]. x & n2.x <= door [0]. x) return false; return true;} else if (pos_door = 2) {if (n1.x! = 0 & n2.x! = 0) return false; if (n1.y <= door [0]. y & n2.y <= door [0]. y) | (n1.y> = door [3]. y & n2.y> = door [3]. y) return false; return true;} else if (pos_door = 3) {if (n1.x! = Len & n2.y! = Len) return false; if (n1.y <= door [0]. y & n2.y <= door [0]. y) | (n1.y> = door [3]. y & n2.y> = door [3]. y) return false; return true;} else {if (n1.y! = Wid & n2.y! = Wid) return false; if (n1.x> = door [3]. x & n2.x> = door [3]. x) | (n1.x <= door [0]. x & n2.x <= door [0]. x) return false; return true ;}} /****** calculate the wiring length ***** // calculate the shortest wiring of the two sockets on the same wall. double find_togcost (Node a, Node B) {// two sockets with the same wall and no door if (! Judge_crossdoor (a, B) return (fabs (. x-b.x) + fabs (. y-b.y) + fabs (. z-b.z); else {// two sockets wiring will pass through the door, the door location is different if (pos_door = 1 | pos_door = 4) return Min (fabs (. x-b.x) + fabs (door [3]. z-a.z) + fabs (door [3]. z-b.z), (fabs (. x-b.x) +. z + B. z); elsereturnMin (fabs (. y-b.y) + fabs (door [3]. z-a.z) + fabs (door [3]. z-b.z), (fabs (. y-b.y) +. z + B. z) ;}}// calculate the shortest wiring of the Two wall sockets double find_acrcost (Node a, Node B) {double cost1, cost2; Node temp1, temp2; // if (. y = 0 & B. y = wid) | (B. y = 0 &. y = wid) {// calculate the right value of if (pos_door = 1) returnMin (Min (. y + fabs (door [3]. z-a.z) + len + B. y), (. y +. z + len + B. y), Min (wid-a.y + len + wid-b.y), (. z + len + B. z); else if (pos_door = 2) {temp1 = temp2 = a; temp1.y = 0, temp2.y = wid; cost1 = find_togcost (a, temp1 ); cost2 = find_togcost (a, temp2); returnMin (cost1 + len + B. y), (cost2 + len + wid-b.y);} else if (pos_door = 3) {temp1 = temp2 = B; temp1.y = 0, temp2.y = wid; cost1 = find_togcost (B, temp1); cost2 = find_togcost (B, temp2); returnMin (cost1 + len +. y), (cost2 + len + wid-a.y);} elsereturnMin (Min (. y + len + B. y), (wid-a.y + wid-b.y + fabs (door [3]. z-a.z) + len), Min (wid-a.y + wid-b.y +. z + len), (. z + B. z + len);} else {if (pos_door = 1) {temp1 = temp2 = a; temp1.x = 0, temp2.x = len; cost1 = find_togcost (, temp1); cost2 = find_togcost (a, temp2); returnMin (cost1 + wid + B. x), (cost2 + wid + len-b.x);} else if (pos_door = 2) return Min (. x + B. x + wid + fabs (door [3]. z-a.z), (. x + B. x + wid +. z), Min (len-a.x + len-b.x + wid), (. z + B. z + wid); else if (pos_door = 4) {temp1 = temp2 = B; temp1.x = 0, temp2.x = len; cost1 = find_togcost (B, temp1 ); cost2 = find_togcost (B, temp2); return Min (cost1 + wid +. x), (cost2 + wid + len-a.x);} else return Min (. x + B. x + wid), (. z + B. z + wid), Min (len-a.x + len-b.x + fabs (door [3]. z-a.z) + wid), (len-a.x + len-b.x +. z + wid) ;}}// calculate the shortest wiring of the Two wall sockets double find_bescost (Node a, Node B) {Node temp =; // find a point in the Two-plane connection (let one point x and y be 0) and convert it into two same wall sockets if (. x = 0 & B. y = 0) | (B. x = 0 &. y = 0) {temp. x = temp. y = 0; return (find_togcost (a, temp) + find_togcost (B, temp);} else if (. x = len & B. y = 0) | (. y = 0 & B. x = len) {temp. x = len, temp. y = 0; return (find_togcost (a, temp) + find_togcost (B, temp);} else if (. x = 0 & B. y = wid) | (B. x = 0 &. y = wid) {temp. x = 0, temp. y = wid; return (find_togcost (a, temp) + find_togcost (B, temp);} else {temp. x = len, temp. y = wid; return (find_togcost (a, temp) + find_togcost (B, temp) ;}/ ****** find the Minimum Spanning Tree (Kruscal) * ***** // comparison function bool cmp (Edge e1, Edge e2) {return e1.quan
> Len> wid> hei> N) {// if the input data is 4 zeros, exit the program if (! Len &&! Wid &&! Hei &&! N) break; // obtain data (the location of the socket and door) for (I = 0; I
> Nd [I]. x> nd [I]. y> nd [I]. z; for (I = 0; I <4; ++ I) cin> door [I]. x> door [I]. y> door [I]. z; pos_door = judge_d (door);/* calculate the distance between two points (note that the wiring should be parallel to the wall) */k = 0; for (I = 0; I