POJ 1753 Flip Game(列舉變元的高斯消元)

畫船聽雨發表於2014-07-27

以前做這道題目的時候好像是dfs過的,資料很小搜尋也很簡單。

不過這道題目也可以用高斯消元來搞,類似於16個開關的開關問題。不過終態會有兩種,所以解兩次取一個最優的結果。

最後有多種解的時候要進行列舉沒種情況所對應的解的總和,選一個最小的。

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30436   Accepted: 13222

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

bwwb
bbwb
bwwb
bwww

Sample Output

4
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
#define INF 0x7ffffff
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 2

const int maxn = 210;

using namespace std;

int a[maxn][maxn];
int x[maxn];
int equ, var;
int x1[maxn], x2[maxn];
int free_x[maxn];
int free_num;
int f[maxn];
char str;

int LCM(int a, int b)
{
    return (a/(__gcd(a, b)))*b;
}

int Gauss()
{
    int row, col, max_r;
    int num = 0;
    row = col = 0;
    while(row < equ && col < var)
    {
        max_r = row;
        for(int i = row+1; i < equ; i++)
            if(abs(a[i][col]) > abs(a[max_r][col])) max_r = i;
        if(max_r != row)
            for(int j = col; j <= var; j++) swap(a[row][j], a[max_r][j]);
        if(a[row][col] == 0)
        {
            free_x[num++] = col;
            col++;
            continue;
        }
        for(int i = row+1; i < equ; i++)
        {
            if(a[i][col] == 0) continue;
            int l = LCM(abs(a[row][col]), abs(a[i][col]));
            int ta = l/a[i][col];
            int tb = l/a[row][col];
            if(ta*tb < 0) tb *= -1;///判斷是否異號
            for(int j = col; j <= var; j++)
                a[i][j] = ((a[i][j]*ta - a[row][j]*tb)%mod + mod)%mod;
        }
        row++;
        col++;
    }
    for(int i = row; i < equ; i++)///無解的情況;
        if(a[i][col] != 0) return INF;
    if(var == row)
    {
        for(int i = var-1; i >= 0; i--)///唯一解的情況,根據上三角陣,迭代求出每一次的值
        {
            int tmp = a[i][var];
            for(int j = i+1; j < var; j++)
                if(a[i][j] != 0) tmp = ((tmp-a[i][j]*x[j])%mod + mod)%mod;
            while(tmp%a[i][i] != 0)
                tmp += mod;
            x[i] = tmp/a[i][i]%mod;
        }
        int cnt = 0;
        for(int i = 0; i < 16; i++)
            cnt += x[i];
        return cnt;
    }
    int res = INF;
    int n = 1<<(var-row);
    int t = var-row;
    for(int i = 0; i < n; i++)///列舉變元
    {
        int cnt = 0;
        for(int j = 0; j < t; j++)
        {
            if(i&(1<<j))
            {
                x[free_x[j]] = 1;
                cnt++;
            }
            else
                x[free_x[j]] = 0;
        }
        for(int j = row-1; j >= 0; j--)
        {
            int xx;
            for(xx = j; xx < var; xx++)
                if(a[j][xx]) break;
            x[xx] = a[j][var];
            for(int k = xx+1; k < var; k++)
                if(a[j][k]) x[xx] ^= x[k];
            cnt += x[xx];
        }
        res = min(res, cnt);
    }
    return res;
}

void init()
{
    equ = var = 16;
    memset(x, 0, sizeof(x));
    memset(a, 0, sizeof(a));
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            int t = i*4+j;
            a[t][t] = 1;
            if(i-1 >= 0) a[(i-1)*4+j][t] = 1;
            if(i+1 < 4)  a[(i+1)*4+j][t] = 1;
            if(j-1 >= 0) a[i*4+j-1][t] = 1;
            if(j+1 < 4)  a[i*4+j+1][t] = 1;
        }
    }
}

int main()
{
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            cin >>str;
            if(str == 'w')
                f[i*4+j] = 0;
            else
                f[i*4+j] = 1;
        }
    }
    for(int i = 0; i < 16; i++) x1[i] = 0;
    for(int i = 0; i < 16; i++) x2[i] = 1;
    init();
    for(int i = 0; i < 16; i++) a[i][16] = f[i]^x1[i];
    int flag1 = Gauss();
    init();
    for(int i = 0; i < 16; i++) a[i][16] = f[i]^x2[i];
    int flag2 = Gauss();
    if(flag1 == flag2 && flag1 == INF)
        cout<<"Impossible"<<endl;
    else
        cout<<min(flag1, flag2)<<endl;
    return 0;
}


相關文章