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

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 
#include 
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< 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";
    }
}

你可能感兴趣的:(简单搜索)