这篇代码仅仅是因为翻了一圈题解都没找到适合我的码风,故此
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int N = 9;
double mp[N][N] , sum[N][N] , n , x0;
double f[N][N][N][N][16];
double get(int x1 , int y1 , int x2 , int y2)
{
return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
}
double solve(int x1 , int y1 , int x2 , int y2 , int num)
{
if(f[x1][y1][x2][y2][num] > -1) return f[x1][y1][x2][y2][num];
if(num == 1)
{
double k = get(x1 , y1 , x2 , y2) - x0;
return f[x1][y1][x2][y2][num] = k * k;
}
double mmin = 10000010;
for(int i = x1 ; i < x2 ; i++)
{
double k1 = get(x1 , y1 , i , y2) - x0 , k2 = get(i + 1 , y1 , x2 , y2) - x0;
mmin = min(mmin , min(k1 * k1 + solve(i + 1 , y1 , x2 , y2 , num - 1) , solve(x1 , y1 , i , y2 , num - 1) + k2 * k2) );
}
for(int i = y1 ; i < y2 ; i++)
{
double k1 = get(x1 , y1 , x2 , i) - x0 , k2 = get(x1 , i + 1 , x2 , y2) - x0;
mmin = min(mmin , min(k1 * k1 + solve(x1 , i + 1 , x2 , y2 , num - 1) , solve(x1 , y1 , x2 , i , num - 1) + k2 * k2) );
}
return f[x1][y1][x2][y2][num] = mmin;
}
int main()
{
cin >> n;
for(int i = 1 ; i <= 8 ; i++)
for(int j = 1 ; j <= 8 ; j++)
for(int x = 1 ; x <= 8 ; x++)
for(int y = 1 ; y <= 8 ; y++)
for(int t = 1 ; t <= (int)n ; t++) f[i][j][x][y][t] = -1;
for(int i = 1 ; i <= 8 ; i++)
for(int j = 1 ; j <= 8 ; j++)
{
cin >> mp[i][j];
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + mp[i][j];
}
x0 = sum[8][8] / n;
// cout << x0 << endl;
printf("%.3lf" , sqrt(solve(1 , 1 , 8 , 8 , (int)n) / n));
return 0;
}