Poj 1753 flip game (Gaussian deyuan)

Source: Internet
Author: User
Tags rounds

Flip game
Time limit:1000 ms   Memory limit:65536 K
Total submissions:30805   Accepted:13409

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. one side of each piece is white and the other one is black and each piece is lying either it's black or white side up. each round you 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 are chosen every round according to the following rules:
    1. Choose any one of the 16 pieces.
    2. 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 are 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 is shown at the picture), then the field will 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 are to write a program that will 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" each that 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 from the given position. if the goal is initially achieved, then write 0. if it's impossible to achieve the goal, then write the word "impossible" (without quotes ).

Sample Input

 
Bwwbbbwbbwwbbwww

Sample output

 
4


Simple question about Gaussian elimination:


# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> using namespace STD; int A [35] [35], X [35]; int equ, VAR; // Number of equations and variable elements void debug () {for (INT I = 0; I <equ; I ++) {for (Int J = 0; j <= var; j ++) printf ("% d", a [I] [J]); puts ("") ;}puts ("");} int Gauss () {int max_r, Col = 0; For (INT I = 0; I <equ & Col <var; I ++, Col ++) {max_r = I; for (Int J = I + 1; j <equ; j ++) {if (a [J] [col]> A [max_r] [col]) max_r = J;} If (max_r! = I) {for (int K = 0; k <= var; k ++) {swap (A [I] [K], A [max_r] [k]) ;}} if (a [I] [col] == 0) {I --; Continue ;}for (Int J = I + 1; j <equ; j ++) if (I! = J & A [J] [col]) {for (int K = Col; k <= var; k ++) A [J] [k] = A [I] [k] ^ A [J] [k];} // debug (); For (INT I = equ-4; I <equ; I ++) if (a [I] [Var]) Return-1; int ans = 100, sum = 0; For (int K = 0; k <= 15; k ++) {x [equ-1] = (k & 1)> 0? 1:0; X [equ-2] = (k & 2)> 0? 1:0; X [equ-3] = (k & 4)> 0? 1:0; X [equ-4] = (k & 8)> 0? 1:0; // printf ("% d \ n", X [equ-1], X [equ-2], X [equ-3], X [equ-4]); sum = 0; For (INT I = equ-5; I> = 0; I --) {int temp = A [I] [Var]; for (Int J = I + 1; j <var; j ++) {temp-= A [I] [J] * X [J];} X [I] = temp/A [I] [I] ;}for (INT I = 0; I <= 15; I ++) if (X [I] % 2! = 0) {sum ++;} // printf ("sum: % d \ n", sum); ans = min (ANS, sum);} return ans ;} void Init () {memset (A, 0, sizeof (a); memset (x, 0, sizeof (x); For (INT I = 0; I <4; I ++) for (Int J = 0; j <4; j ++) {if (I! = 0) A [I * 4 + J] [(I-1) * 4 + J] = 1; if (I! = 3) A [I * 4 + J] [(I + 1) * 4 + J] = 1; if (J! = 0) A [I * 4 + J] [I * 4 + J-1] = 1; if (J! = 3) A [I * 4 + J] [I * 4 + J + 1] = 1; A [I * 4 + J] [I * 4 + J] = 1;} // debug ();} int main () {equ = Var = 16; char s [20], t [7]; memset (S, 0, sizeof (s); memset (T, 0, sizeof (t )); while (scanf ("% s", S )! = EOF) {for (INT I = 0; I <3; I ++) {scanf ("% s", T); strcat (S, T );} init (); For (INT I = 0; I <16; I ++) {If (s [I] = 'B ') A [I] [Var] = 1; else a [I] [Var] = 0;} int ans1 = Gauss (); Init (); For (INT I = 0; I <16; I ++) {If (s [I] = 'W') A [I] [Var] = 1; else a [I] [Var] = 0;} int ans2 = Gauss (); If (ans1! =-1 & ans2! =-1) printf ("% d \ n", min (ans1, ans2); else if (ans1! =-1) printf ("% d \ n", ans1); else if (ans2! =-1) printf ("% d \ n", ans2); else printf ("impossible \ n");} return 0 ;}






Poj 1753 flip game (Gaussian deyuan)

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.