标签:des http color io os ar for strong sp
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 31227 | Accepted: 13583 |
Description
Input
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; const int maxn=100; const int maxm=100; const int inf=0xfffffff; int a[maxn][maxm],x[maxm]; int gauss(int n) { int row,line,i,j,m,free[maxm],cnt=0,t,mn,sum,k; for(row=line=0;line<n;row++,line++) { i=row; for(j=row+1;j<n;j++) if(a[j][line]>a[i][line]) i=j; if(i!=row) for(j=row;j<=n;j++) swap(a[i][j],a[row][j]); if(a[row][line]==0) { free[cnt++]=line; row--; continue; } for(i=row+1;i<n;i++) if(a[i][line]==1) for(j=line;j<=n;j++) a[i][j]^=a[row][j]; } for(i=row;i<n;i++) if(a[i][n]==1) return -1; mn=inf; m=1<<cnt; memset(x,0,sizeof(x)); for(i=0;i<m;i++) { t=i; sum=0; for(j=0;j<cnt;j++) { x[free[j]]=t&1; if(x[free[j]]==1) sum++; t>>=1; } for(j=row-1;j>-1;j--) { x[j]=a[j][n]; for(k=j+1;k<n;k++) x[j]^=a[j][k]&x[k]; if(x[j]==1) sum++; } mn=min(sum,mn); } return mn; } void init(int n) { int i,j,t; memset(a,0,sizeof(a)); for(i=0;i<n;i++) for(j=0;j<n;j++) { t=i*n+j; a[t][t]=1; if(i!=0) a[(i-1)*n+j][t]=1; if(i!=n-1) a[(i+1)*n+j][t]=1; if(j!=0) a[t-1][t]=1; if(j!=n-1) a[t+1][t]=1; } } int main() { int i,j,n=4,ans,m=16,t; char s[maxn][maxm]; while(scanf("%s",s[0])!=EOF) { for(i=1;i<n;i++) scanf("%s",s[i]); init(n); for(i=0;i<n;i++) for(j=0;j<n;j++) if(s[i][j]=='b') a[i*n+j][m]=1; t=gauss(m); t=t==-1?inf:t; init(n); for(i=0;i<n;i++) for(j=0;j<n;j++) if(s[i][j]=='w') a[i*n+j][m]=1; ans=gauss(m); ans=ans==-1?inf:ans; ans=min(ans,t); if(ans==inf) printf("Impossible\n"); else cout<<ans<<endl; } }
poj1753-Flip Game(高斯消元枚举xor线性方程自由变元的值,找为1解的最少数量)
标签:des http color io os ar for strong sp
原文地址:http://blog.csdn.net/stl112514/article/details/39803963