Poj3279 Fliptile

Description

Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word “IMPOSSIBLE”.

Input

Line 1: Two space-separated integers: M and N Lines 2..M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white

Output

Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

Sample Input

1
2
3
4
5
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

Sample Output

1
2
3
4
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

Analysis

题意:一个m*n的方格,每格为1或0,每次可对一格进行翻转(0变为1或1变为0),则该格和其上下左右4格都会被翻转。问多少次翻转可以把所有格子全部变为0,如果无法全部变为0就输出”IMPOSSILBE”。

采用二进制搜索,假设有n列,先考虑第一行的情况,每格都有0或者1两种状态,那么n列就是2^n种状态,再考虑剩下的m-1行,每一行的翻转都必须使它的上一行全为0,这样才能进行下一行的翻转。于是,第一行的情况就确定了剩下所有行的情况。输出所有情况下翻转次数最少的那种情况,如果没有情况满足则输出IMPOSSIBLE。

用二进制形式来对应翻转的位置,二进制位为1则该位翻转,二进制位为0则该位不翻转,每次循环二进制右移一位以此进行下一位的操作。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstring>
using namespace std;
int a[20][20];//存储输入的方格
int b[20][20];//输入方格的拷贝,方便进行翻转
int f[20][20];//存储翻转完成后的方格
int ans[20][20];//存储应当输出的最小次数的方格
int anum, fnum;//最小翻转次数,每种情况的翻转次数
int m, n;
void ex(int x, int y)//对方格b,x行y列进行一次翻转
{
fnum++;
f[x][y] = 1;
b[x][y] = 1 - b[x][y];
b[x - 1][y] = 1 - b[x - 1][y];
b[x + 1][y] = 1 - b[x + 1][y];
b[x][y - 1] = 1 - b[x][y - 1];
b[x][y + 1] = 1 - b[x][y + 1];
}
int main()
{
cin >> m >> n;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
int t = 1 << n;//即2^n种情况
anum = 10000;
for (int loop = 0; loop < t; loop++)
{
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
b[i][j] = a[i][j];
}
}
fnum = 0;
int moven = loop;
memset(f, 0, sizeof(f));
for (int j = 1; j <= n; j++)//翻转第一行
{
if (moven & 1)
{
ex(1, j);
}
moven = moven >> 1;
}
for (int i = 2; i <= m; i++)//翻转剩下的行
{
for (int j = 1; j <= n; j++)
{
if (b[i - 1][j])
ex(i, j);
}
}
int j;
for (j = 1; j <= n; j++)//最后一行有1,不满足条件
{
if (b[m][j])
break;
}
if (j == n + 1 && fnum < anum)//每次循环更新次数最小情况
{
anum = fnum;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
ans[i][j] = f[i][j];
}
}
}
}
if (anum == 10000)
{
cout << "IMPOSSIBLE" << endl;
}
else
{
for (int i = 1; i <= m; i++)
{
cout << ans[i][1];
for (int j = 2; j <= n; j++)
{
cout << " " << ans[i][j];
}
cout << endl;
}
}
return 0;
}