Enumerate the angle between one edge of the square and the x-axis.
Then we can determin a range ,in which we can obtain the minimum square.
After that, we further enumerate the angel in this range and repeat this step for enough times to
make sure we can get the desired answer.Following is my code:
#include <cmath>
using namespace std;
const int MAXN = 10;
const double PI = acos(-1.0);
int x[100], y[100];
int main()
{
int T;
//freopen("in.txt", "r", stdin);
scanf("%d", &T);
while(T--)
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d", &x[i], &y[i]);
double ret = 1e20;
double base = 0;
double delta = PI / (MAXN -1 );
double arc = 0;
for (int t = 0; t < 30; t++)
{
for (int i = 0; i < MAXN; i++)
{
double minx = 1e20, maxx = -1e20, miny = 1e20, maxy = -1e20;
double sinc = sin( arc );
double cosc = cos( arc );
for (int j = 0; j < n; j++)
{
double xx = x[j] * cosc - y[j] * sinc;
double yy = y[j] * cosc + x[j] * sinc;
minx = minx < xx ? minx : xx;
maxx = maxx > xx ? maxx : xx;
miny = miny < yy ? miny : yy;
maxy = maxy > yy ? maxy : yy;
}
double edge = (maxx - minx) > (maxy - miny) ? (maxx - minx) : (maxy - miny);
//ret = ret < edge * edge ? ret : edge * edge;
if (ret > edge * edge)
{
ret = edge * edge;
base = arc;
}
arc += delta;
}
arc = base - delta;
delta /= MAXN;
delta *= 2;
}
printf("%.2lf\n", ret);
}
return 0;