1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #define inf 1000000000 5 int hash[10][10][10][10][20]; 6 int s[10][10][10][10],fsum[10][10][10][10]; 7 int dp(int x1,int y1,int x2,int y2,int k) 8 { 9 int a,b,sum,temp; 10 if (k==1) hash[x1][y1][x2][y2][k]=fsum[x1][y1][x2][y2]; 11 if (hash[x1][y1][x2][y2][k]!=0) return(hash[x1][y1][x2][y2][k]); 12 sum=inf; 13 for (a=x1;a<x2;a++) 14 { 15 temp=dp(x1,y1,a,y2,k-1); 16 if (temp!=-1&&temp+fsum[a+1][y1][x2][y2]<sum) sum=temp+fsum[a+1][y1][x2][y2]; 17 temp=dp(a+1,y1,x2,y2,k-1); 18 if (temp!=-1&&temp+fsum[x1][y1][a][y2]<sum) sum=temp+fsum[x1][y1][a][y2]; 19 } 20 for (b=y1;b<y2;b++) 21 { 22 temp=dp(x1,y1,x2,b,k-1); 23 if (temp!=-1&&temp+fsum[x1][b+1][x2][y2]<sum) sum=temp+fsum[x1][b+1][x2][y2]; 24 temp=dp(x1,b+1,x2,y2,k-1); 25 if (temp!=-1&&temp+fsum[x1][y1][x2][b]<sum) sum=temp+fsum[x1][y1][x2][b]; 26 } 27 if (sum==inf) hash[x1][y1][x2][y2][k]=-1; 28 else hash[x1][y1][x2][y2][k]=sum; 29 return(hash[x1][y1][x2][y2][k]); 30 } 31 int main() 32 { 33 int n,i,j,x1,y1,x2,y2,map[10][10]; 34 double h,f; 35 while (~scanf("%d",&n)) 36 { 37 h=0.0; 38 for (i=1;i<=8;i++) 39 for (j=1;j<=8;j++) 40 { 41 scanf("%d",&map[i][j]); 42 h+=map[i][j]; 43 } 44 memset(hash,0,sizeof(hash)); 45 memset(s,0,sizeof(s)); 46 for (x1=1;x1<=8;x1++) 47 for (y1=1;y1<=8;y1++) 48 for (x2=x1;x2<=8;x2++) 49 for (y2=y1;y2<=8;y2++) 50 { 51 s[x1][y1][x2][y2]=s[x1][y1][x2-1][y2]+s[x1][y1][x2][y2-1]-s[x1][y1][x2-1][y2-1]+map[x2][y2]; 52 fsum[x1][y1][x2][y2]=s[x1][y1][x2][y2]*s[x1][y1][x2][y2]; 53 } 54 f=1.0*dp(1,1,8,8,n); 55 h=(h/n)*(h/n); 56 printf("%0.3lf\n",1.0*sqrt(f/n-h)); 57 } 58 }
转载于:https://www.cnblogs.com/xiao-xin/articles/3848660.html