Flip Game - POJ 1753 高斯消元

来源:互联网 发布:三维模型渲染软件 编辑:程序博客网 时间:2024/06/05 17:46

Flip Game
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 34598 Accepted: 15136

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

题意:求将其翻成全白或者全黑的最小翻动次数

思路:用高斯消元后,可以得到它的所有自由变量,然后状压枚举自由变量的情况,得到一种最小翻动次数的结果。

AC代码如下:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;int T,t,n,m;char s[10][10];int a[20][20],INF=1e9;int Free[20],freenum;bool isfree[20];int p[20];void init(int f){    int i,j,k=0;    memset(a,0,sizeof(a));    for(i=1;i<=4;i++)       for(j=1;j<=4;j++)       {           k++;           a[k][k]=1;           if(s[i][j]-'a'==f)             a[k][17]=1;           if(j!=1)             a[k-1][k]=1;           if(j!=4)             a[k+1][k]=1;           if(i!=1)             a[k-4][k]=1;           if(i!=4)             a[k+4][k]=1;       }}int gauss(int row,int col){    int i,j,k,x,y=0,max_r,S,S2,ans,ret,temp;    freenum=0;    memset(isfree,0,sizeof(isfree));    for(x=1;x<=row && y<=col;x++,y++)    {        max_r=x;        for(i=x+1;i<=row;i++)           if(a[i][y]>a[max_r][y])             max_r=i;        if(max_r!=x)          for(j=y;j<=col+1;j++)             swap(a[max_r][j],a[x][j]);        if(a[x][y]==0)        {            Free[++freenum]=y;            isfree[y]=1;            x--;            continue;        }        for(i=1;i<=row;i++)           if(i!=x && a[i][y]>0)             for(j=y;j<=col+1;j++)                a[i][j]^=a[x][j];    }    for(i=x;i<=row;i++)       if(a[i][y]>0)         return INF;    ans=INF;    for(S=0;S<(1<<freenum);S++)    {        memset(p,0,sizeof(p));        k=1;        ret=0;        S2=S;        while(S2)        {            if(S2&1)              p[Free[k]]=1;            S2/=2;            k++;        }        for(i=x-1;i>=1;i--)        {            y=1;            while(a[i][y]==0)              y++;            for(j=y+1;j<=col+1;j++)               if(a[i][j])                 a[i][17]^=p[j];            p[y]=a[i][17];        }        for(j=1;j<=16;j++)           ret+=p[j];        ans=min(ans,ret);    }    return ans;}int main(){    int i,j,k,ans;    for(i=1;i<=4;i++)       scanf("%s",s[i]+1);    init(1);    ans=gauss(16,16);    init(22);    ans=min(ans,gauss(16,16));    if(ans!=INF)      printf("%d\n",ans);    else      printf("Impossible\n");}



0 0