Flip GameTime
limit:MS
Memory Limit:65536KB
64bit IO Form At:%i64d &%i64u Submit Status
Description
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
Bwwbbbwbbwwbbwww
Sample Output
4
Othello Flip problem, search, enumeration can do, this time use it to practice Gaussian elimination method
Each relationship has a coefficient of 1, if each piece is flipped to determine whether the result is 0 or 1, get an XOR equation, use Gaussian elimination, find the free element, then convert to binary, enumerate the free elements, find the optimal solution.
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define INF 0x3f3f3f3fint map[20][20], a[20], min1, x[20], freex[20]; char str[5][5]; void getmap (int k) {int i, J; memset (map,0,sizeof (MAP)); for (i = 0; i < 4; i++) {for (j = 0; J < 4; J + +) {A[i*4+j] = (str[i][j]-' 0 ') ^ k; MAP[I*4+J][I*4+J] = 1; if (i > 0) map[i*4+j][i*4+j-4] = 1; if (I < 3) map[i*4+j][i*4+j+4] = 1; if (J > 0) map[i*4+j][i*4+j-1] = 1; if (J < 3) map[i*4+j][i*4+j+1] = 1; }} return; void Swap1 (int p,int q) {int J, temp; temp = a[p]; A[P] = A[q]; A[Q] = temp; for (j = 0; J <; J + +) {temp = Map[p][j]; MAP[P][J] = Map[q][j]; MAP[Q][J] = temp; } return; int solve () {int I, j, K, t = 0, NUM1 = 0; for (i = 0; I < && t < 16; t++, i++) {for (j = i; J <, J + +) if (map[j][t]) break; if (j = =) {freex[num1++] = t; i--; Continue; } if (i! = j) Swap1 (I,J); for (j = i+1; J <, J + +) {if (Map[j][t]) {a[j] = A[j]^a[i]; for (k = t; k < k++) map[j][k] = Map[j][k] ^ map[i][k]; }}} for (; i <; i++) if (a[i]) return-1; if (NUM1) return NUM1; for (i = +; I >= 0; i--) {x[i] = A[i]; for (j = i+1; J <, J + +) X[i] = x[i]^ (Map[i][j]*x[j]); } return NUM1;} int f (int s) {int I, j, K, Key, ans, min1 = INF; Getmap (s); key = Solve (); Ans = 0; if (key = = 0) {ans = 0; for (i = 0; i < i++) ans + = x[i]; min1 = min (Min1,ans); } else if (Key > 0) {int temp = 1<<key; for (int t = 0; t < temp; t++) {memset (x,0,sizeof (x)); Ans = 0; for (j = 0; J < Key; J + +) if (T & (1<<j)) {x[Freex[j]] = 1; ans++; } for (i = >=; i-0; i--) {for (k = 0; k <; k++) if ( MAP[I][K]) break; X[K] = A[i]; for (j = k+1; J <; J + +) X[k] ^= (Map[i][j]*x[j]); Ans + = x[k]; } min1 = min (Min1,ans); }} return min1;} int main () {int I, j, min1 = INF, k, ans, temp; for (i = 0; i < 4; i++) {scanf ("%s", Str[i]); for (j = 0; J < 4; J + +) {if (str[i][j] = = ' B ') str[i][J] = ' 0 '; else str[i][j] = ' 1 '; }} ans = f (0); min1 = min (Min1,ans); ans = f (1); min1 = min (Min1,ans); if (min1 = = INF) printf ("impossible\n"); else printf ("%d\n", min1); return 0;}
Poj1753--flip Game (Gaussian elimination problem 2, enumeration of the first kill of free elements)