您的位置:首页 > 大数据 > 人工智能

[poj1681][高斯消元]Painter's Problem

2018-02-28 14:33 288 查看
传送门http://poj.org/problem?id=1681

sol:

和上一题差不多,解一个异或方程组。就是这题会出现无解和自由元的情况。自由元的话我们肯定都设为0。无解的情况就是某一行的未知数前面的系数为0,而常量!=0.其他正常解异或方程即可。

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long ll;
typedef double s64;
int n;
inline int read()
{
char c;
int res,flag=0;
while((c=getchar())>'9'||c<'0') if(c=='-')flag=1;
res=c-'0';
while((c=getchar())>='0'&&c<='9') res=(res<<3)+(res<<1)+c-'0';
return flag?-res:res;
}
const int N=230;
char sr

;
int c

,b

,tot,ans;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
inline void guass()
{
int n,m;
n=tot;
m=tot+1;
for(int i=1;i<=n;++i)
{
int row;
for(row=i;row<=n;++row) if(c[row][i]) break;
if(row>n) continue;
if(row!=i)
for(int j=1;j<=m;++j) swap(c[row][j],c[i][j]);
for(int j=1;j<=n;++j)
if(j!=i&&c[j][i])
for(int k=1;k<=m;++k)
c[j][k]^=c[i][k];
}
ans=0;
for(int i=1;i<=n;++i)
if(c[i][m])
{
ans+=c[i][m];
if(!c[i][i])
{
ans=-1;
return;
}
}
}
inline void solve()
{
n=read();
memset(c,0,sizeof(c));
for(int i=1;i<=n;++i)
scanf("%s",sr[i]+1);
tot=0;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
b[i][j]=++tot;
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
if(sr[i][j]=='w') c[b[i][j]][tot+1]=1;

int x,y;
c[b[i][j]][b[i][j]]=1;
for(int k=0;k<=3;++k)
{
x=i+dx[k];
y=j+dy[k];
if(1<=x&&x<=n&&1<=y&&y<=n)
c[b[i][j]][b[x][y]]=1;
}
}
guass();
if(ans==-1) printf("inf\n");
else printf("%d\n",ans);
}
int main()
{
//  freopen("1681.in","r",stdin);
//  freopen(".out","w",stdout);
int T=read();
while(T--) solve();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: