http://poj.org/problem?id=3279

题意

一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最小字典序的操作。

分析

爆搜铁定超时。。。对于一个格子,要么反转一次,要么反转零次。反转的顺序不改变最终结果。那么我们试着枚举第一行的反转情况(状态压缩),此时能影响第一行的只有第二行的格子了,依次类推,一行一行来。最后检测最后一行的值,就知道了此状态可行不可行。枚举时可以确定反转的字典序从小到大。

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1e9+;
int T;
void testcase(){
printf("Case %d: ",++T);
}
const int MAXN = 3e5+;
const int MAXM = ;
int dx[]={-,,,,};
int dy[]={,-,,,};
int g[][];
int ans[][],tmp[][];
int n,m; bool check(int x,int y){
if(x<||x>=n||y<||y>=m) return false;
return true;
}
int get(int x,int y){
int res=g[x][y];
for(int i=;i<;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(check(nx,ny))
res+=tmp[nx][ny];
}
return res&;
}
int cal(){
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(get(i-,j)){
tmp[i][j]=;
}
}
}
for(int i=;i<m;i++) if(get(n-,i)) return inf;
int res=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
res+=tmp[i][j];
}
}
return res; }
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
scdd(n,m);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
scd(g[i][j]);
}
}
int res=inf;
mset(ans,);
for(int i=;i<(<<m);i++){
mset(tmp,);
for(int j=;j<m;j++){
tmp[][j]= i>>j&;
}
int temp=cal();
if(temp<res){
res=temp;
for(int x=;x<n;x++){
for(int y=;y<m;y++){
ans[x][y]=tmp[x][y];
}
}
}
}
if(res==inf) puts("IMPOSSIBLE");
else{
for(int i=;i<n;i++){
for(int j=;j<m-;j++){
printf("%d ",ans[i][j]);
}
printf("%d\n",ans[i][m-]);
}
}
return ;
}

POJ - 3279 Fliptile (枚举)的更多相关文章

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

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

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

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

  3. POJ 3279 Fliptile(翻格子)

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

  4. POJ 3279(Fliptile)题解

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

  5. POJ 3279 - Fliptile - [状压+暴力枚举]

    题目链接:http://poj.org/problem?id=3279 Sample Input 4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 Sample Output 0 ...

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

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

  7. (简单) POJ 3279 Fliptile,集合枚举。

    Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more ...

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

    <题目链接> <转载于 >>> > 题目大意: 给定一个M*N矩阵,有些是黑色(1表示)否则白色(0表示),每翻转一个(i,j),会使得它和它周围4个格变为另 ...

  9. POJ 3279 Fliptile【枚举】

    题意: 又是农夫和牛的故事...有m*n个黑白块,黑块的背面是白块,白块背面是黑块,一头牛踩一块,则这个块的上下左右的方块都会转动,问至少踩多少块,才会使所有块都变成白色? 分析: 还是开关问题,同样 ...

随机推荐

  1. JavaScript 模拟 HashMap例子

    function map(){    var map = {}; // Map map = new HashMap();        var key = "key";    va ...

  2. php 历史版本下载地址

    PHP 3.* 版本到 7.* 版本下载地址 http://www.php.net/releases/

  3. [转帖]IIS内虚拟站点配置信息说明

    web.config配置详细说明 https://www.cnblogs.com/zhangxiaolei521/p/5600607.html 原作者总结的很详细 但是没有完全的看完 自己对IIS 的 ...

  4. [转帖] cnblog新闻区 “40岁以上的员工,请自觉离开”

    “40岁以上的员工,请自觉离开” 投递人 itwriter 发布于 2018-04-29 22:36 评论(9) 有2733人阅读 原文链接 [收藏] « » “准确地说,华为目前要裁掉的.清退的,是 ...

  5. pandas设置值、更改值

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/24 15:03 # @Author : zhang chao # @Fi ...

  6. React learn path

    React learn path The Road to learn React https://github.com/the-road-to-learn-react https://roadtore ...

  7. JavaScript——AJAX

    AJAX技术是网页构建的必备技能之一,本文希望能帮助大家轻松的学习这项技术 一.什么是ajax? ajax(异步javascript xml) 能够刷新局部网页数据而不是重新加载整个网页. 二.如何使 ...

  8. postgres(pgAdmin) 客户端保存密码

    pgAdmin 大象客户端保存密码后连接服务器,删除掉当前连接,建立一个新的连接不用输入密码也能连接上,其实是客户端保存了密码,让人误以为是空密码可登录.可以通过右键连接,选择重载服务配置,再次连接就 ...

  9. python+django+uwsgi 搭建环境

    第一步: 搭建python环境 最好使用  pyenv可以很好的管理多版本下的python环境 第二步:搭建django环境 使用    pip install django==1.12.1   来安 ...

  10. day28 staticmethod 装饰器

    静态方法装饰器 完全面向对象的程序中,如果存在某个函数既和对象无关也和类无关,就用staticmethod方法, 在调用此方法的时候只需要加上类名即可,说白了就是强行面向对象....把不是对象的方法也 ...