题解
2024-01-26 19:28:52
发布于:浙江
59阅读
0回复
0点赞
造福后代
#include <iostream>
using namespace std;
int ans = 10;
bool g[5][5],vis[5][5];
void turn(int x, int y) {
g[x][y] = !g[x][y];
g[x - 1][y] = !g[x - 1][y];
g[x + 1][y] = !g[x + 1][y];
g[x][y + 1] = !g[x][y + 1];
g[x][y - 1] = !g[x][y - 1];
}
void dfs(int n) {
if (n > ans) return ;
int sum = 0;
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
sum += g[i][j];
if (sum == 9) ans = min(ans, n - 1);
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++) {
if (!vis[i][j]) {
vis[i][j] = 1;
turn(i, j);
dfs(n + 1);
vis[i][j] = 0;
turn(i, j);
}
}
}
int main() {
for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) cin >> g[i][j];
dfs(1);
cout << ans << endl;
return 0;
}
这里空空如也
有帮助,赞一个