#include <vector> #include <cstdlib> #include <memory.h> #include <iostream> #include <math.h> #include <stdio.h> #include <set> using namespace std; long double t[8][8][8][8][20]; int all_sum[8][8][8][8]; int matrix[8][8]; int sum_the_matrix(int i,int j,int x,int y) { if (all_sum[i][j][x][y] != 0) { return all_sum[i][j][x][y]; } int h,v,k,l; int all = 0; for (h=i; h<=x;h++) { for (k=j;k<=y;k++) { all = all+matrix[h][k]; } } all_sum[i][j][x][y] = all; return all_sum[i][j][x][y]; } long double get_the_value(int i,int j,int x,int y,long double means,int times) { if(t[i][j][x][y][times]!=-1.0) { return t[i][j][x][y][times]; } if (i == 0&&j==0&&x==7&&y==0&×==2) { int dfdf = i; } //cout< int all = sum_the_matrix(i,j,x,y); if (times == 0) { t[i][j][x][y][times] = (all-means)*(all-means); return t[i][j][x][y][times]; } set<long double> min_d; int h,v,a,b,c; for (h=i ; h<x ; h++)//horizon
{ int xi = sum_the_matrix(h+1,j,x,y); if ((h-i+y-j)>=(times-1) )//xi is located at the button of the rectangle
{ min_d.insert((xi-means)*(xi-means)+get_the_value(i,j,h,y,means,times-1)); } if ((x-h-1+y-j)>=(times-1))//xi is located at the top of the rectangle
{ min_d.insert((all-xi-means)*(all-xi-means)+get_the_value(h+1,j,x,y,means,times-1)); } } for (v=j;v<y;v++) { int xi = sum_the_matrix(i,v+1,x,y); if ((x-i+y-v-1) >=(times-1))////xi is located at the left of the rectangle
{ min_d.insert((all-xi-means)*(all-xi-means)+get_the_value(i,v+1,x,y,means,times-1)); } if ((x-i+v-j)>=(times-1))////xi is located at the right of the rectangle
{ min_d.insert((xi-means)*(xi-means)+get_the_value(i,j,x,v,means,times-1)); } } t[i][j][x][y][times] = *min_d.begin(); //cout< return t[i][j][x][y][times];
} int main() {
int N,All_sum = 0; long double means = 0.0; int i ,j,x,y,u; memset(all_sum,0,sizeof(int)*8*8*8*8); for (i = 0;i <8;i++) { for (j = 0;j<8;j++) { for (x=0;x<8;x++) { for (y=0;y<8;y++) { for (u=0;u<20;u++) { t[i][j][x][y][u] = -1.0; } } } } } cin>>N; for (i = 0; i <8 ;i++) { for (j= 0;j <8;j++) { cin>>matrix[i][j]; All_sum = All_sum + matrix[i][j]; } } means = (All_sum *1.0)/(N*1.0); printf("%.3lf\n",sqrt(get_the_value(0,0,7,7,means,N-1)/N*1.0)); return 0; }//ps 得提交C++
|