poj 3279

poj 3279 Fliptile(状压+枚举)

题目

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

在这里插入图片描述
在这里插入图片描述

题目大意

题目大意就是说有一个矩阵,矩阵的元素只有0或者1,你可以选择一个元素进行翻转,如果翻转这个元素,那么它上下左右的元素也会一起翻转,翻转的意思就是从0变成1,或者从1变成0,题目最后是询问是否有可能把矩阵全部变成0。如果有,则输出一个矩阵,矩阵里面标1的就是你之前翻转的矩阵元素,如果没有就输出impossible。

思路

一开始陷入了惯性思维,毕竟说了是kb搜索(怪我怪我),一直想怎么dfs和bfs,怎么也没有思路,最后搜了一下题解,看到别人说的是状压和枚举,之前没上手过状压,只会个概念,最后看了看一些题解,就是发现一些问题。题目解决的突破口就在于,第n行如果有元素1,如果你想把它变成0但是又不影响第n行其他元素,你只能改变第n+1行的元素,那么意思就是,第n+1行的开关状态是由第n行决定的。那么我只要知道第一行的状态就好了,那么只用枚举第一行的状态就行,因为只有两个0、1两个状态,所以就用二进制表示,实现状压。(这个叫状压吗……)

代码

#include <iostream>
#include <cstdio>
#include <set>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;
#define dbg(x) cerr << #x " = " << x <<endl;
typedef pair<int, int> P;
typedef long long ll;

int a[20][20];
int tmp[20][20];
int ans[20][20];
int n, m;

int dfs(int layer, int num)
{
    int tmp_pos = 0;
    while(num > 0)
    {
        int ttmp = num & 1;
        if(ttmp == 1)
        {
            tmp[layer][tmp_pos] ^= 1;
            ans[layer][tmp_pos] = 1;
            if(layer-1 >= 0) tmp[layer-1][tmp_pos] ^= 1;
            if(layer+1 < n) tmp[layer+1][tmp_pos] ^= 1;
            if(tmp_pos-1 >= 0) tmp[layer][tmp_pos-1] ^= 1;
            if(tmp_pos+1 < m) tmp[layer][tmp_pos+1] ^= 1;
        }
        num >>= 1;
        tmp_pos++;
    }
    for(int i = 1; i < n; i++)
    {
        for(int j = 0; j <m ; j++)
        {
            if(tmp[i-1][j])
            {
                ans[i][j] = 1;
                tmp[i][j] ^= 1;
                if(i-1 >= 0) tmp[i-1][j] ^= 1;
                if(i+1 < n) tmp[i+1][j] ^= 1;
                if(j-1 >= 0) tmp[i][j-1] ^= 1;
                if(j+1 < m) tmp[i][j+1] ^= 1;
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            if(tmp[i][j]) return 0;
        }
    }
    return 1;
}


int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < m; j ++)
        {
            cin >> a[i][j];
            tmp[i][j] = a[i][j];
        }
    }
    int flg = 0;
    for(int i = 0; i < 1<<m; i++)
    {
        memset(ans, 0, sizeof(ans));
        for(int j = 0; j < n; j++)
        {
            for(int k = 0; k < m; k++)
            {
                tmp[j][k] = a[j][k];
            }
        }
        if(dfs(0, i))
        {
            flg = 1;
            break;
        }
    }
    if(!flg)
    {
        cout << "IMPOSSIBLE" << endl;
    }
    else 
    {
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                cout << ans[i][j] << ' ';
            }
            cout << endl;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值