(POJ 3279) Fliptile 反转(开关问题)

POJ-3279-Fliptile

                Time Limit:2000MS     Memory Limit:65536KB 

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

关于难点自己的具体理解过程:
看完上面的讲解可能还不是很明白,解题的关键思想是:用下一行的(i,j)【i>=1】位置的反转去维护上一行(i-1,j)位置的情况,使上面一行全部为0.在利用最后一行维护完倒数第二行后,看最后一行是否也满足全为0.如果是全为0则可行,否则不可行。
由上面的话可知,若第一行一定,那么经过维护之后我们就知道了在这种情况下到底可不可行。而第一行的反转情况用2^N种,所以我们枚举出来。
我们用flip[i][j]来表示翻转情况,0表示不翻转,1表示翻转。这N个0,1的数可以让我们想到了二进制。我们可以将第一行的翻转情况用(N=4时)0000~1111来表示。代码如下:

//按照字典序尝试第一行的所有可能性
    for (int i = 0; i < 1 << N; i++)  //i表示一个二进制数,用来枚举第1行的各种不同翻法,如0001就是只翻最后一个
    {
        memset(flip,0,sizeof(flip));
        for (int j = 0; j < N; j++)
        {
            flip[0][N - j - 1] = i >> j & 1;
            /*eg:0011001
            ①j == 0; i >> j 即0011001 & 1 -> 1
            ②j == 1; i >> j 即0001100 & 1 -> 0
            ...
            每次取出最后一位,存入flip中
            */
        }
        。。。。。
    }

flip[0][N - j - 1] = i >> j & 1; 这句代码的意思是将i的二进制右移j位后与1“与”(当最后一位为0是结果为0,为1时结果为1)

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#include<iomanip>
using namespace std;

const int maxn = 16;
int M,N;
const int dx[5] = {-1,0,0,0,1};
const int dy[5] = {0,-1,0,1,0};
int tile[maxn][maxn];
int opt[maxn][maxn]; //保存最优解
int flip[maxn][maxn]; //保存中间结果

//查询(x,y)的颜色
int get(int x,int y)
{
    int c = tile[x][y];  //注意这里要加上原来的状态
    for (int d = 0; d < 5; d++)  //查询周围四个以及自己的翻转次数
    {
        int x2 = x + dx[d],y2 = y + dy[d];
        if (0 <= x2 && x2 < M && 0 <= y2 && y2 < N)
        {
            c += flip[x2][y2];
        }
    }
    return c % 2;  //奇数为1,偶数为0
}

//求出第1行确定的情况下的最小操作次数
//不存在解得话,返回-1
int calc()
{
    //求出从第2行开始的翻转方法
    for (int i = 1; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
            if (get(i - 1,j) != 0)
            {
                //如果(i- 1,j)是黑色的话,则必须翻转这个格子
                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;
}
void solve()
{
    int res = -1;
    //按照字典序尝试第一行的所有可能性
    for (int i = 0; i < 1 << N; i++)  //i表示一个二进制数,用来枚举第1行的各种不同翻法,如0001就是只翻最后一个
    {
        memset(flip,0,sizeof(flip));
        for (int j = 0; j < N; j++)
        {
            flip[0][N - j - 1] = i >> j & 1;
            /*eg:0011001
            ①j == 0; i >> j 即0011001 & 1 -> 1
            ②j == 1; i >> j 即0001100 & 1 -> 0
            ...
            每次取出最后一位,存入flip中
            */
        }
        int num = calc();  //num记录翻转次数
        if (num >= 0 && ( res < 0 || res > num))  //如果找到一种可能并且所用步数更少的话,记下这种翻法
        {
            res = num;
            memcpy(opt,flip,sizeof(flip));
        }
    }
    if (res < 0)
    {
        //无解
        printf("IMPOSSIBLE\n");
    }
    else  //最后找到的就是最少的翻法,模拟一遍,然后输出
    {
        for (int  i = 0; i < M; i++)
        {
            for (int j = 0; j < N; j++)
            {
                printf("%d%c",opt[i][j], j + 1 == N ? '\n': ' ');
            }
        }
    }
}
int main()
{
    cin >> M >> N;
    for (int i = 0; i < M; i ++)  //数据输入
    {
        for (int j = 0; j < N; j++)
        {
            cin >> tile[i][j]; //0表示白色,1表示黑色
        }
    }
    solve();
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值