poj 1753 Flip Game 高斯消元+枚举

poj 1753 Flip Game 高斯消元+枚举 题目链接:http://poj.org/problem?id=1753

题目描述:

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40652 Accepted: 17652

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

Source



题目大意:

在一个4*4的格子中,初始状态分别有白格和黑格,按动其中任意一个格子都会使得该格子和该格子的上面、下面、左面和右面的格子的颜色发生变化,由白变黑,或者由黑变白,为了使16个格子同色,问最少能按动几个格子(一个格子只按一下,同一个格子按两下相当于没有按),使得这16个格子要么都变成黑色,要么都变成白色。


题目分析:

一道典型的高斯消元+枚举的题目,之所以要枚举就是因为在对增广阵进行消元之后,打印出增广矩阵看一下,发现会有4行全是0的,这就说明有4个自由变元,那么在回带时,就要对这四个自由变元进行0-1的枚举;再者,由于此题目说是即可以都变成黑色,也可以都变成白色,所以,会得到两个不同的增广阵,则要进行两次高斯消元,在两次都有解的情况下,求解最小值即可。


代码实现:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=16;

int a[maxn][maxn+1],x[maxn];//a 是系数矩阵和增广矩阵,x 是最后存放的解
// a[][maxn]中存放的是方程右面的值(bi)
int b[maxn][maxn+1];
int equ,var;//equ 是系数阵的行数,var 是系数矩阵的列数(变量的个数)
int free_num,ans=100000000;
int abs1(int num) //取绝对值
{
    if (num>=0) return num;
    else
        return -1*num;
}
void Debug() //调试输出,看消元后的矩阵值,提交时,就不用了
{
    int i, j;
    for (i = 0; i < equ; i++)
    {
        for (j = 0; j < var + 1; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}
inline int gcd(int a, int b) //最大公约数
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
inline int lcm(int a, int b) //最小公倍数
{
    return a * b / gcd(a, b);
}
void swap(int &a,int &b)
{
    a=a^b;    //交换2 个数
    b=a^b;
    a=a^b;
}
int dfs(int p)
{
    if(p<=free_num-1)
    {
        for(int i=free_num-1;i>=0;i--)
        {
            int tmp=a[i][var]%2;
            for(int j=i+1;j<var;j++)
            {
                if(a[i][j]!=0)
                    tmp=(tmp-(a[i][j]*x[j])%2+2)%2;
            }
            x[i]=(tmp/a[i][i])%2;
        }
        int sum=0;
        for(int i=0;i<var;i++)
            sum+=x[i];
        if(ans>sum)
            ans=sum;
        return 0;
    }
    x[p]=0;dfs(p-1);
    x[p]=1;dfs(p-1);
}
int Gauss()
{
    int k,col = 0; //当前处理的列
    for(k = 0; k < equ && col < var; ++k,++col)
    {
        int max_r = k;
        for(int i = k+1; i < equ; ++i)
            if(a[i][col] > a[max_r][col])
                max_r = i;
        if(max_r != k)
        {
            for(int i = k; i < var + 1; ++i)
                swap(a[k][i],a[max_r][i]);
        }
        if(a[k][col] == 0)
        {
            k--;
            continue;
        }
        for(int i = k+1; i < equ; ++i)
        {
            if(a[i][col] != 0)
            {
                int LCM = lcm(a[i][col],a[k][col]);
                int ta = LCM/a[i][col], tb = LCM/a[k][col];
                if(a[i][col]*a[k][col] < 0)
                    tb = -tb;
                for(int j = col; j < var + 1; ++j)
                    a[i][j] = ( (a[i][j]*ta)%2 - (a[k][j]*tb)%2 + 2 ) % 2; //a[i][j]只有0 和1 两种状态
            }
        }
    }
//上述代码是消元的过程,行消元完成
//解下来2 行,判断是否无解
//注意K 的值,k 代表系数矩阵值都为0 的那些行的第1 行
    for(int i = k; i < equ; ++i)
        if(a[i][col] != 0) return -1; // 无解返回-1
///Debug();
//唯一解或者无穷解,k<=var
//var-k==0 唯一解;var-k>0 无穷多解,自由解的个数=var-k
//能执行到这,说明肯定有解了,无非是1 个和无穷解的问题。
//下面这几行很重要,保证秩内每行主元非0,且按对角线顺序排列,就是检查列
    for(int i = 0; i <equ; ++i)//每一行主元素化为非零
        if(!a[i][i])
        {
            int j;
            for(j = i+1; j<var; ++j)
                if(a[i][j])
                    break;
            if(j == var)
                break;
            for(int k = 0; k < equ; ++k)
                swap(a[k][i],a[k][j]);
        }
// ----处理保证对角线主元非0 且顺序,检查列完成
    free_num=k;
    if (var-k>0)
    {
//无穷多解,先枚举解,然后用下面的回带代码进行回带;
//这里省略了下面的回带的代码;不管唯一解和无穷解都可以回带,只不过无穷解
//回带时,默认为最后几个自由变元=0 而已。
        /*int r[20];
        memset(r,0,sizeof(r));
        for(int i=0;i<16;i++)
        {
            for(int j=15;j>11;j--)
            {
                r[j]=(1<<(j-12))&i?1:0;
            }
        }*/
        dfs(var-1);
        return ans;
    }
    if (var-k==0)//唯一解时
    {
//下面是回带求解代码,当无穷多解时,最后几行为0 的解默认为0;
        for(int i = k-1; i >= 0; --i) //从消完元矩阵的主对角线非0 的最后1 行,开始往
//回带
        {
            int tmp = a[i][var] % 2;
            for(int j = i+1; j < var; ++j) //x[i]取决于x[i+1]--x[var]啊,所以后面的解对前面的解有影响。
                if(a[i][j] != 0)
                    tmp = ( tmp - (a[i][j]*x[j])%2 + 2 ) % 2;
//if (a[i][i]==0) x[i]=tmp; //最后的空行时,即无穷解得
//else
            x[i] = (tmp/a[i][i]) % 2; //上面的正常解
        }
        int sum=0;
        for(int i=0;i<var;i++)
        {
            sum+=x[i];
        }
        return sum;
//回带结束了
    }
}

char str[10][10];
int main()
{
    equ=16;
    var=16;
    memset(a,0,sizeof(a));
    memset(x,0,sizeof(x));
    memset(str,0,sizeof(str));
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(i-1>=0)
                a[4*i+j][4*(i-1)+j]=1;
            if(i+1<=3)
                a[4*i+j][4*(i+1)+j]=1;
            if(j-1>=0)
                a[4*i+j][4*i+j-1]=1;
            if(j+1<=3)
                a[4*i+j][4*i+j+1]=1;
            a[4*i+j][4*i+j]=1;
        }
        scanf("%s",str[i]);
    }
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(str[i][j]=='b')
                a[4*i+j][16]=1;
            else
                a[4*i+j][16]=0;
        }
    }
    ///Debug();
    for(int i=0;i<16;i++)
    {
        for(int j=0;j<=16;j++)
        {
            b[i][j]=a[i][j];
        }
    }

    int flag1,flag2;
    flag1=flag2=0;
    flag1=Gauss();
    int sum1=0,sum2=0;
    sum1=ans;
    for(int i=0;i<16;i++)
    {
        for(int j=0;j<=16;j++)
        {
            a[i][j]=b[i][j];
        }
    }
    for(int i=0;i<16;i++)
    {
        a[i][16]=a[i][16]^1;
    }
    flag2=Gauss();
    sum2=ans;
    if(flag1==-1&&flag2==-1)
        printf("Impossible\n");
    else
    //cout<<"sum1= "<<sum1<<endl;
    //cout<<"sum2= "<<sum2<<endl;
    printf("%d\n",min(sum1,sum2));
    return 0;
}


