Check count (1)
Time limit:10000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 8170 Accepted Submission (s): 3095
problem DescriptionGive you a n*n checkered checkerboard with a non-negative number in each lattice.
Take out a number of the number, so that any of the two number of the lattice does not have a common edge, that is, the number of the 2 lattice is not adjacent, and the number of the largest.
InputIncludes multiple test instances, each of which consists of an integer n and n*n non-negative numbers (N<=20)
OutputFor each test instance, the output may be the largest and
Sample Input375 15 21 75 15 28 34 70 5
Sample Output188 more simple state compression DP
1 //2016.9.82#include <iostream>3#include <cstdio>4#include <cstring>5 6 using namespacestd;7 8 intarr[ A][ A];9 intdp[ A][20000], sum[ A][20000];//Dp[i][j] Indicates the maximum value that a row I can obtain using the J method, and Sum[i][j] indicates that the first line of the first method is obtained by using theTen intstate[20000];//indicates a viable state, i.e. a feasible method of taking a number One intLen, N; A - BOOLOkintSta//viable State, i.e. 1 of position 22 not adjacent - { the return(sta& (sta<<1))==0?true:false; - } - - intGet_sum (intPosintX//For POS line, using the X method can be obtained and + { - intsum =0, cnt =1; + while(x) A { atSum + = (x2) *arr[pos][n-CNT]; -X >>=1; -cnt++; - } - returnsum; - } in - voidInitintM//Initialize to { +Len =0; - for(inti =0; I < (1<<M); i++) the if(OK (i)) state[len++] =i; * for(inti =0; I < n; i++) $ for(intj =0; J < Len; J + +)Panax NotoginsengSUM[I][J] =get_sum (i, state[j]); -Memset (DP,0,sizeof(DP)); the for(inti =0; i < Len; i++) +dp[0][i] = sum[0][i]; A } the + intMain () - { $ while(cin>>N) $ { - for(inti =0; I < n; i++) - for(intj =0; J < N; J + +) thescanf"%d", &arr[i][j]); - init (n);Wuyi for(inti =1; I < n; i++)//Processing line I the for(intj =0; J < Len; J + +)//adopt the method of the first J - for(intK =0; K < Len; k++)//enumerates the methods taken on the previous line K Wu if((state[j]&state[k]) = =0)//Methods J and K are feasible. PS: To add parentheses,& priority than = = also low, debug a half-day a face, really drunk -DP[I][J] = max (Dp[i][j], dp[i-1][K]+SUM[I][J]);//state transition Equation About $ intAns =0; - for(inti =0; i < Len; i++)//find the maximum value - if(dp[n-1][i]>ans) -Ans = dp[n-1][i]; A +cout<<ans<<Endl; the } - return 0; $}
HDU1565 (State compression DP)