HDU 3647 Tetris (violent DFS)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.