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. ASP.NET Web Service 标准SOAP开发案例代码(自定义验证安全头SOAPHeader)

    using System.Xml;using System.Xml.Serialization;using System.Web.Services.Protocols;using System.Con ...

  2. [转载]以及部分总结--Linux下创建单机ASM存储的Oracle实例的过程---感谢方总

    Linux下单机安装ASM流程总结 一.安装Linux ESXi上传iso镜像至存储目录 创建虚拟机,并且选择主机设备的ISO启动 选择完成时编辑虚拟机设置 配置镜像文件如下: 打开控制台: 并且选择 ...

  3. 远程连接db2数据库

    在db2数据库中,需要理解catalog(编目)这个概念,理解前先说下db2数据库的体系结构:由系统(节点)也就是主机,下面是实例,实例下面是数据库,然后是表空间,再是数据库对象.现在假设你有一个数据 ...

  4. 剑指offer:滑动窗口的最大值

    滑动窗口的最大值 题目描述 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值 ...

  5. flask+mako+peewee(上)

    其实关于什么用flask搭建一个后台博客啥的跟着官方文档做一遍就行了.感觉啥都有我这里就不赘述了只是记录一个笔记,因为稍微有几个地方有点坑. 目标:做了一个简易页面给电商的同事用来添加商品 首先是安装 ...

  6. rsync实现数据同步

    希望两台机器指定目录的数据保持一致 192.168.19.252(master)                 192.168.19.251(slave) /cache 拉复制            ...

  7. Java之Date Time API (Java 8 新特性)

    Java 8 – Date Time API Java 8 comes with a much improved and much required change in the way date an ...

  8. VMware Linux虚拟机yum源更换成国内阿里源

    [虚拟机系统] Centos 6.8 阿里源:https://opsx.alibaba.com/mirror 网易源:http://mirrors.163.com/ 更换yum源时请保证虚拟机和外网是 ...

  9. ajax 调用 java webapi 多个参数(二)

    第一种方法:http://blog.csdn.net/hanjun0612/article/details/74436273 附上另一种解决方法. 这个方法主要针对  嵌套模型(模型中含有模型)的ap ...

  10. TiKV 源码解析系列文章(三)Prometheus(上)

    本文为 TiKV 源码解析系列的第三篇,继续为大家介绍 TiKV 依赖的周边库 rust-prometheus,本篇主要介绍基础知识以及最基本的几个指标的内部工作机制,下篇会介绍一些高级功能的实现原理 ...