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;

 

相关文章:

  • 2021-12-06
  • 2021-08-05
  • 2021-05-26
  • 2022-12-23
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2021-06-07
  • 2021-06-02
  • 2021-10-09
相关资源
相似解决方案