Reprinted please indicate the source, thank youHttp://blog.csdn.net/ACM_cxlove? Viewmode = Contents
By --- cxlove
Question: Give the four points on the ball, and find the coordinates of the ball center.
Http://acm.hfut.edu.cn/OnlineJudge/
It is simulated annealing...
At that time, I had no idea. I had to give up after I had been decisive.
For each vertex, we calculate the error from the center of the target. Variance ???
A random point, and then set an initial step, each time a random 30 direction out, if better than the current position, then update
Until the step size reaches the required precision.
The time limit for this question is 3 s, and the range is 1000. It seems that the precision requirement is still very high, and each step can be reduced by 0.05 times.
In the end, 1 S + is over and the efficiency is not high.
# Include <iostream> # include <fstream> # include <iomanip> # include <cstdio> # include <cstring> # include <algorithm> # include <cstdlib> # include <cmath> # include <set> # include <map> # include <queue> # include <stack> # include <string> # include <vector> # include <sstream> # include <ctime> # include <cassert> # define ll long # define EPS 1e-8 # define INF 999999.0 # define zero () FABS (a) <EPS # define N 20 # define PI ACOs (-1.0) using namesp Ace STD; struct point {Double X, Y, Z; bool check () {If (FABS (x) <500 + EPS & FABS (z) <500 + EPS & FABS (y) <500 + EPS) return true; return false ;}} P [4], cur, central; double dist (point P1, point P2) {return SQRT (p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y) + (p1.z-p2.z) * (p1.z-p2.z);} // error between the Current State and the target State; double get_diff (point CEN) {double diff = 0, sum = 0, DIS [4]; for (INT I = 0; I <4; I ++) {dis [I] = dist (CEN, P [I]); sum + = dis [I];} sum /= 4; for (INT I = 0; I <4; I ++) diff + = (DIS [I]-sum) * (DIS [I]-sum ); return diff;} int main () {int T, CAS = 0; scanf ("% d", & T); While (t --) {for (INT I = 0; I <4; I ++) scanf ("% lf", & P [I]. x, & P [I]. y, & P [I]. z); Central. X = Central. y = Central. z = 0; // step size: initial 1000, initial position: 0, 0 double step = 1000, best = get_diff (central); While (Step> 0.001) {for (INT I = 0; I <30; I ++) {// random direction double A = (RAND () % 30000 + 1) /30000.0 * pI * 2; Double B = (RAND () % 30000 + 1 )/30000.0 * pI * 2; cur. X = Central. X + step * sin (a) * Cos (B); cur. y = Central. Y + step * sin (a) * sin (B); cur. z = Central. Z + step * Cos (a); If (! Cur. check () continue; double TMP = get_diff (cur); If (TMP + EPS <best) {best = TMP; Central = cur ;}} step * = 0.95 ;} printf ("case # % d: %. 1f %. 1f %. 1f \ n ", ++ cas, Central. x, Central. y, Central. z);} return 0 ;}