题意:有一个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;
}

  

POJ - 3279 Fliptile(反转---开关问题)的更多相关文章

  1. 【POJ 3279 Fliptile】开关问题,模拟

    题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...

  2. POJ 3279 Fliptile ( 开关问题)

    题目链接 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give ...

  3. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  4. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  5. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  6. POJ 3279(Fliptile)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...

  7. POJ 3279 Fliptile(DFS+反转)

    题目链接:http://poj.org/problem?id=3279 题目大意:有一个n*m的格子,每个格子都有黑白两面(0表示白色,1表示黑色).我们需要把所有的格子都反转成黑色,每反转一个格子, ...

  8. POJ 3279 Fliptile(反转 +二进制枚举)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13631   Accepted: 5027 Descrip ...

  9. POJ 3279 Fliptile[二进制状压DP]

    题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...

随机推荐

  1. linux环境基于python语言docx转pdf

    windows平台因借助win32com具有多种方法将word转为pdf,但linux环境不具备此环境,win32com包也将import失败,那该如何做呢? # -*- coding: utf-8 ...

  2. 继OpenJDK 之后,OpenJFX也将迁移到 Git

    导读 近日 OpenJFX 项目负责人 Kevin Rushforth 提交了一份将 OpenJFX 迁移到 GitHub 的提案. OpenJFX 是 JavaFX 的开源实现.JavaFX 是一个 ...

  3. 安装数据库Typical path for xclock: /usr/X11R6/bin/xclock 错误问题

    [oracle@localhost database]$ ./runInstaller Starting Oracle Universal Installer... Checking Temp spa ...

  4. other#一些问题的列表

    centos7及以后修改hostname, hostnamectl set-hostname centos7 centos7之前修改hostname, vi /etc/sysconfig/networ ...

  5. 拓扑排序--P2881 [USACO07MAR]排名的牛Ranking the Cows

    *传送 FJ想按照奶牛产奶的能力给她们排序.现在已知有N头奶牛(1 ≤ N ≤ 1,000).FJ通过比较,已经知道了M(1 ≤ M ≤ 10,000)对相对关系.每一对关系表示为“X Y”,意指X的 ...

  6. spring core:@AliasFor的派生性

    spring对Annotation的派生性应用可谓炉火纯青,在spring core:@Component的派生性讲过支持层次上派生性,而属性上派生的需求则借助了@AliasFor,它是从spring ...

  7. IDE及PHP基础——注释、变量、数据、运算符、输出等

    IDE(Integrated Development Environment ),集成开发环境,是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面等工具.集成了代码编写 ...

  8. c# 外挂操作(内存操作)(内存读写取窗口句柄移动窗口取模块地址)工具类

    来源于网上  参考 https://www.cnblogs.com/fuhua/p/5877781.html 等众多文章 详情取看我第二个例子封装功能较多 https://www.cnblogs.co ...

  9. B. Yet Another Crosses Problem

    B. Yet Another Crosses Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  10. Windows10 网络图标消失 连接不上网络 的解决方法

    [背景]电脑win10的,下载一个软件重启之后网络图标消失,并且无法联网. 参照此解决方法: 原因: [Windows Event Log]服务对应的注册表出现问题,导致无法正常启动,进而导致一些依赖 ...