POJ 1753 Flip Game (枚举或高斯消元)

Flip Game

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 48244

 

Accepted: 20555

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

1.暴力枚举

搜索就是一种比较万能的暴力枚举方式。由于这个题只要求4*4的矩阵。

那么也就是O(2^n*n)的复杂度。2^16不过三万多,秒过。

2.高斯消元法

完全可以把对每个点的操作设为x.每个点最终结果作为一个y.
然后就成为一个异或方程组问题!

16个方程16个未知量。高斯消元即可。

项数矩阵,对i有影响的灯为jA[i][j]=1;

系数矩阵,全为0或全为1分别解一次即可。

最终就是要解出xi,使得ak1*x1^ak2*x2...^akn*xn=bk;

正如加法,异或运算也是有任意的交换律的。所以可以用高斯消元的办法。

高斯消元后就可以知道自由元的个数了。要求的是最少的1,用dfs枚举自由元即可。


暴力枚举:
#include
using namespace std;
char o[10][10];
int M[10][10];
int cnt=-1; 
int z=0;
void dfs(int n,int num)
{
	//z++;
	//if(z>10000)cout<=1)M[x-1][y]^=1;
	if(y+1<=4)M[x][y+1]^=1;if(y-1>=1)M[x][y-1]^=1;
	dfs(n+1,num+1);
	M[x][y]^=1;if(x+1<=4)M[x+1][y]^=1;if(x-1>=1)M[x-1][y]^=1;
	if(y+1<=4)M[x][y+1]^=1;if(y-1>=1)M[x][y-1]^=1;	
	dfs(n+1,num);
}
int main()
{
	int a,b,c,d,e;
	for(a=0;a<=3;a++)scanf("%s",o[a]);
    for(a=1;a<=4;a++)
    {
	    for(b=1;b<=4;b++)
        {
    	   if(o[a-1][b-1]=='b')M[a][b]=1;
    	   else M[a][b]=0;
           //putchar(o[a-1][b-1]);
		}
		//putchar(10);
    }
    dfs(1,0);
    if(cnt==-1)puts("Impossible");
    else cout<

高斯消元:
#include
#include
#include
#define inf 999999999
using namespace std;
int K[20][20];char P[5][5];
int Gauss();
void start();
void pre(char t);
void swap(int x,int y);
int dfs(int x,int y,int num);
int main()
{
	start();
	return 0;
}
void start()
{
	int a,b,c,d;
	for(a=1;a<=4;a++)scanf("%s",P[a]);char k=P[1][0];
	for(a=1;a<=4;a++)
	for(b=1;b<=4;b++)
	{
		if(k!=P[a][b-1])k='t';
		c=(a-1)*4+b;
		K[c][c]=1;
		if(b!=4)K[c+1][c]=1;
		if(b!=1)K[c-1][c]=1;
		if(a!=1)K[c-4][c]=1;
		if(a!=4)K[c+4][c]=1;
	}
	if(k!='t'){printf("0");return;}
	d=inf;pre('w');d=min(d,Gauss());
	for(a=1;a<=16;a++)for(b=1;b<=17;b++)K[a][b]=0;
	for(a=1;a<=4;a++)
	for(b=1;b<=4;b++)
	{
		c=(a-1)*4+b;
		K[c][c]=1;
		if(b!=4)K[c+1][c]=1;
		if(b!=1)K[c-1][c]=1;
		if(a!=1)K[c-4][c]=1;
		if(a!=4)K[c+4][c]=1;
	}
    pre('b');d=min(d,Gauss());
    if(d==inf)printf("Impossible");
    else printf("%d",d);return;
}
void pre(char t)
{
	int a,b,c;
	for(a=1;a<=4;a++)
	for(b=1;b<=4;b++)
	{
		c=(a-1)*4+b;
		if(P[a][b-1]==t)K[c][17]=1;
	}
	
}
int Gauss()
{
	int a,b,c,d;int x,y;	

	for(x=1,y=1;x<=16&&y<=16;x++,y++)
	{
		if(K[x][y]==0)
		{
			for(a=x+1;a<=16;a++)if(K[a][y]==1)break;
			if(a!=17)swap(x,a);
			else {x--;continue;}
		}
		for(a=x+1;a<=16;a++)if(K[a][y]!=0)for(b=y;b<=17;b++)K[a][b]^=K[x][b];
	}
	/*for(a=1;a<=16;a++)
	{
		for(b=1;b<=17;b++)cout<=1;a--)
		{
			if(K[a][17]!=0)ans++;
			for(b=a-1;b>=1;b--)K[b][17]^=K[b][a]*K[a][17];
		}
		return ans;
	}
	else
	{
		for(a=x+1;a<=16;a++){if(K[a][17]!=0)return inf;}
		return dfs(16,16-x,0);
	}
}
void swap(int x,int y)
{
	int temp[20],a;
	for(a=1;a<=17;a++)temp[a]=K[x][a];
	for(a=1;a<=17;a++)K[x][a]=K[y][a];
	for(a=1;a<=17;a++)K[y][a]=temp[a];
	return;
}
int dfs(int x,int y,int num)
{
	int a;
	if(x==1)
	{
		if(K[x][17]==1)return num+1;
		else return num;
	}
	if(y==0)
	{
		if(K[x][17]==1)
		{
			for(a=x-1;a>=1;a--)K[a][17]^=K[a][x];
			int t=dfs(x-1,y,num+1);
			for(a=x-1;a>=1;a--)K[a][17]^=K[a][x];
			return t;
		}
		else return dfs(x-1,y,num);
	}
	else
	{
		for(a=x-1;a>=1;a--)K[a][17]^=K[a][x];
		int t=dfs(x-1,y-1,num+1);
		for(a=x-1;a>=1;a--)K[a][17]^=K[a][x];
		return min(t,dfs(x-1,y-1,num));
	}
}


 

你可能感兴趣的:(基本算法)