HDU 2167 pebbles

Source: Internet
Author: User

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

Related Keywords:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.