现在的位置: 首页 > 算法 > 正文

poj1753-Flip Game(高斯消元枚举xor线性方程自由变元的值,找为1解的最少数量)

2019年11月06日 算法 ⁄ 共 2832字 ⁄ 字号 评论关闭
Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31227   Accepted: 13583

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

Northeastern Europe 2000
#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=100;
const int maxm=100;
const int inf=0xfffffff;
int a[maxn][maxm],x[maxm];
int gauss(int n)
{
	int row,line,i,j,m,free[maxm],cnt=0,t,mn,sum,k;
	for(row=line=0;line<n;row++,line++)
	{
		i=row;
		for(j=row+1;j<n;j++)
			if(a[j][line]>a[i][line])
				i=j;
		if(i!=row)
			for(j=row;j<=n;j++)
				swap(a[i][j],a[row][j]);
		if(a[row][line]==0)
		{
			free[cnt++]=line;
			row--;
			continue;
		}
		for(i=row+1;i<n;i++)
			if(a[i][line]==1)
				for(j=line;j<=n;j++)
					a[i][j]^=a[row][j];
	}
	for(i=row;i<n;i++)
		if(a[i][n]==1)
			return -1;
	mn=inf;
	m=1<<cnt;
	memset(x,0,sizeof(x));
	for(i=0;i<m;i++)
	{
		t=i;
		sum=0;
		for(j=0;j<cnt;j++)
		{
			x[free[j]]=t&1;
			if(x[free[j]]==1)
				sum++;
			t>>=1;
		}
		for(j=row-1;j>-1;j--)
		{
			x[j]=a[j][n];
			for(k=j+1;k<n;k++)
				x[j]^=a[j][k]&x[k];
			if(x[j]==1)
				sum++;
		}
		mn=min(sum,mn);
	}
	return mn;
}
void init(int n)
{
	int i,j,t;
	memset(a,0,sizeof(a));
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
		{
			t=i*n+j;
			a[t][t]=1;
			if(i!=0)
				a[(i-1)*n+j][t]=1;
			if(i!=n-1)
				a[(i+1)*n+j][t]=1;
			if(j!=0)
				a[t-1][t]=1;
			if(j!=n-1)
				a[t+1][t]=1;
		}
}
int main()
{
	int i,j,n=4,ans,m=16,t;
	char s[maxn][maxm];
	while(scanf("%s",s[0])!=EOF)
	{
		for(i=1;i<n;i++)
			scanf("%s",s[i]);
		init(n);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				if(s[i][j]=='b')
					a[i*n+j][m]=1;
		t=gauss(m);
		t=t==-1?inf:t;
		init(n);
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				if(s[i][j]=='w')
					a[i*n+j][m]=1;
		ans=gauss(m);
		ans=ans==-1?inf:ans;
		ans=min(ans,t);
		if(ans==inf)
			printf("Impossible\n");
		else
			cout<<ans<<endl;
	}
}

抱歉!评论已关闭.