Question link:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4539
Question meaning:
Given a matrix, soldiers cannot be placed in some locations. Each soldier can attack all positions where the Hamilton distance is exactly 2. Find the maximum number of soldiers that can be placed so that there is no conflict between soldiers.
Solution:
Status compression DP.
Because the current position is related to the first two rows, the adjacent two rows are regarded as two-dimensional states, and the state of row I can be obtained by enumerating the rows of the I-2.
Recording status DP [] [I] [J] indicates the number of soldiers that can be put when the current row is in the I state and the previous row is in the J state.
DP [] [I] [J] = CAL (I) + max (DP [] [J] [k]);
Code:
# Include <iostream> # include <cmath> # include <cstdio> # include <cstdlib> # include <string> # include <cstring> # include <algorithm> # include <vector> # include <map> # include <set> # include <stack> # include <list> # include <queue> # define EPS 1e-6 # define INF 0x1f1f1f1f # define PI ACOs (-1.0) # define ll _ int64 # define lson L, M, (RT <1) # define rson m + 1, R, (RT <1) | 1 // # pragma comment (linker, "/Stack: 1024000000,1024000000") using names Pace STD;/* freopen ("data. in "," r ", stdin); freopen (" data. out "," W ", stdout); */# define maxn 110int DP [2] [1 <10] [1 <10]; int save [maxn]; int Cal (INT st) // count 1 {int res = 0; while (ST) {If (st & 1) RES ++; ST >>=1 ;} return res;} int main () {int n, m, TMP; while (~ Scanf ("% d", & N, & M) {for (INT I = 1; I <= N; I ++) {save [I] = 0; For (Int J = 0; j <m; j ++) {scanf ("% d", & TMP); If (! TMP) // 0 indicates that it cannot be put, and can be put as 1. Save [I] | = (1 <j) ;}} memset (DP, 0, sizeof (DP); int Lim = 1 <m; For (INT I = 0; I <Lim; I ++) {If (I & (I <2) | (I & save [1]) // This status meets the condition of continue; DP [1] [I] [0] = CAL (I); // only remember the status 0, indicating a flag} int LA = 1, cur; for (INT I = 2; I <= N; I ++) {cur = La ^ 1; // memset (DP [cur], 0, sizeof (DP [cur]); For (Int J = 0; j <Lim; j ++) {If (J & (j <2 )) | (J & save [I]) // This status meets the conditions of continue; For (int K = 0; k <Lim; k ++) {If (K & (k <2) | (K & save [I-1]) // This State meets the requirements of the current row continue; if (J & (k <1) continue; If (J & (k> 1) // you must consider moving left to the right; DP [cur] [J] [k] = CAL (j); // number of rows 1 int max = 0; For (INT p = 0; P <Lim; P ++) {If (P & (P <2) | (P & save [I-2]) continue; if (K & (P <1) | (J & P) // The Relationship Between the I-2 row and the I-1 row continue; if (K & (P> 1) // The Relationship Between the I-2 row and the I row continue; max = max (max, DP [la] [k] [p]);} DP [cur] [J] [k] + = max ;}} LA = cur;} int ans = 0; for (INT I = 0; I <Lim; I ++) for (Int J = 0; j <Lim; j ++) // note that the entire link cannot be composed of 0 ans = max (ANS, DP [cur] [I] [J]); printf ("% d \ n", ANS );} return 0 ;}