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<iostream>
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<<z<<endl;
	if(n==17)
	{
		int a,b,c;
		c=M[1][1];
		for(a=1;a<=4;a++)for(b=1;b<=4;b++)if(M[a][b]!=c)return;
		
		if(cnt==-1)cnt=num;
		else cnt=min(cnt,num);
		
		return;
	}
	int x=(n-1)/4+1; int y=(n-1)%4+1;
	//cout<<x<<" "<<y<<endl;
	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+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<<cnt;
    return 0;
}

高斯消元:
#include<iostream>
#include<cstdlib>
#include<cstdio>
#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<<K[a][b]<<" ";
		cout<<endl;
	}
	cout<<endl<<endl;*/
	x--;int ans=0;
	if(x==16)
	{
		for(a=16;a>=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));
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1753题目为"Flip Game",题目给出了一个4x4的棋盘,每个格子有黑色或白色,每次翻转一个格子会同时翻转它上下左右四个格子的颜色,目标是把整个棋盘都变为同一种颜色,求把棋盘变成同种颜色的最小步数。 解题思路: 一般关于棋盘变色的题目,可以考虑使用搜索来解决。对于POJ1753题目,可以使用广度优先搜索(BFS)来解决。 首先,对于每个格子,定义一个状态,0表示当前格子是白色,1表示当前格子是黑色。 然后,我们可以把棋盘抽象成一个长度为16的二进制数,将所有格子的状态按照从左往右,从上往下的顺序排列,就可以用一个16位的二进制数表示整个棋盘的状态。例如,一个棋盘状态为: 0101 1010 0101 1010 则按照从左往右,从上往下的顺序把所有格子的状态连接起来,即可得到该棋盘的状态为"0101101001011010"。 接着,我们可以使用队列来实现广度优先搜索。首先将初始状态加入队列中,然后对于队列中的每一个状态,我们都尝试将棋盘上的每个格子翻转一次,生成一个新状态,将新状态加入队列中。对于每一个新状态,我们也需要记录它是从哪个状态翻转得到的,以便在得到最终状态时能够输出路径。 在搜索过程中,我们需要维护每个状态离初始状态的步数,即将该状态转换为最终状态需要的最小步数。如果我们找到了最终状态,就可以输出答案,即最小步数。 代码实现:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值