题目链接:http://poj.org/problem?id=3279
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

Problem solving report:

Description: 有一个 M * N 的格子,每个格子可以翻转正反面,它们有一面是黑色,另一面是白色。黑色翻转之后变成白色,白色翻转之后则变成黑色。游戏要做的是把所有的格子翻转为白色。不过每次翻转一个格子,与它上下左右相邻接的格子也会被翻转。求用最小的步数完成时,每个格子的翻转次数。最小步数的解有多个时,输出字典序最小的一组;解不存在的话,则输出IMPOSSIBLE。
Problem solving: 首先,同一个格子翻转两次就会恢复原状,所以多次翻转是多余的。不妨先指定好最上面一行的翻转方法。然后判断下面的一行需不需要翻转,如此反复下去就能确定所有格子的翻转方法,如果最后一行并非全为白色,则意味着不存在可行的操作方法。将第一行的所有翻转方式都尝试一次就能求出整个问题的最小步数,最上面一行的翻转方式共有2^N种

Accepted Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 20;
const int inf = 0x3f3f3f3f;
int min_, m, n;
int dir[5][2] = {{0, 1}, {1, 0}, {0, 0}, {0, -1}, {-1, 0}};
int hap[MAXN][MAXN], pha[MAXN][MAXN], mp[MAXN][MAXN];
int DFS(int x, int y) {
    int res = mp[x][y];
    for (int i = 0; i < 5; i++) {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if (tx >= 0 && tx < m && ty >= 0 && ty < n)
            res += hap[tx][ty];
    }
    return res & 1;
}
int Flip() {
    int cnt = 0;
    for (int i = 1; i < m; i++)
        for (int j = 0; j < n; j++)
            if (DFS(i - 1, j))
                hap[i][j] = 1;
    for (int i = 0; i < n; i++)
        if (DFS(m - 1, i))
            return inf;
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            cnt += hap[i][j];
    return cnt;
}
bool slove() {
    int res;
    for (int i = 0; i < 1 << n; i++) {
        memset(hap, 0, sizeof(hap));
        for (int j = 0; j < n; j++)
            hap[0][n - j - 1] = i >> j & 1;
        res = Flip();
        if (res < min_) {
            min_ = res;
            memcpy(pha, hap, sizeof(hap));
        }
    }
    return min_ < inf;
}
int main() {
    min_ = inf;
    scanf("%d%d", &m, &n);
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            scanf("%d", &mp[i][j]);
    if (slove()) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++)
                printf("%d ", pha[i][j]);
            printf("\n");
        }
    }
    else printf("IMPOSSIBLE\n");
    return 0;
}