【题解】POJ-3279 Fliptile

Fliptile (POJ - 3279)

题面

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”.

输入

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

输出

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

样例输入

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

样例输出

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

提示

思路

开关问题,二进制枚举第一行开关情况

代码

查看代码
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
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 15+5;

int b[N][N] = {{0}}, c[N][N] = {{0}}, a[N][N] = {{0}};
int n, m, ans = inf;

int dx[]= { 1, -1, 0, 0, 0};
int dy[]= { 0, 0, 1, -1, 0};

int isb(int x, int y) {
int t = b[x][y];
for(int i=0; i<5; i++) {
int xi = x+dx[i];
int yi = y+dy[i];

if(xi<0 || xi>=n || yi<0 || yi>=m) {
continue;
}
t += c[xi][yi];
}
return t%2;
}

int dfs() {
for(int i=1; i<n; i++)
for(int j=0; j<m; j++)
if(isb(i-1,j))
c[i][j] = 1;

for(int i=0; i<m; i++)
if(isb(n-1,i))
return -1;

int res=0;
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
res += c[i][j];

return res;
}

int main(void) {
while(scanf("%d%d", &n, &m)==2) {

for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
scanf("%d", &b[i][j]);
}
}
for(int i=0; i<(1<<m); i++) {
memset(c, 0, sizeof(c));
for(int j=0; j<m; j++) {
c[0][m-j-1] = i>>j&1;
}
int res = dfs();
if(res>=0 && res<ans) {
ans = res;
memcpy(a, c, sizeof(c));
}
}
if(ans==inf) {
printf("IMPOSSIBLE\n");
} else {
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
}
}
return 0;
}
_/_/_/_/_/ EOF _/_/_/_/_/