POJ 3279 Fliptile(反转开关问题)

题意:有一个M*N的网格,有黑有白,反转使全部变为白色,求最小反转步数情况下的每个格子的反转次数,若最小步数有多个,则输出字典序最小的情况。解不存在,输出IMPOSSIBLE。

分析:

1、枚举第一行的所有反转情况,共2N。二进制枚举子集,可使字典序最小。

2、研究0~M-2行,分别确定当前行的下一行的反转情况。flip---每个格子是否反转,1---反转,0---不反转。

eg:第0行的第1个元素a[0][0],要使其变为白色,除了可以反转a[0][0],还可以a[0][1]和a[1][0]。

通过二进制枚举子集,可知flip[0][0],flip[0][1]的情况,再加上a[0][0],若考虑完这些因素,a[0][0]还是黑色,那么a[1][0]也需要反转,否则,不需反转。

3、最后验证一下a[M-1][0]~a[M-1][N-1]如果全为白色,则此操作可行。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
    if(fabs(a - b) < eps) return 0;
    return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {0, -1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 15 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN][MAXN];
int flip[MAXN][MAXN];
int pic[MAXN][MAXN];
int M, N;
bool judge1(int x, int y){
    return x >= 0 && x < M && y >= 0 && y < N;
}
int get_cnt(int x, int y){//得到周边及自身的翻转次数
    int cnt = 0;
    for(int i = 0; i < 5; ++i){//若某方向元素还未研究,则flip[tmpx][tmpy]为0,不影响计算结果
        int tmpx = x + dr[i];
        int tmpy = y + dc[i];
        if(judge1(tmpx, tmpy)){
            cnt += flip[tmpx][tmpy];
        }
    }
    return cnt;
}
bool judge2(){//判断该枚举结果是否能使最后一行全为白色
    for(int i = 0; i < N; ++i){
        int cnt = get_cnt(M - 1, i);
        if((cnt + a[M - 1][i]) % 2 != 0) return false;
    }
    return true;
}
int cal(){
    for(int i = 0; i < M - 1; ++i){
        for(int j = 0; j < N; ++j){
            int cnt = get_cnt(i, j);
            if((cnt + a[i][j]) % 2 != 0){//来自上左右自身的翻转后仍为黑色
                flip[i + 1][j] = 1;
            }
        }
    }
    if(!judge2()) return -1;
    int num = 0;
    for(int i = 0; i < M; ++i){
        for(int j = 0; j < N; ++j){
            num += flip[i][j];
        }
    }
    return num;
}
void solve(){
    int ans = INT_INF;
    for(int i = 0; i < (1 << N); ++i){
        memset(flip, 0, sizeof flip);
        for(int j = 0; j < N; ++j){
            if(i & (1 << j)){
                flip[0][j] = 1;
            }
        }
        int cnt = cal();
        if(cnt != -1){
            if(cnt < ans){
                ans = cnt;
                memcpy(pic, flip, sizeof flip);
            }
        }
    }
    if(ans == INT_INF) printf("IMPOSSIBLE\n");
    else{
        for(int i = 0; i < M; ++i){
            for(int j = 0; j < N; ++j){
                if(j) printf(" ");
                printf("%d", pic[i][j]);
            }
            printf("\n");
        }
    }
}
int main(){
    scanf("%d%d", &M, &N);
    for(int i = 0; i < M; ++i){
        for(int j = 0; j < N; ++j){
            scanf("%d", &a[i][j]);
        }
    }
    solve();
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6547544.html