Poj 3279 FlipTile

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16502 Accepted: 6030

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

 

#include<iostream>
#include<stdio.h>
#include<vector>
#include<map>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<set>
#include<cmath>
using namespace std;
const int MAX_N = 17;

/*
    题目大意:有一个 M * N 的格子,每个格子可以翻转正反面,它们有一面是黑色,另一面是白色。黑色翻转之后变成白色,白色翻转之后则变成黑色。
 游戏要做的是把所有的格子翻转为白色。不过因为牛蹄很大,所以每次翻转一个格子,与它上下左右相邻接的格子也会被翻转。
    求用最小的步数完成时,每个格子的翻转次数。最小步数的解有多个时,输出字典序最小的一组;解不存在的话,则输出IMPOSSIBLE

    题目样例:0表示白色,1表示黑色

            1 0 0 1
            0 1 1 0
            0 1 1 0
            1 0 0 1

    题目思路:首先,同一个格子翻转两次就会恢复原状,所以多次翻转是多余的。此外,翻转的格子的集合相同的话,其次序是无关紧要的。

            不妨先指定好最上面一行的翻转方法。此时,能翻转(1,1)的只剩下了(2,1),所以可以直接判断(2,1)是否需要翻转。类似的(2,1)~(2,N)都能这样判断,
        如此反复下去就能确定所有格子的翻转方法,最后(M,1)~(M,N)并非全为白色,则意味着不存在可行的操作方法。

            像这样,先确定第一行的翻转方式,然后可以很容易判断这样是否存在解以及解的最小步数是多少,这样将第一行的所有翻转方式都尝试一次就能求出整个问题的最小步数。这个算法中最上面
        一行的翻转方式共有2^N种,复杂度为O(M * N * 2^N)

 */

int opt[MAX_N][MAX_N];
int flip[MAX_N][MAX_N]; // 记录瓷砖翻次数
int a[MAX_N][MAX_N];
int dir[5][2] = {{-1,0},{1,0},{0,1},{0,-1},{0,0}};

int m,n;

// 统计 位置(x,y) 周围(包括自身)翻转次数
int get(int x, int y){
    // 记录自身颜色 1 的话相当于 翻转了一次
    int res = a[x][y];
    for(int i = 0; i < 5; ++i){
        int nx = x + dir[i][0];
        int ny = y + dir[i][1];
        if(nx>=0 && nx <m && ny >=0 && ny <n){
            res += flip[nx][ny];
        }
    }
    return res % 2;
}

// 计算在第一行 翻转情况确定的情况下 是否可以 成功翻转所有瓷砖(求出最小翻转次数)
int calc(){
    for(int i = 1; i < m; ++i){
        for(int j = 0; j < n; ++j){
            // 按照从上到下 从左到右的顺序 判断 所以只有上面 或者左侧 是已经确定的(所以此时左侧和上侧决定该瓷砖要不要翻转)
            if(get(i-1,j) != 0){
                flip[i][j] = 1;
            }
        }
    }

    // 最后一行 仍存在 未翻转的瓷砖 则 此法不可行
    for(int j = 0; j < n; ++j){
        if(get(m-1,j) != 0)
            return -1;
    }

    int res = 0;
    // 统计 所有的翻转次数
    for(int i = 0; i < m; ++i){
        for(int j = 0; j < n; ++j){
            if(flip[i][j]){
                // 这里不可以 ++res 因为 flip[i][j] 可能不为1
                res += flip[i][j];
            }
        }
    }

    return res;
}


void solve(){
    /*  以n = 4 为例 :从 0 - 15 对应二进制位 0000 - 1111
        每个 位置 对应值 翻与不翻
        (注意不要与初始输入的值混淆,那里的1,0 代表初始的黑白颜色)
        这里表示翻执行的时候 是否翻转
     */
    int res = -1;
    for(int i = 0; i < 1 << n; ++i){
        // 不要忘记 初始化第一行
        memset(flip,0,sizeof(flip));
        for(int j = 0; j < n; ++j){
            flip[0][n-j-1] = i >> j & 1; // 从右到左(先取低位) 依次把 首行翻转情况 赋值给flip
        }
        int num = calc();
        if(num >= 0 && (res < 0 || res > num)){
            res = num;
            // 最优解
            memcpy(opt,flip,sizeof(flip));
        }
    }

    if(res == -1){
        cout << "IMPOSSIBLE" <<endl;
    }
    else{
        for(int i = 0; i < m; ++i){
            for(int j = 0; j < n; ++j){
                if(j == 0){
                    cout << opt[i][j];
                }
                else{
                    cout << " "<<opt[i][j];
                }
            }
            cout << endl;
        }
    }
}

int main()
    {
        ios::sync_with_stdio(false);
        while(cin >> m >> n){
            memset(flip,0,sizeof(flip));
            memset(a,0,sizeof(a));
            for(int i = 0; i < m; ++i){
                for(int j = 0; j < n; ++j)
                    cin >> a[i][j];
            }
            solve();
        }

        return 0;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值