For the first time, I thought it was a general back-to-back question. The first thought was to arrange all the circles and make them tangent to the previous circle when placing the new circle, finally, the minimum size of the rectangle is obtained through backtracking. After a dozen minutes of compilation, WA thought about the problem for a while and thought it was a general backtracing question, the initial idea is to arrange the circles in full order, and make them tangent to the previous circle when placing the new circle, and finally obtain the minimum size of the rectangle through backtracking. After a dozen minutes of editing, the result is WA. after thinking about the problem for a while, you cannot just make it tangent to the previous circle (if the first circle is large, the radius is 100, for example, the second circle is very small, the radius is 1, and the third circle is also very large, the radius is also 100, when the third circle is placed, if it is tangent to the second circle, it will surely intersect with the first circle, it cannot be ). Then I started to modify the code, and then I encountered various errors. I made a one-night update on and off. The first thing I want to do is to first tangent a circle to the first place, and then determine whether it is intersecting with the previous circle, if not, it indicates that this placement is correct and the distance is saved the most. if any circle exists with it, it means it cannot be tangent to the previous circle, and then push another one forward, make it tangent to the first circle, and then judge whether it is intersecting with other circles... In order to determine whether the circle exists, a center array is added to store the center position of each circle. This idea is very correct and clear, and then we met three Wa points. WA1: when determining the minimum size of a rectangle, the rightmost position of the last circle cannot be used, because the last circle may be small, the rightmost position is not as far as the rightmost position of the previous circle. Therefore, the maximum value of the center position + radius of each circle is used as the minimum size of the rectangle. WA2: when placing the first few circles, note that the center of the circle cannot be placed at the center of the circle-the radius of the circle <0. For example, if the radius of the first circle is 1 and the radius of the second yuan is 100, the second yuan cannot be tangent to the first circle because the left side of the circle is beyond the wall of the rectangle box! WA3: the MinL setting in the main function is too small. it is estimated that the number of test data is large. the question does not indicate the maximum radius. the starting value is 65536. The result is WA, change DBL_MAX to AC !!
# Include
# Include
# Include
# Include
# Include
Using namespace std; int m, Put [10]; // Put [I]: double MinL, size [10], center [10]; // size [I]: the radius of the circle numbered I; center [I]: place bool vis [10] At the center of the I-th circle; double getlen (int a, int B) // calculate the distance between the two centers {return sqrt (size [a] + size [B]) * (size [a] + size [B])-(size [a]-size [B]) * (size [a]-size [B]);} bool isok (int a, int B) // determines whether it is intersecting with the previously placed circle {for (int I = 0; I
Maxsize) maxsize = center [k] + size [Put [k];} if (maxsize
= 0; j --) {tmpl = getlen (Put [j], I); center [cur] = center [j] + tmpl; if (center [cur]-size [Put [cur] <0) {OK = 0; break;} if (isok (cur, j) break ;} if (OK) dfs (cur + 1); vis [I] = 0 ;}}} int main () {int n; cin> n; while (n --) {cin> m; MinL = DBL_MAX; memset (vis, 0, sizeof (vis); for (int I = 0; I
> Size [I]; for (int I = 0; I