poj棋盘分割(记忆化)

http://poj.org/problem?id=1191

黑书上P116 想了挺久 没想出来 想推出一公式来着 退不出来。。

想偏了  正解:递归

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<stdlib.h>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 #define INF 0xfffffff
 9 #define LL long long
10 int dp[20][10][10][10][10];
11 int aa[10][10],n;
12 int divide(int k,int a,int b,int c,int d)
13 {
14     int i,j,minz = INF,g;
15     if(dp[k][a][b][c][d])
16     return dp[k][a][b][c][d];
17     if(k==n)
18     {
19         int s = 0;
20         for(i = a; i <= c ; i++)
21             for(j = b ; j <= d ; j++)
22             s+=aa[i][j];
23         dp[k][a][b][c][d] = s*s;
24         return s*s;
25     }
26     for(i = a+1 ; i <= c ; i++)
27     {
28         int s = 0;
29         for(g = i ; g <= c ; g++)
30         for(j = b ; j <= d ; j++)
31         s+=aa[g][j];
32         minz = min(minz,s*s+divide(k+1,a,b,i-1,d));
33         s = 0;
34         for(g = a ; g <= i-1 ; g++)
35         for(j = b ; j <= d ; j++)
36         s+=aa[g][j];
37         minz = min(minz,s*s+divide(k+1,i,b,c,d));
38     }
39     for(i = b+1 ; i <= d ; i++)
40     {
41         int s = 0;
42         for(g = a ; g <= c ; g++)
43         for(j = i ; j <= d ; j++)
44         s+=aa[g][j];
45         minz = min(minz,s*s+divide(k+1,a,b,c,i-1));
46         s = 0;
47         for(g = a ; g <= c ; g++)
48         for(j = b ; j <= i-1 ; j++)
49         s+=aa[g][j];
50         minz = min(minz,s*s+divide(k+1,a,i,c,d));
51     }
52     dp[k][a][b][c][d] = minz;
53     return minz;
54 }
55 int main()
56 {
57     int i,j;
58     while(scanf("%d",&n)!=EOF)
59     {
60         double s=0;
61         memset(dp,0,sizeof(dp));
62         int m = 8;
63         n--;
64         for(i = 1; i <= m ;i++)
65             for(j = 1; j <= m ; j++)
66             {
67                 scanf("%d",&aa[i][j]);
68                 s+=double(aa[i][j]);
69             }
70         s = s/double(n+1);
71         int ans = divide(0,1,1,m,m);
72         double an = 1.0/double(n+1)*double(ans)-s*s;
73         printf("%.3lf\n",sqrt(an));
74     }
75     return 0;
76 }
View Code

下面那个不是 贴错了

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<stdlib.h>
 5 #define N 5010
 6 #define M 3010
 7 #define INF 0xfffffff
 8 using namespace std;
 9 int dp[M][N],h[N];
10 int main()
11 {
12     int i,j,n,m,g;
13     scanf("%d%d",&m,&n);
14     for(i = 1; i <= n ;i++)
15     scanf("%d",&h[i]);
16     for(i = 1; i <= m ;i++)
17         for(j = 1; j <= n ;j++)
18         dp[i][j] = INF;
19     int o = INF;
20     for(i = 2 ; i < n ;i++)
21     {
22         dp[1][i] = min(o,(h[i]-h[i-1])*(h[i]-h[i-1]));
23         o = min(dp[1][i],o);
24     }
25     for(i = 2 ; i <= m ; i++)
26     {
27         int o = INF;
28         for(j = 2*i ; j < n-(m-i)*3 ;j++)
29         {
30             if(j-2<(n-(m-i+1)*3))
31             dp[i][j] = min(o,dp[i-1][j-2]+(h[j]-h[j-1])*(h[j]-h[j-1]));
32             else
33             dp[i][j] = min(o,dp[i-1][j-3]+(h[j]-h[j-1])*(h[j]-h[j-1]));
34             o = min(o,dp[i][j]);
35         }
36     }
37     printf("%d\n",dp[m][n-1]);
38     return 0;
39 }
View Code
posted @ 2013-08-16 18:33  _雨  阅读(228)  评论(0编辑  收藏  举报