状压DP uvalive 6560

 1 // 状压DP uvalive 6560
 2 // 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值
 3 // 思路:
 4 // dp[i][j]:第i行j状态下的值
 5 // j:0表示不合并,1表示向下合并
 6 // 一开始输入要修改一下,然后滚动数组优化
 7 
 8 #include <iostream>
 9 #include <algorithm>
10 #include <cstring>
11 #include <cstdio>
12 #include <vector>
13 #include <cmath>
14 #include <map>
15 #include <queue>
16 using namespace std;
17 #define LL long long
18 typedef pair<int,int> pii;
19 const int inf = 0x3f3f3f3f;
20 const int MOD = 998244353;
21 const int N = 1020;
22 const int maxx = 200010; 
23 #define clc(a,b) memset(a,b,sizeof(a))
24 const double eps = 0.025;
25 void fre() {freopen("in.txt","r",stdin);}
26 void freout() {freopen("out.txt","w",stdout);}
27 inline int read() {int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
28 
29 int n;
30 int g[N][3];
31 int dp[2][10];
32 bool check(int u,int f){
33     for(int i=0;i<3;i++){
34         if(u&(1<<i)&&f&(1<<i)) return false;
35     }
36     return true;
37 }
38 
39 int cal(int r,int u,int f){
40     int ans=0;
41     for(int i=0;i<3;i++){
42         if(f&(1<<i)) ans+=g[r][i]*g[r-1][i];
43     }
44     int t=u|f;
45     int tem1=0,tem2=0;
46     if(!(t&1)&&!(t&2)) tem1=g[r][0]*g[r][1];
47     if(!(t&2)&&!(t&4)) tem2=g[r][1]*g[r][2];
48     ans+=max(tem1,tem2);
49     return ans;
50 }
51 int main(){
52     int cas=1;
53     while(~scanf("%d",&n),n){
54         for(int i=0;i<3;i++){
55             for(int j=1;j<=n;j++){
56                 scanf("%d",&g[j][i]);
57             }
58         }
59         memset(dp[0],0,sizeof(dp[0]));
60         int p=0;
61         int ans=-inf;
62         for(int i=1;i<=n;i++){
63             int pre=p;
64             p=p^1;
65             memset(dp[p],0,sizeof(dp[p]));
66             for(int j=0;j<8;j++){
67                 for(int k=0;k<8;k++){
68                     if(check(j,k)){
69                         dp[p][j]=max(dp[p][j],dp[pre][k]+cal(i,j,k));
70                     }
71                 }
72                 if(i==n){
73                     ans=max(ans,dp[p][j]);
74                 }
75             }
76         }
77         printf("Case %d: ",cas++);
78         printf("%d
",ans);
79     }
80     return 0;
81 }
原文地址:https://www.cnblogs.com/ITUPC/p/5877012.html