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<<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