[kuangbin带你飞]专题一 简单搜索-D - Fliptile POJ - 3279

本文介绍了一种基于网格翻转的智力游戏算法,旨在帮助玩家以最少的步骤将所有方块翻转到同一面。文章详细解释了通过枚举策略解决该问题的方法,并提供了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题解:

        题目的意思是有一个M*N的灯泡,一个灯泡有两种状态,要么是 1 ,代表背着我们, 要么是 0,代表正对着我们。我们可以选择翻转灯泡,但是每次翻转都会其上下左右的灯泡都被翻转,我们的枚举就是非常的神奇了。只需枚举第一行的情况即可,因为后面的翻转情况都是由上一行的情况所决定的。
        这题还有几点要注意的.
        1.需要最小的操作次数。
        2.如果有多种方案,要以字典序的方式输出最小的。
#include <iostream>
#include <cstring>
using namespace std;

const int MAXN = (int)20;
int N,M;

int NumMin = 1000007;
int TemMin = 0;
int ans[MAXN][MAXN];
int pic[MAXN][MAXN];
int tem[MAXN][MAXN];//每次临时的
int fli[MAXN][MAXN];//结果时翻出的的点

void Filp(int x,int y){
    TemMin ++;
    tem[x][y]   = !tem[x][y];
    tem[x+1][y] = !tem[x+1][y];
    tem[x-1][y] = !tem[x-1][y];
    tem[x][y+1] = !tem[x][y+1];
    tem[x][y-1] = !tem[x][y-1];
}

//如果成功返回true,否则返回false
bool Enum(){
    for (int j = 1;j <= N;j ++)
        if (fli[1][j])Filp(1,j);

    for (int i = 1;i < M;i ++){
        for (int j = 1;j <= N;j ++){
            if (tem[i][j])Filp(i+1,j),fli[i+1][j] = 1;
        }
    }

    bool flag = 1;
    for (int j = 1;j <= N;j ++){
        if (tem[M][j])flag = false;
    }
    return flag;
}

int main()
{
    ios::sync_with_stdio(false);

    cin >> M >> N;

    for (int i = 1;i <= M;i ++){
        for (int j = 1;j <= N;j ++){
            cin >> pic[i][j];
        }
    }


    int flag = 0;
    for (int i = 0;i < 1<<N;i ++){
        memset(fli, 0 ,sizeof(fli));
        memcpy(tem,pic,sizeof(pic));
        TemMin = 0;
        int t = i;
        for (int j = N;j > 0;j --){
            if (t&1)fli[1][j] = 1;
            t >>= 1;
        }


        if (Enum() && NumMin > TemMin){
            flag = 1;
            NumMin = TemMin;
            memcpy(ans,fli,sizeof(fli));
        }

    }

    if (flag){
        for (int i = 1;i <= M;i ++){
            for (int j = 1;j <= N;j ++){
                cout << ans[i][j] << (j==N?'\n':' ');
            }
        }
    }else {
        cout << "IMPOSSIBLE\n";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值