【POJ1681】Painter's Problem 高斯消元,求最小∑系数的异或方程组

#include <stdio.h>
int main()
{
	puts("转载请注明出处[vmurder]谢谢");
	puts("网址:blog.csdn.net/vmurder/article/details/43483547");
}

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

题意:

多组数据、

有个n*n的正方形,然后你要对某些位置进行操作,使得最后灯的状态都变成y。

操作:这个灯位置的上下左右以及自己这五盏灯状态都取反。

然后求最小操作次数。

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

先说我自己WA了的原因:

给正方形上的点id[i][j]=++cnt赋hash,然后没清id,WA了几遍~

还自己拍极限(永远n=15),还一直过。

嗯,反正分析我代码就懂了。(ctrl+F搜索"id[")

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

然后说正经题解:

一些自由元乱取0/1 AC的都是因为数据弱,不要乱提。

首先每个位置都可以选择“操作”或者“不操作”

然后对于自由元我们可以随意选择它开还是不开,而非自由元我们则需要回代一遍出解。

然后深搜一遍就好了~

这里我们不能给所有自由元都取“不操作”,因为这样看起来少了好多次操作数,

但是实际上,这些自由元是参与非自由元求值的回代的,

所以一个自由元的不作为可能导致很多个非自由元的悲剧。

-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 300
#define inf 0x3f3f3f3f
using namespace std;
const int dx[]={0,0,0,1,-1};
const int dy[]={0,1,-1,0,0};

bool a[N][N],x[N];
int crs[N],n,ans;

void dfs(int id,int which,int now)
{
	if(id<1)
	{
		ans=min(ans,now);
		return ;
	}
	if(now>=ans)return ;

	if(crs[id]==which)
	{
		bool ret=a[id][n+1];
		for(int i=which+1;i<=n;i++)ret^=(a[id][i]*x[i]);
		x[which]=ret;
		dfs(id-1,which-1,now+ret);
	}
	else {
		x[which]=true;
		dfs(id,which-1,now+1);
		x[which]=false;
		dfs(id,which-1,now);
	}
	return ;
}

int Gauss(int n,int m)
{
	int i,j,k,id;
	for(id=i=1;i<n;i++,id++)
	{
		for(j=id;j<=m&&!a[j][i];j++);
		if(j>m){id--;continue;}

		crs[id]=i;
		if(id!=j)for(k=i;k<=n;k++)swap(a[id][k],a[j][k]);
		for(j=id+1;j<=m;j++)if(a[j][i])for(k=i;k<=n;k++)
			a[j][k]^=a[id][k];
	}
	for(i=id;i<=m;i++)if(a[i][n])return -1;
	return id-1;
}

int id[N][N],cnt;
char src[N];

void init()
{
	cnt=0,ans=inf;
	memset(a,0,sizeof a);
	memset(x,0,sizeof x);
	memset(id,0,sizeof id);
	memset(crs,0,sizeof crs);
}

int main()
{
	freopen("test.in","r",stdin);

	int i,j,k,g,t;
	for(scanf("%d",&g);g--;)
	{
		init();

		scanf("%d",&n);
		for(i=1;i<=n;i++)for(j=1;j<=n;j++)id[i][j]=++cnt;
		for(i=1;i<=n;i++)
		{
			scanf("%s",src+1);
			for(j=1;j<=n;j++)if(src[j]=='w')
				a[id[i][j]][cnt+1]=true;
		}
		for(i=1;i<=n;i++)for(j=1;j<=n;j++)for(k=0;k<=4;k++)
			if(t=id[i+dx[k]][j+dy[k]])
				a[id[i][j]][t]=true;
		n*=n;
		t=Gauss(n+1,n);
		if(t==-1)puts("inf");
		else {
			dfs(t,n,0);
			printf("%d\n",ans);
		}

	}
	return 0;
}

【POJ1681】Painter's Problem 高斯消元,求最小∑系数的异或方程组

时间: 2024-08-08 09:48:19

【POJ1681】Painter's Problem 高斯消元,求最小∑系数的异或方程组的相关文章

POJ 1681 Painter&#39;s Problem (高斯消元)

题目地址:POJ 1681 跟前两题几乎一模一样的...不多说了.高斯消元+自由元枚举. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include &

poj 1681 Painter&amp;#39;s Problem(高斯消元)

http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. 注意依据自由变元求其它解及求最优值的方法. #include <stdio.h> #include <algorithm> #include <set> #include <map> #include <vector> #include <math.h> #include

hdu 2262 高斯消元求期望

Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1070    Accepted Submission(s): 298 Problem Description After a long drastic struggle with himself, LL decide to go for some

hdu 3992 AC自动机上的高斯消元求期望

Crazy Typewriter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 391    Accepted Submission(s): 109 Problem Description There was a crazy typewriter before. When the writer is not very sober, it

hdu 4418 高斯消元求期望

Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1480    Accepted Submission(s): 327 Problem Description Agent K is one of the greatest agents in a secret organization called Men in B

uva 10828 高斯消元求数学期望

Back to Kernighan-RitchieInput: Standard Input Output: Standard Output You must have heard the name of Kernighan and Ritchie, the authors of The C Programming Language. While coding in C, we use different control statements and loops, such as, if-the

HDU4870_Rating_双号从零单排_高斯消元求期望

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 654    Accepted Submission(s): 415 Special Judge Problem Description A little gir

poj1681--Painter&#39;s Problem(高斯消元问题4)

Painter's Problem Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description There is a square wall which is made of n*n small square bricks. Some bricks are white while some bricks are yellow. Bob is a pai

高斯消元 求整数解模版

#include <iostream> #include <string.h> #include <cmath> using namespace std; const int maxn = 105; int equ, var; // 有equ个方程,var个变元.增广阵行数为equ, 分别为0到equ - 1,列数为var + 1,分别为0到var. int a[maxn][maxn]; int x[maxn]; // 解集. bool free_x[maxn]; //