nefu504new Flip Game+高斯消元
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=504
题面描述:
new Flip Game
Problem:504
Time Limit:1000ms
Memory Limit:65536K
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 left_top(左上), 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
It have t cases,begin input t,means cases.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
1 bwwb bbwb bwwb bwww
Sample Output
7
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[16][17],b[16][17],c[16][17]; //系数矩阵int ans[16];
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 c1[20];
int t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
//ans=1000000000;
for (int i=0; i<4; i++)
{
cin>>c1;
//构造系数矩阵A
for (int j=0; j<4; j++)
{
k = 4*i+j;
a[k][k]=1;
if (i>0) a[k-4][k]=1; //上
if (i<3) a[k+4][k]=1; //下
if (j>0) a[k-1][k]=1; //左
if (j<3) a[k+1][k]=1; //右
if (i>0&& j>0) a[k-5][k]=1; //左上
if (c1[j]=='b')
a[k][16] = 1;
}
}
for(int i=0; i<16; i++)
for(int j=0; j<=16; j++)
{
c[i][j]=a[i][j];
// cout<<a[i][j];
// if (j==16) cout<<endl;
}
for(int k=0; k<16; k++)
c[k][16]^=1;
int j1=gauss();
int kkk1=0;
if(j1==0)
{
kkk1=0;
for(int i=0; i<16; i++)
{
if(a[i][16]==1)
{
kkk1++;
}
}
}
//cout<<j1<<endl;
#ifdef debug
for(int i=0; i<16; i++)
{
for(int j=0; j<=16; j++)
cout<<a[i][j];
cout<<endl;
}
#endif // debug
//cout << "Hello world!" << endl;
for(int i=0; i<16; i++)
for(int j=0; j<=16; j++)
a[i][j]=c[i][j];
//ans=100000000;
int j2=gauss();
int kkk2=0;
if(j2==0)
{
kkk2=0;
for(int i=0; i<16; i++)
{
if(a[i][16]==1)
{
kkk2++;
}
}
}
//int min2=ans;
if (j1==-1&&j2==-1) cout<<"Impossible"<<endl;
else if(j1==0 || j2==0)
{
cout<<min(kkk1,kkk2)<<endl;
}
else
cout<<min(j2,j1)<<endl;
}
}
return 0;
}