题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3503
这种写法很慢。。。
每个点建立一个xor方程,分别对它上下左右还有自己的系数都为1其他都为0,然后自由元就都设为1。
#include<cstring> #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #define rep(i,l,r) for (int i=l;i<=r;i++) #define down(i,l,r) for (int i=l;i>=r;i--) #define clr(x,y) memset(x,y,sizeof(x)) #define maxn 2000 using namespace std; int n,m,a[maxn][maxn],ans[maxn]; char ch; int dx[5]={0,0,1,0,-1},dy[5]={0,1,0,-1,0}; int read(){ int x=0,f=1; char ch=getchar(); while (!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();} while (isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();} return x*f; } bool jud(int x,int y){ if (x<1||x>n||y<1||y>m) return 0; return 1; } int p(int x,int y){ return (x-1)*m+y; } void gs(int n){ rep(i,1,n){ int j=i; while (!a[j][i]&&j<=n) j++; if (j>n) continue; if (i!=j) rep(k,1,n+1) swap(a[i][k],a[j][k]); rep(j,i+1,n) if (j!=i&&a[j][i]) rep(k,1,n+1) a[j][k]^=a[i][k]; } down(i,n,1){ if (!a[i][i]) {ans[i]=1; continue;} int ret=a[i][n+1]; down(j,n,i+1) if (a[i][j]) ret^=ans[j]; ans[i]=ret; } } int main(){ // freopen("in.txt","r",stdin); n=read(); m=read(); rep(i,1,n) rep(j,1,m) rep(k,0,4){ int x=i+dx[k],y=j+dy[k]; if (jud(x,y)) a[p(i,j)][p(x,y)]=1; } gs(n*m); int cnt=0; rep(i,1,n) { rep(j,1,m) printf("%d ",ans[++cnt]); puts(""); } return 0; }