1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <stdlib.h> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 #include <string> 10 #include <iostream> 11 #include <ctype.h> 12 #include <string.h> 13 #include <set> 14 #include <stack> 15 #include<functional> 16 using namespace std; 17 #define Size 18 18 #define maxn 1<<30 19 #define minn 1e-6 20 int n; 21 int a[Size][Size]; 22 int press[Size][Size]; 23 int ans=maxn; 24 int isFini = 0; 25 bool guest(){ 26 int steps = 0; 27 for (int i = 1; i <= n; i++){ 28 if (press[1][i]) steps++; 29 } 30 if (steps == n) isFini = 1; 31 for (int i = 1; i <n; i++) 32 for (int j = 1; j <= n; j++){ 33 press[i + 1][j] = (press[i][j] + a[i][j] + press[i - 1][j] + press[i][j - 1] + press[i][j + 1])%2; 34 } 35 for (int i = 1; i <= n; i++){ 36 if (a[n][i] != (press[n][i] + press[n-1][i] + press[n][i - 1] + press[n][i + 1])%2) return false; 37 } 38 steps = 0; 39 for (int i = 1; i <= n; i++) 40 for (int j = 1; j <= n; j++){ 41 if (press[i][j]) steps++; 42 } 43 if (steps < ans) ans = steps; 44 return false; 45 } 46 void solve(){ 47 int c = 1; 48 while (guest()==false){ 49 if (isFini) break; 50 press[1][1]++; 51 c = 1; 52 while (press[1][c] > 1){ 53 press[1][c] = 0; 54 c++; 55 press[1][c]++; 56 } 57 } 58 if (ans == maxn) cout << "inf" << endl; 59 else cout << ans << endl; 60 } 61 int main(){ 62 cin >> n; 63 for (int i = 1; i <= n; i++) 64 for (int j = 1; j <= n; j++){ 65 char c; 66 cin >> c; 67 a[i][j] = c == 'w' ? 1 : 0; 68 } 69 solve(); 70 system("pause"); 71 }