POJ-3279 Fliptile(爆枚优化)

本文探讨了一种涉及翻转网格中黑白方块的智力挑战,目标是最小化操作次数使所有方块翻转至白色面朝上。通过深度优先搜索(DFS)策略,文章详细解释了如何递归枚举所有可能的第一行状态,并逐步确定后续行的状态,以找到最少翻转次数及具体操作位置。若任务无法完成,将返回IMPOSSIBLE。

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

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 × Ngrid (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

先确定第一行,只有第二行能影响第一行,则第二行也确定 ……下边都确定了。

先递归枚举所有第一行的情况(DFS){

              然后检查是否能达成全部都是0 而且 操作次数比之前更少

              如果是,记录操作二维数组

}

最后输出

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


void p();
void turn(bool arr[20][20],int i, int j);
void copy_arr();
void copy_ans();
void dfs(int);


int check();
int n,m,ans = 99999999,f=0;
bool o[20][20], o_ans[20][20], arr[20][20], arr2[20][20];


int main(){
    scanf("%d%d",&n,&m);
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++){
        char c = '\0';
        while(1) if((c=getchar()) =='1' || c=='0') break;
        arr[i][j] = c-'0';
    }
    
    dfs(1);
    if(f==0)
        printf("IMPOSSIBLE\n");
    else
        p();
    return 0;
}

void dfs(int j){
    if(j == m+1){
        copy_arr();
        int t = check();
        if(t < ans){ans = t, f=1;copy_ans();}
    }else{
        dfs(j+1);//不反

        o[1][j] = 1;//反转
        turn(arr,1,j);
        dfs(j+1);
        o[1][j] = 0;//还原
        turn(arr,1,j);
    }
}

int check(){
    int t = 0;
    for(int i = 2; i <= n; i++)
    for(int j = 1; j <= m; j++)
        if(arr2[i-1][j]){
            turn(arr2, i, j);
            o[i][j] = 1;
        }else o[i][j] = 0;
    for(int j = 1; j <= m; j++)
        if(arr2[n][j]) return 99999999;
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
        if(o[i][j]) t++;
    return t;
}
void copy_ans(){
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            o_ans[i][j] = o[i][j];
}
void p(){
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++)
            o_ans[i][j] == 1?printf("1 "):printf("0 ");
        printf("\n");
    }
}
void turn(bool arr[20][20],int i, int j){
    arr[i][j] = !arr[i][j];
    arr[i+1][j] = !arr[i+1][j];
    arr[i-1][j] = !arr[i-1][j];
    arr[i][j+1] = !arr[i][j+1];
    arr[i][j-1] = !arr[i][j-1];
}

void copy_arr(){
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
        arr2[i][j] = arr[i][j];
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值