提交时间:2024-01-24 20:56:14

运行 ID: 127040

#include <bits/stdc++.h> using namespace std; char a[20][20]; int n, tmp[20][20]; void to_tmp() { for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) tmp[i][j] = a[i][j]!='w'; } void change(int x, int y) { tmp[x][y] ^= 1; if (x > 1) tmp[x-1][y] ^= 1; if (x < n) tmp[x+1][y] ^= 1; if (y > 1) tmp[x][y-1] ^= 1; if (y < n) tmp[x][y+1] ^= 1; } int check(int row, int c) { int tot=0; for (int i=1; i<=n; i++) { if (row & (1<<i-1)) { change(1, i); tot++; } } for (int i=1; i<n; i++) { for (int j=1; j<=n; j++) { if (tmp[i][j] != c) { change(i+1, j); tot++; } } } for (int i=1; i<=n; i++) { if (tmp[n][i] != c) tot = INT_MAX; } return tot; } signed main() { int ans = INT_MAX; scanf("%d", &n); for (int i=1; i<=n; i++) scanf("%s", a[i]+1); for (int i=0; i<(1<<n); i++) { to_tmp(); ans = min(ans, check(i, 0)); } for (int i=0; i<(1<<n); i++) { to_tmp(); ans = min(ans, check(i, 1)); } if ( ans < INT_MAX) printf("%d", ans); else printf("Impossible"); return 0; }