POJ 1753 Flip Game (Dfs+ enumeration)

Source: Internet
Author: User

POJ 1753

Test instructions

Enter a 4*4 image, consisting of black and white color, define an operation: Change the color of a small ball in a lattice (black to white, white to black), while its top and bottom around the lattice inside the ball will also change color. The minimum number of operations can make it a solid color pattern.

Ideas:

For a lattice operation an even number of times equals no operation, the operation odd number of times equals the operation once, so the answer is between 0~16 and impossible.

Starting from N=0 enumeration of the possible composition of n operations, that is, the operation of which lattice, if a certain combination can change the pattern to a solid color will stop.

Because the total number of combinations reaches 2^16, enumeration combinations can be traced back by DFS.


Another method is the bitwise operation +BFS, the details can be referenced http://www.cnblogs.com/lyy289065406/archive/2011/07/29/2120501.html


Code

/** @author novicer* language:c++/c*/#include <iostream> #include <sstream> #include <fstream># include<vector> #include <list> #include <deque> #include <queue> #include <stack># include<map> #include <set> #include <bitset> #include <algorithm> #include <cstdio># include<cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime># Include<iomanip> #define INF 2147483647#define CLS (x) memset (x,0,sizeof (x)) #define RISE (I,A,B) for (int i = A; I < = b; i++) using namespace Std;const double eps (1e-8); typedef long LONG lint;int map[6][6] = {};int flag = 0, Step;int change_r Ow[] = {0,-1,0,1,0};int change_col[] = {0,0,1,0,-1};bool All_color () {for (int i = 1; I <= 4; i++) for (int j = 1; J &lt ; = 4; J + +) if (map[i][j]! = map[1][1]) return False;return true;} void Flip (int row, int col) {for (int i = 0; i < 5; i++) map[row+change_row[i]][col+change_col[i]] =! map[row+change_row[i]][col+Change_col[i]];} void Dfs (int row, int col, int deep) {if (deep = = Step) {flag = All_color (); return;} if (flag) return;if (Row > 4 | | col > 4) Return;flip (Row,col), if (Row < 4) Dfs (row+1, col, deep+1); Elsedfs (1, col+1, deep+1); Flip (Row,col); if (Row < 4) Dfs (row+1, col, deep); Elsedfs (1, col+1, deep); return;} void input () {for (int i = 1, i <= 4; i++) for (int j = 1; J <= 4; j + +) {char c;cin >> C; MAP[I][J] = (c = = ' B ')? 1:0;}} void Solve () {for (step = 0; step <=, step++) {DFS (1,1,0); if (flag) break;} if (flag) cout << step << endl;elsecout << "impossible" << Endl;} int main () {//freopen ("Input.txt", "R", stdin),//freopen ("Output.txt", "w+", stdout); input (); solve (); return 0;}


Copyright notice: Bo Master said authorized all reproduced:)

POJ 1753 Flip Game (Dfs+ enumeration)

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.