# GitBook

```Fliptile

Time Limit: 2000MS
Memory Limit: 65536K

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
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Sample Output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

(如果有00111和11100则输出00111)

0000(0) 表示 否否否否

0001(1) 表示 否否否是

0010(2) 表示 否否是否

0011(3) 表示 否否是是

0^1==1

1^1==0

0^0==0

1^0==1

#include<stdio.h>
#include<string.h>
int map[20][20];
int temp[20][20];
int N,M;
void Pr(int x) {
for(int i=0; i<M; i++) {
map[0][i] ^= (x>>(M-1-i))&1;
if((x>>(M-1-i))&1) {
if(i>0)
map[0][i-1] ^= 1;
if(i<M-1)
map[0][i+1] ^= 1;
if(N>1)
map[1][i] ^= 1;
}
printf("%d",(x>>(M-1-i))&1);
printf("%c",i==M-1?'\n':' ');
}
for(int i=1; i<N; i++) {
for(int j=0; j<M; j++) {
printf("%d",map[i-1][j]);
if(map[i-1][j]==1) {
map[i-1][j] ^= 1;
map[i][j] ^= 1;
if(j>0)
map[i][j-1] ^= 1;
if(j<M-1)
map[i][j+1] ^= 1;
if(i<N-1)
map[i+1][j] ^= 1;
}
printf("%c",j==M-1?'\n':' ');
}
}
}
int main() {
while(scanf("%d %d",&N,&M)!=EOF) {
for(int i=0; i<N; i++)
for(int j=0; j<M; j++)
scanf("%d",&map[i][j]);
int res=99999999,rest=-1;
int T=(1<<M),max=T-1;
while(T--) {
memcpy(temp,map,sizeof(map));
int t=T^max;
int cnt=0;
for(int i=0; i<M; i++) {
temp[0][i] ^= (t>>(M-1-i))&1;
if((t>>(M-1-i))&1) {
cnt++;
if(i>0)
temp[0][i-1] ^= 1;
if(i<M-1)
temp[0][i+1] ^= 1;
if(N>1)
temp[1][i] ^= 1;
}
}
for(int i=1; i<N; i++) {
for(int j=0; j<M; j++) {
if(temp[i-1][j]==1) {
cnt++;
temp[i-1][j] ^= 1;
temp[i][j] ^= 1;
if(j>0)
temp[i][j-1] ^= 1;
if(j<M-1)
temp[i][j+1] ^= 1;
if(i<N-1)
temp[i+1][j] ^= 1;
}
}
}
int i;
for(i=0; i<M; i++)
if(temp[N-1][i]==1)
break;
if(i==M&&cnt<res)
res=cnt,rest=t;
}
if(res==99999999)
printf("IMPOSSIBLE\n");
else
Pr(rest);
}
return 0;
}