Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:求高斯消元正解…………

Posted by teoy at 2011-11-08 23:07:47 on Problem 1753
In Reply To:Re:求高斯消元正解………… Posted by:qiqijianglu at 2011-10-06 12:41:56
> 同求

发下我的代码吧,写的太二了……

//异或高斯消元最简单版本需要枚举自由变元确定最优解
//还没完成枚举自由变元的部分

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
#define maxn 20
int a[maxn][maxn];
int b[maxn][maxn];
int n;
int pre[maxn];
int post[maxn];
int x[maxn];
char map[maxn][maxn];
bool x_free[maxn];
void Debug()
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n+1;j++)
		{
			printf("%d ",a[i][j]);
		}
		printf("\n");
	}
}
int Gauss(int a[maxn][maxn])
{
	int max_r,i,col;
	int free_index,free_x_num;
	vector<int> v;
	for(i=0,col=0;i<n&&col<n;i++,col++)
	{
		max_r=i;
		for(int j=i+1;j<n;j++)
		{
			if(a[j][col]>a[max_r][col])
			{
				max_r=j;
			}
		}
		if(max_r!=i)
		{
			for(int j=0;j<=n;j++)
			{
				swap(a[i][j],a[max_r][j]);
			}
		}
		if(a[i][col]==0)
		{
			v.push_back(col);
			i--;continue;
		}
		for(int j=i+1;j<n;j++)
		{
			if(a[j][col]!=0)
			{
				for(int k=col;k<n+1;k++)
				{
					a[j][k]=a[j][k]^a[i][k];
				}
			}
		}
	}
	for(int k=i;k<n;k++)
	{
		if(a[k][n]) return 17;
	}	
	int ans=n*n;
	for(int xx=0;xx<(1<<v.size());xx++)
	{
		int t=xx;
		memset(x_free,0,sizeof(x_free));
		for(int j=0;j<v.size();j++)
		{
			x_free[v[j]]=1;
		}
		for(int k=0;k<v.size();k++)
		{
			x[v[k]]=t%2;
			t/=2;
		}	
		for(int j=i-1;j>=0;j--)
		{
			free_x_num=0;
			for(int k=0;k<n;k++)
			{
				if(a[j][k]!=0&&!x_free[k]) free_x_num++,free_index=k;
			}
			if(free_x_num>1||free_x_num==0) {printf("fuck\n");continue;}
			int temp=a[j][n];
			for(int k=0;k<n;k++)
			{
				if(a[j][k]!=0&&k!=free_index) temp^=x[k];
			}
			x[free_index]=temp;
			x_free[free_index]=true;
		}
		int s=0;
		for(int j=0;j<n;j++)
		{
			s+=x[j];
			//printf("%d ",x[j]);
		}
		//printf("\n");
		ans=min(s,ans);
	}	
	return ans;
}
int dx[4]={0,0,-1,1};
int dy[4]={1,-1,0,0};
int main(void)
{
	n=4*4;
	for(int i=0;i<4;i++)
	{
		scanf("%s",map[i]);
	}
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			a[i*4+j][n]=map[i][j]=='b'?0:1;
		}
	}
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			a[i*4+j][i*4+j]=1;
			for(int k=0;k<4;k++)
			{
				int x=i+dx[k];
				int y=j+dy[k];
				if(x>=0&&x<4&&y>=0&&y<4)
				{
					a[x*4+y][i*4+j]=1;
				}
			}
		}
	}
	//Debug();
	memset(x_free,0,sizeof(x_free));
	memset(x,-1,sizeof(x));	
	for(int i=0;i<16;i++)
	{
		for(int j=0;j<=16;j++)
		{
			b[i][j]=a[i][j];
		}
	}
	int ans=Gauss(a);
	for(int i=0;i<16;i++)
	{
		b[i][n]=b[i][n]^1;
	}
	memset(x_free,0,sizeof(x_free));
	memset(x,-1,sizeof(x));		
	int tmp=Gauss(b);
	ans=min(ans,tmp);
	if(ans==17)printf("Impossible\n");
	else printf("%d\n",ans);
	//Debug();
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator