#include<iostream> #include<string> using namespace std; int a[20][20];int b[20][20]; #define rep(i,j,k) for(int i=j;i<=k;i++) int n; string s[20]; void update(int i,int j) { b[i][j]=1-b[i][j]; b[i-1][j]=1-b[i-1][j]; b[i+1][j]=1-b[i+1][j]; b[i][j-1]=1-b[i][j-1]; b[i][j+1]=1-b[i][j+1]; } int judge(int x) { rep(i,1,n) rep(j,1,n) b[i][j]=a[i][j]; int sum=0; rep(i,1,n) { if(x%2) { update(1,i); sum++; } x/=2; } rep(i,1,n-1) rep(j,1,n) { if(b[i][j]==0) { update(i+1,j); sum++; } } rep(i,1,n) if(b[n][i]==0) return 300; return sum; } int main() { cin>>n; rep(i,1,n) { string tmp; cin>>tmp; s[i]=" "+tmp; } rep(i,1,n) { rep(j,1,n) if(s[i][j]==‘w‘) a[i][j]=0; else a[i][j]=1; } int maxnum=300; rep(i,0,(1<<n)-1) { maxnum=min(maxnum,judge(i)); } if(maxnum==300) cout<<"inf"; else cout<<maxnum; }
时间: 2024-10-23 08:58:07