Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
128606 林扬泉 翻转棋盘2 C++ 解答错误 20 0 MS 252 KB 1583 2024-01-25 21:26:23

Tests(2/10):


//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstring> #include<string> using namespace std; const int MAXN = 10; const int INF = 0x3f3f3f3f; int n; int MAP[MAXN][MAXN], a[MAXN][MAXN]; void Flip(int x, int y)//翻转操作 { a[x][y] = !a[x][y]; a[x-1][y] = !a[x-1][y]; a[x+1][y] = !a[x+1][y]; a[x][y-1] = !a[x][y-1]; a[x][y+1] = !a[x][y+1]; } bool judge(int op)//最后一行是否全为op { for (int j = 1; j <= n; j++) { if (a[n][j] != op) return false; } return true; } int work(int op)//全变为op { int cnt = 0, ans = INF; for (int state = 0; state < (1<<n); state++)//模拟第一行的所有方法 { cnt = 0; memcpy(a, MAP, sizeof(MAP)); for (int j = 0; j < n; j++)// 第一行进行操作,二进制枚举。 { if (state&(1<<j)) cnt++, Flip(1, j + 1); } for (int i = 2; i <= n; i++) { for (int j = 1; j <= n; j++) { if (a[i-1][j] != op) cnt++, Flip(i, j); } } if (judge(op)) ans = min(ans, cnt); } return ans; } int main() { n = 4; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { char c; cin >> c; if (c == 'w') MAP[i][j] = 0; if (c == 'b') MAP[i][j] = 1; } } int ans = min(work(0), work(1)); if (ans != INF) cout << ans << endl; else cout << "Impossible" << endl; return 0; }


测评信息: