Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
127040 廖悦扬 翻转棋盘2 C++ 通过 100 235 MS 264 KB 1274 2024-01-24 20:56:14

Tests(10/10):


#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; }


测评信息: