Question: I will give you ten Russian squares and ask if you can splice them into a rectangle with a specified length and width. The order of the falling blocks is strictly determined, and the falling blocks cannot fall under the first falling blocks.
Each square is made up of smaller squares. You can use a one-dimensional array to record the number of squares on each column. DFS follows the principle of filling at the bottom. If it can be seamlessly spliced with an existing square, it will be placed up. Otherwise, it will be traced back.
# Include <iostream> # include <cstring> using namespace STD; char Tet [20]; // records the drop order of the square int box [45]; // divide the square into small grids. Each column is recorded with multiple small grids int M, N; bool DFS (INT cur) {If (cur = 10) return true; switch (Tet [cur]) {Case 'I': For (INT I = 0; I <n; I ++) {If (Box [I] + 4 <= m) // you can judge whether the string can be placed vertically. {box [I] + = 4; If (DFS (cur + 1) return true; box [I]-= 4 ;} if (I + 3 <n & Box [I] = Box [I + 1] & Box [I + 1] = Box [I + 2] & Box [I + 2] = Box [I + 3] & Box [I] + 1 <= m) // can I put {for (Int J = I; j <I + 4; j ++) ++ box [J]; If (DFS (cur + 1) horizontally )) return true; For (Int J = I; j <I + 4; j ++) -- Box [J] ;}} break; Case 'O ': for (INT I = 0; I <n; I ++) {if (I + 1 <n & Box [I] = Box [I + 1] & Box [I] + 2 <= m) {box [I] + = 2; Box [I + 1] + = 2; If (DFS (cur + 1) return true; Box [I]-= 2; box [I + 1]-= 2 ;}} break; Case 'l': For (INT I = 0; I <n; I ++) {if (I + 1 <n & Box [I] + 3 <= M & Box [I] = Box [I + 1]) // L {box [I] + = 3; Box [I + 1] + = 1; if (DFS (cur + 1) return true; box [I]-= 3; Box [I + 1]-= 1 ;} if (I + 2 <n & Box [I] + 1 = Box [I + 1] & Box [I + 1] = Box [I + 2] & & Box [I] + 2 <= M & Box [I + 1] + 1 <= m) // Rotate 90 ° {box [I] + = 2 clockwise; Box [I + 1] + = 1; Box [I + 2] + = 1; if (DFS (cur + 1) return true; Box [I]-= 2; Box [I + 1]-= 1; box [I + 2]-= 1 ;} if (I + 1 <n & Box [I] + 1 <= M & Box [I + 1] + 3 <= M & Box [I + 1] + 2 = Box [I]) // rotate 180 ° {box [I] + = 1 clockwise; Box [I + 1] + = 3; if (DFS (cur + 1) return true; box [I]-= 1; Box [I + 1]-= 3 ;} if (I + 2 <n & Box [I] = Box [I + 1] & Box [I + 1] = Box [I + 2] & Box [I + 2] + 2 <= m) // clockwise rotate 270 ° {box [I] + = 1; Box [I + 1] + = 1; Box [I + 2] + = 2; if (DFS (cur + 1) return true; Box [I]-= 1; Box [I + 1]-= 1; box [I + 2]-= 2 ;}} break; Case 'J': For (INT I = 0; I <n; I ++) {if (I + 1 <n & Box [I] = Box [I + 1] & Box [I + 1] + 3 <= m) // 0 {box [I] + = 1; Box [I + 1] + = 3; if (DFS (cur + 1) return true; box [I]-= 1; Box [I + 1]-= 3 ;} if (I + 2 <n & Box [I] = Box [I + 1] & Box [I + 1] = Box [I + 2] & Box [I] + 2 <= m) // 90 {box [I] + = 2; Box [I + 1] + = 1; Box [I + 2] + = 1; if (DFS (cur + 1) return true; Box [I]-= 2; Box [I + 1]-= 1; box [I + 2]-= 1 ;} if (I + 1 <n & Box [I] + 2 = Box [I + 1] & Box [I] + 3 <= M & Box [I + 1] + 1 <= m) // 180 {box [I] + = 3; Box [I + 1] + = 1; if (DFS (cur + 1) return true; box [I]-= 3; Box [I + 1]-= 1 ;} if (I + 2 <n & Box [I] = Box [I + 1] & Box [I + 2] + 1 = Box [I + 1] & & Box [I] + 1 <= M & Box [I + 2] + 2 <= m) // 270 {box [I] + = 1; Box [I + 1] + = 1; Box [I + 2] + = 2; if (DFS (cur + 1) return true; Box [I]-= 1; Box [I + 1]-= 1; box [I + 2]-= 2 ;}} break; Case 'Z': For (INT I = 0; I <n; I ++) {if (I + 2 <n & Box [I + 2] = Box [I + 1] & Box [I + 1] + 1 = Box [I] & Box [I] + 1 <= M & Box [I + 1] + 2 <= m) // 0 {box [I] + = 1; Box [I + 1] + = 2; Box [I + 2] + = 1; if (DFS (cur + 1) return true; Box [I]-= 1; Box [I + 1]-= 2; box [I + 2]-= 1 ;} if (I + 1 <n & Box [I] + 1 = Box [I + 1] & Box [I] + 2 <= M & Box [I + 1] + 2 <= m) // 90 {box [I] + = 2; Box [I + 1] + = 2; If (DFS (cur + 1) return true; box [I]-= 2; Box [I + 1]-= 2 ;}} break; Case 's': For (INT I = 0; I <N; I ++) {if (I + 2 <n & Box [I] = Box [I + 1] & Box [I + 1] + 1 = Box [I + 2] & Box [I + 1] + 2 <= M & Box [I + 2] + 1 <= m) // 0 {box [I] + = 1; Box [I + 1] + = 2; Box [I + 2] + = 1; if (DFS (cur + 1) return true; Box [I]-= 1; Box [I + 1]-= 2; box [I + 2]-= 1 ;} if (I + 1 <n & Box [I + 1] + 1 = Box [I] & Box [I] + 2 <= M & Box [I + 1] + 2 <= m) // 90 {box [I] + = 2; Box [I + 1] + = 2; If (DFS (cur + 1) return true; box [I]-= 2; Box [I + 1]-= 2 ;}} break; Case 'T': For (INT I = 0; I <N; I ++) {if (I + 2 <n & Box [I] = Box [I + 1] & Box [I + 1] = Box [I + 2] & box [I + 1] + 2 <= m) // 0 {box [I] + = 1; Box [I + 1] + = 2; Box [I + 2] + = 1; if (DFS (cur + 1) return true; Box [I]-= 1; Box [I + 1]-= 2; box [I + 2]-= 1 ;} if (I + 1 <n & Box [I] + 1 = Box [I + 1] & Box [I] + 3 <= m) // 90 {box [I] + = 3; Box [I + 1] + = 1; if (DFS (cur + 1) return true; box [I]-= 3; Box [I + 1]-= 1 ;} if (I + 2 <n & Box [I] = Box [I + 2] & Box [I + 1] + 1 = Box [I] & Box [I + 1] + 2 <= m) /// 180 {box [I] + = 1; Box [I + 1] + = 2; Box [I + 2] + = 1; if (DFS (cur + 1) return true; Box [I]-= 1; Box [I + 1]-= 2; box [I + 2]-= 1 ;} if (I + 1 <n & Box [I + 1] + 1 = Box [I] & Box [I + 1] + 3 <= m) // 270 {box [I] + = 1; Box [I + 1] + = 3; if (DFS (cur + 1) return true; box [I]-= 1; Box [I + 1]-= 3 ;}} break;} return false;} int main () {While (CIN> N> M & N | M) {for (INT I = 0; I <10; I ++) cin> Tet [I]; memset (box, 0, sizeof (box); If (DFS (0) cout <"yes" <Endl; else cout <"no" <Endl;} return 0 ;}
HDU 3647 Tetris (violent DFS)