POJ 1753 Flip Game(枚举变元的高斯消元)

46 篇文章 0 订阅
23 篇文章 0 订阅

以前做这道题目的时候好像是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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值