POJ - 3279 - Fliptile(暴力\递推)

题目链接点我
题目大意:给你一个nxm行的01矩阵,你每次可以点一个位置然后反转这个位置上下左右以及自己的状态,问你是否可以把它全置为0,如果可以,把字典序最小的情况输出出来。
  这题和我上一篇博文类似传送门←_←不同的是,题目要把变换次数最小的情况下字典序最小的结果输出出来。其实字典序不用担心,因为我们二进制枚举本来就是按字典序从低到高来的,所以我们只要关心次数就行了。

//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl '\n'
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define maxx(a, b) (a > b ? a : b)
#define minn(a, b) (a < b ? a : b)
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("==================================================\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  1000000007LL;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const double EULC = 0.5772156649015328;
const int NIL = -1;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 17;
int dx[] = {0, 0, 0, 1, -1};
int dy[] = {1, -1, 0, 0, 0};
int n, m, cnt;
char g[maxn][maxn], bk[maxn][maxn], ans[maxn][maxn], tmp[maxn][maxn];
void turn(int x, int y) { //改变自己&&周围4个点的状态
    ++cnt;
    tmp[x][y] = '1'; 
    for (int i = 0; i<5; ++i) {
        int xx = x+dx[i], yy = y+dy[i];
        if (xx>=0 && xx<n && yy>=0 && yy<m)
            bk[xx][yy] ^= 1;
    }    
}
int main(void) {
    while(~scanf("%d%d%*c", &n, &m)) {
        for (int i = 0; i<n; ++i)
            for (int j = 0; j<m; ++j)
                scanf("%c%*c", &g[i][j]);
        int __max = INF;
        for (int k = 0; k<1<<m; ++k) { 
            cnt = 0;
            memcpy(bk, g, sizeof(g)); //复制初始状态
            for (int i = 0; i<n; ++i) //默认每个点的点击数目全为0
                for (int j = 0; j<m; ++j)
                    tmp[i][j] = '0';
            for (int i = 0; i<m; ++i) //二进制枚举第一行所有情况
                if (k>>i&1) turn(0, i);
            for (int i = 0; i<n-1; ++i) //根据上一行状态递推下一行操作
                for (int j = 0; j<m; ++j)
                    if (bk[i][j]&1) turn (i+1, j);
            bool flag = true;
            for (int i = 0; i<m; ++i) //检查最后一行是否全为0
                if (bk[n-1][i]&1) flag = false;
            if (flag && __max > cnt) {
                __max = cnt;
                memcpy(ans, tmp, sizeof(tmp));
            }
        }
        if (__max != INF)
            for (int i = 0; i<n; ++i)
                for (int j = 0; j<m; ++j)
                    printf(j==m-1 ? "%c\n" : "%c ", ans[i][j]);
        else printf("IMPOSSIBLE\n");
    }
    return 0;
}
posted @ 2020-03-18 17:53  shuitiangong  阅读(115)  评论(0编辑  收藏  举报