There is a matrix of N * n (3 <= n <= 15), from which several numbers can be used to maximize the sum. the number is required. When a certain number is selected, no eight numbers can be selected.
Question: The s array is in a valid state, that is, the number of binary values not adjacent to each other is selected at the same time. sum [I] [J] indicates the sum of the numbers in the J state of row I, in DP, you only need to enumerate the status of the previous row and determine whether it is feasible. Then, you can transfer the optimal solution of each valid state of this row.
#include <cstdio>#include <cstring>#include <algotithm>#define rep(i,n) for(int i=0;i<n;i++)using namespace std;int t,i,n,m,k,dp[16][1<<16],a[16][16],sum[16][1<<16],s[1<<16],ans;int main(){ char str[112]; while(gets(str)){ int len=strlen(str);n=0; for(int i=0;i<len;i+=3)a[0][n++]=(str[i]-‘0‘)*10+(str[i+1]-‘0‘); for(i=1;i<n;i++){ t=0,gets(str); for(int j=0;j<len;j+=3)a[i][t++]=(str[j]-‘0‘)*10+(str[j+1]-‘0‘); } for(i=m=0;i<(1<<n);i++)if((i&(i<<1))==0)s[m++]=i; memset(sum,0,sizeof(sum));memset(dp,0,sizeof(dp)); rep(i,n)rep(j,m)rep(k,n)if(s[j]&(1<<k))sum[i][j]+=a[i][k]; rep(i,m)dp[0][i]=sum[0][i]; for(int i=1;i<n;i++)rep(j,m)rep(k,m){ if(s[j]&s[k])continue; if(s[j]&(s[k]>>1))continue; if(s[j]&(s[k]<<1))continue; dp[i][j]=max(dp[i][j],dp[i-1][k]+sum[i][j]); }ans=0; rep(i,m)ans=max(ans,dp[n-1][i]); printf("%d\n",ans);gets(str); }return 0;}
HDU 2167 pebbles