Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
123971 廖悦扬 翻转棋盘 C++ 通过 100 2 MS 252 KB 1301 2024-01-23 10:33:37

Tests(25/25):


#include <bits/stdc++.h> using namespace std; char chess[4][4], now[] = {'w', 'b'}; int ans = INT_MAX; char rev(char c) { if(c==now[0]) return now[1]; return now[0]; } char reverses(int x, int y) { return rev(chess[x][y]); } bool in(int x, int y) { return x >= 0 && x <= 3 && y >= 0 && y <= 3; } bool isOk() { for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { if (chess[i][j] != chess[0][0]) return false; } } return true; } void reverse_this(int x, int y) { chess[x][y] = reverses(x, y); if (in(x-1, y)) chess[x-1][y] = reverses(x-1, y); if (in(x+1, y)) chess[x+1][y] = reverses(x+1, y); if (in(x, y-1)) chess[x][y-1] = reverses(x, y-1); if (in(x, y+1)) chess[x][y+1] = reverses(x, y+1); } void dfs(int x, int y, int dep) { if (isOk()) { ans = min(ans, dep); return; } if (!in(x, y)) return; int new_y = y + (x+1)/4, new_x = (x+1)%4; dfs(new_x, new_y, dep); reverse_this(x, y); dfs(new_x, new_y, dep+1); reverse_this(x, y); } signed main() { for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { scanf("%c", &chess[i][j]); if (chess[i][j] != 'b' && chess[i][j] != 'w') { j--; } } } dfs(0, 0, 0); if (ans == INT_MAX) printf("Impossible"); else printf("%d", ans); return 0; }


测评信息: