# POJ - 3279（枚举+暴力）

Fliptile
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14297 Accepted: 5257

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

Source

  1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4
5 using namespace std;
6
7 int m,n;
8
9 bool right(int i,int j){
10     if(i>=0&&i<m&&j>=0&&j<n){
11         return true;
12     }else{
13         return false;
14     }
15 }
16
17 void flip(char grid[][16],int i,int j){
18     int ii=i,jj=j;
19
20     if(grid[ii][jj]=='1'){
21         grid[ii][jj]='0';
22     }else{
23         grid[ii][jj]='1';
24     }
25
26     ii=i+1,jj=j;
27     if(right(ii,jj)){
28         if(grid[ii][jj]=='1'){
29             grid[ii][jj]='0';
30         }else{
31             grid[ii][jj]='1';
32         }
33     }
34
35     ii=i-1,jj=j;
36     if(right(ii,jj)){
37         if(grid[ii][jj]=='1'){
38             grid[ii][jj]='0';
39         }else{
40             grid[ii][jj]='1';
41         }
42     }
43
44     ii=i,jj=j+1;
45     if(right(ii,jj)){
46         if(grid[ii][jj]=='1'){
47             grid[ii][jj]='0';
48         }else{
49             grid[ii][jj]='1';
50         }
51     }
52
53     ii=i,jj=j-1;
54     if(right(ii,jj)){
55         if(grid[ii][jj]=='1'){
56             grid[ii][jj]='0';
57         }else{
58             grid[ii][jj]='1';
59         }
60     }
61
62 }
63
64 int main()
65 {
66     char reserve[16][16];
67     char grid[16][16];
68     char result[16][16];
69     char output[16][16];
70     int bin[16];
71     scanf("%d %d",&m,&n);
72     getchar();
73     for(int i=0;i<m;i++){
74         for(int j=0;j<n;j++){
75             scanf("%c",&reserve[i][j]);
76             getchar();
77         }
78     }
79     int coun=1<<n;
80
81     int mincou=99999;
82     for(int iii=0;iii<coun;iii++){
83
84         memcpy(grid,reserve,sizeof(reserve));
85         memset(result,'0',sizeof(result));
86
87         int cou=0;
88         int t=iii;
89         int b_i=0;
90         bin[0]=0;
91
92         while(t){
93             bin[b_i]=t%2;
94             t/=2;
95             result[0][b_i]='0'+bin[b_i];
96             b_i++;
97         }
98         for(int i=0;i<n;i++){
99             if(result[0][i]=='1'){
100                 cou++;
101                 flip(grid,0,i);
102             }
103         }
104
105         for(int i=0;i<m-1;i++){
106             for(int j=0;j<n;j++){
107                 if(grid[i][j]=='1'){
108                     flip(grid,i+1,j);
109                     result[i+1][j]='1';
110                     cou++;
111                 }
112             }
113         }
114         bool f=true;
115         for(int i=0;i<n;i++){
116             if(grid[m-1][i]=='1'){
117                 f=false;
118                 break;
119             }
120         }
121         if(f==true){
122             if(cou<mincou){
123                 mincou=cou;
124                 memcpy(output,result,sizeof(result));
125             }else if(cou==mincou){
126                 bool flag=true;
127                 for(int i=0;i<m;i++){
128                     for(int j=0;j<n;j++){
129                         if(output[i][j]>result[i][j]){
130                             break;
131                         }else if(output[i][j]<result[i][j]){
132                             flag=false;
133                             break;
134                         }
135                     }
136                 }
137                 if(flag){
138                     memcpy(output,result,sizeof(result));
139                 }
140             }
141         }
142     }
143
144     if(mincou==99999){
145         printf("IMPOSSIBLE\n");
146     }else{
147         for(int i=0;i<m;i++){
148             for(int j=0;j<n;j++){
149                 if((i==m-1) && (j==n-1)){
150                     printf("%c",output[i][j]);
151                 }else{
152                     printf("%c ",output[i][j]);
153                 }
154             }
155             if(i!=m-1){
156                 printf("\n");
157             }
158
159         }
160     }
161
162
163     return 0;
164 }