[搜索]poj3279 fliptile

Fliptile
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4886 Accepted: 1870
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

输入一个m*n的格子,其中有黑色的面(1)、白色的面(0),对每个面可以选择翻转或者不翻转,当进行翻转时,与它上下左右相邻的格子也会被翻转。求将所有格子全部翻转成白色的最小步数。

首先,同一个格子翻转两次的话就会恢复原状,翻转三次就是跟翻转一次的结果完全相同。所以对于每个格子,只有可能翻转0次或者1次。另外,很容易可以推出,在翻转的时候,只要最终翻转的位置相同,先后次序改变是不会影响最终结果的。所以考虑把最优解存在m*n的数组中。

首先可以考虑所有枚举的办法。总共有2^(m*n)的办法,太大了,不能在给定时间内求出最优解,需要找到另外的条件来剪枝。
先考虑最左上角的格子,如果预先枚举好了横向的翻转方法,那么除去横向的位置可以影响该位置的情况外,只有(2,1)这个格子的翻动才会影响到这个格子的黑白情况。所以可以根据该格子的黑白情况直接判断出2*1是否需要翻转。
同理,(2,2)到(2,n)的格子情况都可以根据已经枚举好的(1,2)到(1,n)的来确定。所以第二行的格子情况也可以全部确定。
以此类推,如此反复下去就可以确定所有格子的翻转方法。最后判断(m,1)到(m,n)是否全为白色,就可以判断是否存在可行的操作方法。

这个算法中,最上面一行的翻转方式有2^n种,复杂度为O(m*n*2^n),n最大为15,至此就可以无压力出解了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

//枚举第一行可能的情况,总共2的15次
const int MAXN = 20;
int mat[MAXN][MAXN];
int opt[MAXN][MAXN];
int flip[MAXN][MAXN]; //保存中间结果即翻转的情况
int m,n;
const int dx[5] = {-1,0,0,0,1};
const int dy[5] = {0,-1,0,1,0};

int get(int x,int y) //根据mat的情况来写结果
{
    int c = mat[x][y];
    for(int i = 0; i < 5; i++)
    {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx>=0 && nx < m && ny>= 0 && ny < n)
        {
            c+= flip[nx][ny];
        }
    }
    return c % 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++)
            res += flip[i][j];
    return res;
}

int main()
{

    while(cin>>m>>n)
    {
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                cin>>mat[i][j];
            }
        }
        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;
            }
            int num = calc(); //第一行已经确定了,求出第1行确定情况下的最小操作次数。
            if (num >= 0 && (res < 0 || res > num))
            {
            res = num;
            memcpy(opt,flip,sizeof(flip));
            }
        }

        if (res < 0)
            puts("IMPOSSIBLE");
        else
        {
            int i, j;
            for(i = 0 ; i < m; i++)
            {
                for(j = 0; j < n-1; j++)
                {
                    printf("%d ",opt[i][j]);
                }
                printf("%d\n",opt[i][j]);
            }
        }
    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值