Flip Game--高斯消元

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 42090 Accepted: 18169

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

题目链接:http://poj.org/problem?id=1753


这个题我写过博客了,题意我就不说了,http://blog.csdn.net/bless924295/article/details/51427270

这个题是个经典的好题,我上次用状态压缩做的,一个16进制数,每一个数表示一种状态。。。。具体的去看我的那篇博客吧,链接我已经放上了,今天我用的是高斯消元做的,在我做poj的时候第一个题就是这个题,我知道可以用高斯消元过,直到今天才过了他。。。高斯消元的讲解看我上几篇博客,这个题就是个裸地高斯。


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=20;
int equ,var;
int det[maxn][maxn];
int gauss(){
    int max_i,k,col,cou=0;
    int free_num[maxn],free_i[maxn],x[maxn];

    memset(free_i,0,sizeof(free_i));
    memset(x,0,sizeof(x));

    for (k=col=0;k<equ&&col<var;k++,col++){//转化为阶梯矩阵
        max_i=k;
        for(int i=k+1;i<equ;i++)// 找到该col列元素绝对值最大的那行与第k行交换.
            if(det[max_i][col]<det[i][col])
                max_i=i;
        if(det[max_i][col]==0){// 说明该col列第k行以下全是0了,则处理当前行的下一列.
            free_i[col]=1;
            x[cou++]=col;
            k--;
            continue;
        }
        if(max_i!=k)
            for(int i=col;i<=var;i++)
                swap(det[max_i][i],det[k][i]);
        for(int i=k+1;i<equ;i++)
            if(det[i][col])// 枚举要删去的行.
                for(int j=col;j<=var;j++)
                    det[i][j]^=det[k][j];
    }
    for(int i=k;i<equ;i++)
        if(det[i][var])//判断是否有解
            return maxn;
    int res=maxn,num=1<<cou;
    for(int i=0;i<num;i++){
        int nu=0,m=i;
        memset(free_num,0,sizeof(free_num));
        for (int j=0;j<cou;j++){//枚举不确定变元
            if (m % 2){
                free_num[x[j]]=1;
                nu++;
            }
            m/=2;
        }
        for(int j=k-1;j>=0;j--)
            if (!free_i[j]){//根据不确定变元,计算确定变元
                int temp=det[j][16];
                for(int l=j+1;l<var;l++)
                    if(det[j][l])
                        temp^=free_num[l];
                free_num[j]=temp;
                if(temp)
                    nu++;
            }
        res=min(res,nu);
    }
    return res;
}
int main(){
    int map1[maxn];
    int j=0;
    char s[maxn];
    for(int i=0;i<4;i++){
        scanf("%s",s);
        for(int x=0;s[x];x++){
            if(s[x]=='b'){
                map1[j++]=1;
            }
            else{
                map1[j++]=0;
            }
        }
    }
    equ=16,var=16;
    memset(det,0,sizeof(det));
    for(int i=0;i<16;i++){
        det[i][i]=1;
        det[i][16]=map1[i];
        if(i<12){
            det[i][i+4]=1;
        }
        if(i>3){
            det[i][i-4]=1;
        }
        if(i%4!=0){
            det[i][i-1]=1;
        }
        if(i%4!=3){
            det[i][i+1]=1;
        }
    }
    int res=gauss();
    memset(det,0, sizeof(det));
    for(int i=0;i<16;i++){
        det[i][i]=1;
        det[i][16]=1-map1[i];
        if(i<12){
            det[i][i+4]=1;
        }
        if(i>3){
            det[i][i-4]=1;
        }
        if(i%4!=0){
            det[i][i-1]=1;
        }
        if(i%4!=3){
            det[i][i+1]=1;
        }
    }
    res=min(res,gauss());
    if (res==maxn)
        printf("Impossible\n");
    else
        printf("%d\n",res);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值