HDU 2167-Shape pressure dp Squares

Source: Internet
Author: User

Test instructions: Give a number, the rule that the eight numbers around after the removal of the numbers are not desirable, the largest number and ideas can be obtained: State compression DP, each row of the method is the state, obviously, due to the rule of the number of restrictions, the desirable state is not 1<<size_col, but very limited, We can preprocess the state (no more than 1600), greatly reducing the complexity of time, running time: 140ms

#include <bits/stdc++.h>using namespacestd;intdp[ -][ the],sta[ the],len,n;intg[ the][ the];voidinit () { for(intI=0;i< (1<< the); i++)if(! (i& (i<<1)) &&! (i& (i>>1))) Sta[len++]=i;//preprocess all the desirable states}intres[ -][ the];//add a memory to avoid duplication of calculationsInlineintCalstatus (intRowintStaintJ//calculates the sum of all numbers in this state of row row{    if(Res[row][j])returnRes[row][j]; intres=0;  for(intj=0; j<n;j++){        if(sta&1) {res+=G[row][j]; } STA>>=1; }    returnres[row][j]=Res;}intMain () {init ();  while(n=0, ~SCANF ("%d", &g[0][n++]) {memset (Res),0,sizeof(Res)); Memset (DP,0,sizeof(DP));//Initialize//Pit Daddy input part         while(true) {scanf ("%d", &g[0][n++]); if(GetChar () = ='\ n') Break; }         for(intI=1; i<n;i++)             for(intj=0; j<n;j++) scanf ("%d",&G[i][j]); //First line preprocessing         for(intI=0; i<n;i++)             for(intj=0;j<len&&sta[j]< (1<<n), J + +) dp[0][j]=calstatus (0, sta[j],j); intans=0;  for(intI=1; i<n;i++)        {             for(intj=0;j<len&&sta[j]< (1<<n); j + +)            {                 for(intk=0;k<len&&sta[k]< (1&LT;&LT;N); k++)                    if(! (sta[k]& (sta[j]<<1)) &&! (sta[k]& (sta[j]>>1)) &&! (Sta[k]&sta[j])) {//Move left right and no move, two states do not conflict, State transferDp[i][k]=max (dp[i][k],dp[i-1][j]+Calstatus (i,sta[k],k)); Ans=Max (ans,dp[i][k]); }}} printf ("%d\n", ans); }    return 0;}

HDU 2167-Shape pressure dp Squares

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.