代码实现二:异或实现:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=16;
int a[maxn][maxn+1],b[maxn][maxn+1],c[maxn][maxn+1];

int Gauss()//消元
{
    int num=0,hg=0,tp=0,ans=0;
    int min1=100,tmp=0;
    for(int i=0; i<16; i++) //i代表列,也是主元的位置
    {
        int k=i;    //k代表行,从对角线的行开始就行
        for(; k<16; k++)
            if(a[k][i]!=0) //找到这列第1个不为0的行,好做主元啊
                break;
        if (k==16)
        {
            hg=i;    //如果k==16,就是含有自由变量了,不能交换和消元,hg表示开始自由变量的行数
            break;
        }

        for(int j=0; j<=16; j++) //交换行
            swap(a[i][j],a[k][j]);
        //if (j==16) cout<<"jiao huan="<<++num<<"  i="<<i<<" k="<<k<<endl;

        //开始消元
        for(int j=0; j<16; j++) //j代表行
            if(i!=j&&a[j][i])  //不是主元行,要消的行已经是1才消
                for(int k=0; k<=16; k++) //k代表列
                    a[j][k]=a[i][k]^a[j][k];
    }
    // if (hg==0) ans=0;//唯一解
    if (hg>0)
    {
        for(int i=hg; i<16; i++) tp+=a[i][16];
        if (tp>0)
        {
            ans=-1;    //无解
            return ans;
        }
        if (tp==0)
        {
            ans=hg; //含有16-hg+1个自由变元
            //下面开始枚举自由解变量,并开始回带
            for(int i=hg; i<16; i++) a[i][i]=1;
            min1=100;
            for(int m=0; m<16; m++)
            {
                tmp=0;
                memset(b,0,sizeof(b));
                for(int i=0; i<16; i++)
                    for(int j=0; j<=16; j++)
                        b[i][j]=a[i][j];

                for(int l=15; l>=hg; l--)
                {
                    b[l][16]=(1<<(l-12))&m?1:0;
                }
                for(int i=hg; i<16; i++)
                {
                    //开始消元
                    for(int j=0; j<16; j++) //j代表行
                        if(i!=j&&b[j][i])  //不是主元行,要消的行已经是1才消
                            for(int k=0; k<=16; k++) //k代表列
                                b[j][k]=b[i][k]^b[j][k];
                }
                for(int i=0; i<16; i++) tmp+=b[i][16];
                if (min1>=tmp) min1=tmp;

            }
            ans=min1;
        }
        //cout<<"min1="<<min1<<endl;
    }
    return ans;
}

