题解:
乱搞
令x[i][j]表示(i,j)是否操作,a[i][j]表示状态
先假设都翻到0
则x[i][1]^x[i][2]^^^x[i][n]^x[1][j]^x[2][j]^^^x[n][j]^x[i][j]=a[i][j]
令d[i][j]=x[i][1]^x[i][2]^^^x[i][n]^x[1][j]^x[2][j]^^^x[n][j]^x[i][j]
则d[i][1]^d[i][2]^^^d[i][n]^d[1][j]^d[2][j]^^^d[n][j]^d[i][j]=x[i][j]
然后可以求出x[i][j]
根据式子发现翻转成0和1的步数和=n^2
然后取min
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn=1009; int n; int a[maxn][maxn]; int r[maxn],l[maxn]; char ss[maxn]; int cnt=0; int main(){ scanf("%d",&n); for(int i=1;i<=n;++i){ scanf("%s",ss+1); for(int j=1;j<=n;++j)a[i][j]=ss[j]-‘0‘; } for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j)r[i]^=a[i][j]; } for(int j=1;j<=n;++j){ for(int i=1;i<=n;++i)l[j]^=a[i][j]; } for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ int tm=r[i]^l[j]^a[i][j]; // if(!tm)cout<<i<<‘ ‘<<j<<endl; cnt+=tm; } } cout<<min(cnt,n*n-cnt)<<endl; return 0; }