Fellow me on GitHub

POJ3279(KB1-D 熄灯问题)

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

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 //2017-02-22
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 
 6 using namespace std;
 7 
 8 bool grid[20][20], tmp[20][20];
 9 int n, m, ans[20], res[20];
10 
11 void flip(int x, int y)
12 {
13     tmp[x][y] = !tmp[x][y];
14     if(x-1>=0)tmp[x-1][y] = !tmp[x-1][y];
15     if(y-1>=0)tmp[x][y-1] = !tmp[x][y-1];
16     tmp[x+1][y] = !tmp[x+1][y];
17     tmp[x][y+1] = !tmp[x][y+1];
18 }
19 
20 void solve()
21 {
22     int penn, minflip = 0x3f3f3f3f;
23     bool fg = false;
24     for(int i = 0; i < (1<<m); i++)
25     {
26         for(int x = 0; x < n; x++)
27               for(int y = 0; y < m; y++)
28                   tmp[x][y] = grid[x][y];
29         for(int y = 0; y < m; y++)
30             if(i&(1<<y))
31                   flip(0, m-1-y);
32         ans[0] = i;
33         for(int x = 1; x < n; x++){
34             penn = 0;
35             for(int y = 0; y < m; y++){
36                 if(tmp[x-1][y]){
37                     flip(x, y);
38                     penn += (1<<(m-1-y));
39                 }
40             }
41             ans[x] = penn;
42         }
43         bool ok = true;
44         for(int j = 0; j < m; j++)
45               if(tmp[n-1][j])
46                   ok = false;
47         if(ok){
48             fg = true;
49             int cnt = 0;
50             for(int j = 0; j < n; j++){
51                 for(int pos = 0; pos < m; pos++)
52                       if(ans[j]&(1<<(m-1-pos)))cnt++;
53             }
54             if(cnt < minflip){
55                 minflip = cnt;
56                 for(int k = 0; k < n; k++)
57                       res[k] = ans[k];
58             }
59         }
60     }
61     if(!fg)cout<<"IMPOSSIBLE"<<endl;
62     else{
63         for(int j = 0; j < n; j++){
64             for(int pos = 0; pos < m; pos++)
65                   if(pos == m-1)cout<<(res[j]&(1<<(m-1-pos))?1:0)<<endl;
66                 else cout<<(res[j]&(1<<(m-1-pos))?1:0)<<" ";
67         }
68     }
69 }
70 
71 int main()
72 {
73     while(cin>>n>>m)
74     {
75         for(int i = 0; i < n; i++)
76               for(int j = 0; j < m; j++)
77                   cin>>grid[i][j];
78         solve();
79     }
80 
81     return 0;
82 }

 

posted @ 2017-02-22 22:03  Penn000  阅读(320)  评论(0编辑  收藏  举报