题目链接:
题目分析:
先状压/\(dfs\)枚举第一排状态,然后在每个\(1\)下面翻,即确定了第一排就确定了后面的状态
最后验证一下最后一排是不是全0即可
代码:
#include<bits/stdc++.h>
#define N 50
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
return cnt * f;
}
int mapp[N][N], m, n, cnt, ans[N][N], res = (1 << 30);
bool vis[N][N], flag;
void rev(int x, int y) {
mapp[x][y] ^= 1;
mapp[x][y - 1] ^= 1;
mapp[x - 1][y] ^= 1;
mapp[x][y + 1] ^= 1;
mapp[x + 1][y] ^= 1;
}
void cpy() {
for (register int i = 1; i <= m; ++i)
for (register int j = 1; j <= n; ++j) ans[i][j] = vis[i][j];
}
void work() {
for (register int i = 2; i <= m; ++i)
for (register int j = 1; j <= n; ++j) if (mapp[i - 1][j]) rev(i, j), vis[i][j] = 1, ++cnt;
flag = 0;
for (register int i = 1; i <= m; ++i)
for (register int j = 1; j <= n; ++j) if (mapp[i][j]) flag = 1;
if (!flag) if (cnt < res) cpy(), res = cnt;
for (register int i = 2; i <= m; ++i)
for (register int j = 1; j <= n; ++j) if (vis[i][j]) rev(i, j), vis[i][j] = 0, --cnt;
}
void dfs_(int k) {
if (!k) {work(); return;}
dfs_(k - 1), rev(1, k), vis[1][k] = 1, ++cnt;
dfs_(k - 1), rev(1, k), vis[1][k] = 0, --cnt;
}
int main() {
m = read(), n = read();
for (register int i = 1; i <= m; ++i)
for (register int j = 1; j <= n; ++j) mapp[i][j] = read();
dfs_(n);
if (res == (1 << 30)) return printf("IMPOSSIBLE"), 0;
for (register int i = 1; i <= m; ++i) {
for (register int j = 1; j <= n; ++j) printf("%d ", ans[i][j]);
printf("\n");
}
return 0;
}#include<bits/stdc++.h>
#define N 50
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
return cnt * f;
}
int mapp[N][N], m, n, cnt, ans[N][N], res = (1 << 30);
bool vis[N][N], flag;
void rev(int x, int y) {
mapp[x][y] ^= 1;
mapp[x][y - 1] ^= 1;
mapp[x - 1][y] ^= 1;
mapp[x][y + 1] ^= 1;
mapp[x + 1][y] ^= 1;
}
void cpy() {
for (register int i = 1; i <= m; ++i)
for (register int j = 1; j <= n; ++j) ans[i][j] = vis[i][j];
}
void work() {
for (register int i = 2; i <= m; ++i)
for (register int j = 1; j <= n; ++j) if (mapp[i - 1][j]) rev(i, j), vis[i][j] = 1, ++cnt;
flag = 0;
for (register int i = 1; i <= m; ++i)
for (register int j = 1; j <= n; ++j) if (mapp[i][j]) flag = 1;
if (!flag) if (cnt < res) cpy(), res = cnt;
for (register int i = 2; i <= m; ++i)
for (register int j = 1; j <= n; ++j) if (vis[i][j]) rev(i, j), vis[i][j] = 0, --cnt;
}
void dfs_(int k) {
if (!k) {work(); return;}
dfs_(k - 1), rev(1, k), vis[1][k] = 1, ++cnt;
dfs_(k - 1), rev(1, k), vis[1][k] = 0, --cnt;
}
int main() {
m = read(), n = read();
for (register int i = 1; i <= m; ++i)
for (register int j = 1; j <= n; ++j) mapp[i][j] = read();
dfs_(n);
if (res == (1 << 30)) return printf("IMPOSSIBLE"), 0;
for (register int i = 1; i <= m; ++i) {
for (register int j = 1; j <= n; ++j) printf("%d ", ans[i][j]);
printf("\n");
}
return 0;
}
原文地址:https://www.cnblogs.com/kma093/p/11620134.html
时间: 2024-11-02 16:15:02