#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int m, n, a[20][20], b[20][20], c[20][20], ans[20][20];
void fff(int x, int y) {
b[x][y] = !b[x][y];
if (x - 1 >= 0) {
b[x - 1][y] = !b[x - 1][y];
}
if (x + 1 < m) {
b[x + 1][y] = !b[x + 1][y];
}
if (y - 1 >= 0) {
b[x][y - 1] = !b[x][y - 1];
}
if (y + 1 < n) {
b[x][y + 1] = !b[x][y + 1];
}
}
int main()
{
while (scanf("%d %d", &m, &n) == 2) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
int minf = 1000;
for (int kase = 0; kase < (1<<n); kase++) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
b[i][j] = a[i][j];
}
}
memset(c, 0, sizeof(c));
int cnt = 0;
for (int i = 0; i < n; i++) {
c[0][i] = (1 & (kase>>(n - i - 1)));
if (c[0][i]) {
fff(0, i);
cnt++;
}
}
for (int i = 1; i < m; i++) {
for (int j = 0; j < n; j++) {
if (b[i - 1][j]) {
c[i][j] = 1;
fff(i, j);
cnt++;
}
}
}
bool ok = true;
for (int i = 0; i < n; i++) {
if (b[m - 1][i]) {
ok = false;
break;
}
}
if (ok && cnt < minf) {
minf = cnt;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
ans[i][j] = c[i][j];
}
}
}
}
if (minf == 1000) {
printf("IMPOSSIBLE\n");
}
else {
for (int i = 0; i < m; i++) {
printf("%d", ans[i][0]);
for (int j = 1; j < n; j++) {
printf(" %d", ans[i][j]);
}
printf("\n");
}
}
}
return 0;
}
POJ 3279 Fliptile——开关问题
原创
©著作权归作者所有:来自51CTO博客作者软糖酱八号机的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
有趣的css - 好看的开关按钮
用css模拟实现一个好看的开关按钮动效。
css 开关按钮 动效设计 交互设计 ux/ui -
POJ3279 Fliptile —— 状态压缩 + 模拟
题目链接:http://poj.org/problem?id=3279 Fliptile Time Limit: 2
#include i++ 枚举法 ide ico