Topic Connection
http://poj.org/problem?id=1753
Flip gamedescription
Flip game is played on a rectangular 4×4 field with two-sided pieces placed on each of its squares. One side of each piece are white and the other one are black and each piece is lying either it's black or white side up. Each round your flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped is chosen every round according to the following rules:
- Choose any one of the pieces.
- Flip The chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen Piece (if there is any).
Consider the following position as an example:
Bwbw
Wwww
Bbwb
Bwwb
Here ' B ' denotes pieces lying their black side up and ' w ' denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice was shown at the picture) and then the field would become:
Bwbw
bWWW
Wwwb
Wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You is to write a program, that would search for the minimum number of rounds needed to achieve this goal.
Input
The input consists of 4 lines with 4 characters "W" or "B" from each of the denote game field position.
Output
Write to the output file a single integer number-the minimum number of rounds needed to achieve the goal of the game fro M the given position. If The goal is initially achieved and then write 0. If it's impossible to achieve the goal and then write the word "impossible" (without quotes).
Sample Input
Bwbw
Wwww
Bbwb
Bwwb
Bwwb
Bbwb
Bwwb
bWWW
Wwww
Wwww
Wwww
Wwww
bbbb
bbbb
bbbb
bbbb
bbbb
Bwbb
bbbb
bbbb
Bwbb
Bwbb
Bwbb
bbbb
Bwbb
Wwwb
Bwbb
bbbb
Wwww
Wwwb
Wwbb
Wwwb
Wwww
Wwww
Wwwb
Wwbb
Wbwb
Bwbw
Wbwb
Bwbw
bbbb
Bwwb
Bwwb
bbbb
Bwwb
Wbbw
Wbbw
Bwwb
Bbww
Bbww
Wwbb
Wwbb
Bbwb
Bbbw
Wwbb
Wwwb
Wwwb
Wwbw
Wbww
Wwbw
bbbb
Wwww
Wwbb
Wbbb
Bwwb
Wbwb
Wbbb
Wbbb
Bwbb
Bwbb
Bwbw
Bbbw
Wbwb
bbbb
Bbww
Wbbb
Bbwb
bbbb
Wbwb
bbbb
Sample Output
Impossible
4
0
0
Impossible
Impossible
1
1
1
Impossible
4
4
Impossible
Impossible
Impossible
Impossible
4
5
6
5
Data very small direct explosion search.
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <map>using std::min;using std::find;using std::p air;using std::swap; Using std::vector;using std::multimap; #define PB (E) push_back (e) #define SZ (c) (int) (c). Size () #define MP (A, b) make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) __typeof ((c). Begin ()) #define CLS (arr, Val) memset (arr, Val, si Zeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #define REP (i, n) for (int i = 0; i < (int) n; i++) #defi NE tr (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) const int N = 1000000;const int INF = 0x3f3f3f3f;bool Vis[n & Gt;> 2];struct Node {int s; BOOL Mat[4][4];} que[n];const int dx[] = {0, 0, 0,-1, 1,}, dy[] = {0,-1, 1, 0, 0,};inline int hash (Node &x) {int ret = 0, k = 1; Rep (I, 4) {Rep (J, 4) {ret + k * X.mat[i][j]; K <<= 1; }} RETUrn (ret + n)% n;} void BFs () {int lb = 0, ub = 1, v = hash (que[0]); if (!v | | v = = 65535) {puts ("0"); return;} QUE[0].S = 1, cls (Vis, false), vis[v] = true; while (lb! = UB) {Node &x = que[lb++]; Rep (I, 4) {Rep (J, 4) {Node t = x; Rep (k, 5) {int NX = Dx[k] + i, NY = dy[k] + j; if (NX < 0 | | NX > 3 | | NY < 0 | | NY > 3) Continue; T.mat[nx][ny] ^= 1; } T.S = X.s + 1; v = hash (t); if (!v | | v = = 65535) {printf ("%d\n", x.s); return;} if (Vis[v]) continue; VIS[V] = true; que[ub++] = t; }}} puts ("impossible");} int main () {#ifdef LOCAL freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w+", stdout); #endif Char buf[10]; while (~SCANF ("%s", buf)) {Rep (I, 4) {if (i) scanf ("%s", buf); ReP (J, 4) {que[0].mat[i][j] = buf[j] = = ' B '? 1:0; }} BFS (); } return 0;}
POJ 1753 Flip Game