传纸条的加强版,会了那个,这个也就没问题了。数据范围特别小。6维的也能过吧。。。
1 #include <stdio.h> 2 #include <string.h> 3 int p[21][21],o[61][21][21][21]; 4 int main() 5 { 6 int i,j,k,u,n,max,t; 7 int a[9] = {0,0,0,-1,-1,-1,0,-1}; 8 int b[9] = {0,0,-1,0,-1,0,-1,-1}; 9 int c[9] = {0,-1,0,0,0,-1,-1,-1}; 10 scanf("%d",&n); 11 for(i = 1; i <= n; i ++) 12 for(j = 1; j <= n; j ++) 13 { 14 scanf("%d",&p[i][j]); 15 } 16 memset(o,243,sizeof(o)); 17 o[0][1][1][1] = p[1][1]; 18 for(i = 1; i <= 2*n-2; i ++) 19 { 20 for(j = 1; j <= n&&j <= i+1; j ++) 21 { 22 for(k = 1; k <= n&&k <= i+1; k ++) 23 { 24 for(u = 1; u <= n&&u <= i+1; u ++) 25 { 26 for(t = 0; t <= 7; t ++) 27 { 28 if(t == 0) 29 max = o[i-1][j+a[t]][k+b[t]][u+c[t]]; 30 else 31 { 32 if(max < o[i-1][j+a[t]][k+b[t]][u+c[t]]) 33 max = o[i-1][j+a[t]][k+b[t]][u+c[t]]; 34 } 35 } 36 if(k == j) 37 max += p[j][i+2-j]; 38 else 39 max += p[j][i+2-j]+p[k][i+2-k]; 40 if(j != u&&k != u) 41 max += p[u][i+2-u]; 42 o[i][j][k][u] = max; 43 } 44 } 45 } 46 } 47 printf("%d\n",o[2*n-2][n][n][n]); 48 return 0; 49 }