Title: zoj problem set-2563 long dominoes
Question: give a small rectangle of 1*3, and find the maximum number of different methods covering the matrix of M * n?
Analysis: There is a topic of 1*2, which is quite popular. Link: here
This is almost the same, that is, the status of the current row has an impact on the previous row and on the previous row. So
Definition status: DP [I] [now] [Up] indicates the number of solutions when the status of row I is now and that of the last row is up.
Then the transfer equation: DP [I] [now] [Up] = sum (DP [I-1] [Up] [uup]) the premise is legal
The determination of legitimacy is more difficult to consider, because it is 1*3 and can only be placed horizontally or vertically.
If it is placed horizontally, the 3 cells of up and uup must be full.
If it is placed vertically, the current cells of up and uup must be empty.
If you use simple enumerative DP, the layer-4 loop times out, so we need to use up and upp to deeply search and construct the current row.
AC code:
# Include <cstdio> # include <cstring> # include <string> # include <iostream> # include <algorithm> # include <vector> using namespace STD; # define int long intconst long n = 9; const int INF = 0x3f3f3f; int DP [31] [1 <n] [1 <n]; int m, n; bool judge (int s, int up, int UPP) {for (INT I = s; I <S + 3; I ++) if (! (Up & (1 <I) |! (UPP & (1 <I) return false; return true;} void DFS (INT St, int CNT, int now, int up, int UPP, int num) {If (CNT = m) {DP [st] [now] [Up] + = num; return;} If (CNT + 3 <= M & judge (CNT, up, UPP) // Heng DFS (St, CNT + 3, now | (1 <CNT) | (1 <(CNT + 1 )) | (1 <(CNT + 2), up, UPP, num); If (! (Up & (1 <CNT ))&&! (UPP & (1 <CNT) // vertical DFS (St, CNT + 1, now | (1 <CNT), up | (1 <CNT ), UPP, num); If (UPP & (1 <CNT) // leave null DFS (St, CNT + 1, now, up, UPP, num );} int main () {While (~ Scanf ("% d", & M, & N) & M + n) {memset (DP, 0, sizeof (DP); DFS (1, 0, 0, (1 <m)-1, (1 <m)-1, 1); For (INT I = 2; I <= N; I ++) {for (INT up = 0; up <(1 <m); up ++) // upstream {for (int st = 0; st <(1 <m); ST ++) // The upper line {If (DP [I-1] [Up] [st]) // be sure to judge DFS (I, 0, 0, up, St, DP [I-1] [Up] [st]) ;}} printf ("% LLD \ n ", DP [N] [(1 <m)-1] [(1 <m)-1]);} return 0 ;}
Enumeration timeout code:
# Include <cstdio> # include <cstring> # include <string> # include <iostream> # include <algorithm> # include <vector> using namespace STD; # define int long intconst long n = 9; const int INF = 0x3f3f3f; int DP [31] [1 <n] [1 <n]; int m, n; bool isit (INT st) {int TMP = 0; For (INT I = 0; I <m; I ++) {If (st & (1 <I) TMP ++; else {If (TMP % 3) return false ;}} if (TMP % 3) return false; return true;} int solve (INT now, int Up, int uup) {for (INT I = 0; I <m; I ++) {If (! (Uup & (1 <I) // judge whether the last row is 0 {If (up & (1 <I) Return-1; else {If (! (Now & (1 <I) Return-1; else up | = (1 <I) ;}} else {If (up & (1 <I) & now & (1 <I) // 1 1 {if (I = m-2 | I = m-1) Return-1; else if (! (Now & (1 <(I + 1) |! (Now & (1 <(I + 2) |! (Up & (1 <(I + 1) |! (Up & (1 <(I + 2) |! (Uup & (1 <(I + 2) |! (Uup & (1 <(I + 1) Return-1; I + = 2 ;}} return up ;}int main () {While (~ Scanf ("% d", & M, & N) & M + n) {for (int st = 0; ST <(1 <m ); st ++) {If (! Isit (ST) continue; For (INT up = 0; up <(1 <m); up ++) {If (isit (up )) {DP [2] [st] [Up] = 1 ;}}for (INT I = 3; I <= N; I ++) {for (int no = 0; no <(1 <m); no ++) {for (INT Kp = 0; KP <(1 <m ); KP ++) // upstream DP [I] [No] [KP] = 0; For (INT up = 0; up <(1 <m); up ++) // upstream {int temp; For (int st = 0; ST <(1 <m); ST ++) // If (temp = solve (no, up, St ))! =-1) DP [I] [No] [temp] + = DP [I-1] [Up] [st]; // printf ("% d \ n ", DP [I] [Up] [Up]) ;}} printf ("% LLD \ n", DP [N] [(1 <m) -1] [(1 <m)-1]);} return 0 ;}
Zoj problem set-2563 long dominoes [Pressure DP]