int main()
{
    int k,free_num;
    char str[20];
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    for(int i=0; i<4; i++)
    {
        scanf("%s",str);
        for(int j=0; j<4; j++)
        {
            if(i-1>=0)
                a[4*i+j][4*(i-1)+j]=1;
            if(i+1<=3)
                a[4*i+j][4*(i+1)+j]=1;
            if(j-1>=0)
                a[4*i+j][4*i+j-1]=1;
            if(j+1<=3)
                a[4*i+j][4*i+j+1]=1;
            a[4*i+j][4*i+j]=1;
            if(str[j]=='b')
                a[4*i+j][16]=1;
            else
                a[4*i+j][16]=0;
        }
    }
    for(int i=0;i<16;i++)
    {
        for( int j=0;j<=16;j++)
        {
            c[i][j]=a[i][j];
        }
    }
    int flag1=0,flag2=0;
    flag1=Gauss();
    for(int i=0;i<16;i++)
    {
        for(int j=0;j<=17;j++)
        {
            a[i][j]=c[i][j];
        }
    }
    for(int i=0;i<16;i++)
    {
        a[i][16]=a[i][16]^1;
    }
    flag2=Gauss();
    if(flag1==-1&&flag2==-1)
    {
        printf("Impossible\n");
    }
    else
    {
        printf("%d\n",min(flag1,flag2));
    }
    return 0;
}









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值