POJ2965-The pilots brothers 'refrigerator

Source: Internet
Author: User

Reprinted please indicate the source: Thank youHttp://user.qzone.qq.com/289065406/blog/1299077378

 

Tip: This question is consistent with the idea of playing a game in poj1753. It should be noted that the flip process should be output. Therefore, BFs cannot be used and DFS must be used. (after finding the target, process Backtracking is required)

Compared with poj1753, you should also pay attention to the method of playing chess. If you do not pay attention to it, it will be a great waste of time and lead to timeout. Because the entire row is flipped, there will be a lot of unnecessary operations at the boundary. Code details

There are two methods for this question: Enum and bit. The following two codes are respectively pasted:

 

/* Code 1: DFS + Enum * // memory time // 240 K 641 Ms // This question is not suitable for BFS because it is necessary to output the chess piece for each flip, use the DFS output path # include <iostream> using namespace STD; bool lock [10] [10] = {false}; bool flag; int step; int Ri [16], CJ [16]; bool isopen (void) {for (INT I = 3; I <7; I ++) for (Int J = 3; j <7; j ++) if (Lock [I] [J]! = True) return false; return true;} void flip (INT row, int col) // In fact, you can also refer to the poj1753 method of playing chess, but it will time out and fail {// The reason for the timeout is that there are too many redundant operations when playing the game lock [row] [col] =! Lock [row] [col]; // poj1753 uses a 6x6 rectangle. You can only play the game in one circle! For (INT I = 3; I <= 6; I ++) // The 10x10 rectangle is used here. The extra operations include three turns! Lock [I] [col] =! Lock [I] [col]; // In fact, you can only use a 4x4 rectangle using bitwise operations, which greatly reduces the time complexity and eliminates unnecessary operations, but the program is complicated, for (Int J = 3; j <= 6; j ++) Lock [row] [J] =! Lock [row] [J]; return;} void DFS (INT row, int Col, int deep) {If (deep = step) {flag = isopen (); return;} If (flag | ROW = 7) return; flip (row, col); ri [Deep] = row; CJ [Deep] = Col; if (COL <6) DFS (row, Col + 1, deep + 1); elsedfs (row + 1, 3, deep + 1); flip (row, col ); if (COL <6) DFS (row, Col + 1, deep); else DFS (row + 1, 3, deep); Return ;} int main (void) {char temp; int I, j; for (I = 3; I <7; I ++) for (j = 3; j <7; j ++) {CIN> temp; if (temp = '-') Lock [I] [J] = true;} For (step = 0; Step <= 16; Step ++) {DFS (3, 3, 0); If (FLAG) break;} cout <step <Endl; for (I = 0; I <step; I ++) cout <Ri [I]-2 <''<cj [I]-2 <Endl; return 0 ;}

 

================ Gorgeous split line ================

 

/* Code 2: DFS + bit * // memory time // 240 K 563 Ms // This question is not suitable for BFS because it is necessary to output the pawns for each flip, the full path of DFS output should be # include <iostream> using namespace STD; int chess; // The Board status int step; bool flag = false; int Ri [16], CJ [16]; bool isopen (void) {If (ChEss = 0 xFFFF) return true; elsereturn false;} void flip (INT bit) {chess = chess ^ (0x1 <bit); // reverse int ROW = bit/4 for the flip bit; int Col = bit % 4; for (int c = 0; C <4; C ++) Chess = chess ^ (0x1 <(row * 4 + C )); // returns the inverse (INT r = 0; r <4; r ++) Chess = chess ^ (0x1 <(R * 4 + col) of the entire row )); // return;} void DFS (INT bit, int deep) {If (deep = step) {flag = isopen (); return ;} if (flag | bit> 15) return; int ROW = Ri [Deep] = bit/4; int Col = CJ [Deep] = bit % 4; flip (BIT ); if (COL <4) DFS (bit + 1, deep + 1); else DFS (bit + 4)/4*4, deep + 1); flip (BIT ); if (COL <4) DFS (bit + 1, deep); elsedfs (bit + 4)/4*4, deep); return;} int main (void) {/* input initial state */Char temp; int I, j; for (I = 0; I <4; I ++) for (j = 0; j <4; j ++) {CIN> temp; If (temp = '-') Chess = chess ^ (0x1 <(I * 4 + J ));} /* DFS */For (step = 0; Step <= 16; Step ++) {DFS (0, 0); If (FLAG) break ;} cout <step <Endl; for (I = 0; I <step; I ++) cout <Ri [I] + 1 <''<cj [I] + 1 <Endl; return 0 ;}

 

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.