POJ - 3279 Fliptile (枚举)
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 (枚举)的更多相关文章
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- 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 ...
- POJ 3279 Fliptile(反转 +二进制枚举)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13631 Accepted: 5027 Descrip ...
- (简单) POJ 3279 Fliptile,集合枚举。
Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more ...
- POJ 3279 Fliptile (二进制枚举)
<题目链接> <转载于 >>> > 题目大意: 给定一个M*N矩阵,有些是黑色(1表示)否则白色(0表示),每翻转一个(i,j),会使得它和它周围4个格变为另 ...
- POJ 3279 Fliptile【枚举】
题意: 又是农夫和牛的故事...有m*n个黑白块,黑块的背面是白块,白块背面是黑块,一头牛踩一块,则这个块的上下左右的方块都会转动,问至少踩多少块,才会使所有块都变成白色? 分析: 还是开关问题,同样 ...
随机推荐
- Delphi/XE2 使用TIdHttp控件下载Https协议服务器文件[转]
之前的一篇博文详细描述了使用TIdhttp控件下载http协议的文件,在我项目的使用过程中发现对于下载Https协议中的文件与Http协议的文件不同,毕竟Https在HTTP协议基础上增加了SSL协议 ...
- Zabbix的简单使用
0. 卸载mariadb 安装mysql 方法 rpm -qa |grep mariadb 然后 rpm -e --nodeps mariadb***** 安装mysql # 下载mysql源安装包 ...
- bat脚本的写法
当你每次都要输入相同的命令时,可以把这么多命令存为一个批处理,从此以后,只要运行这个批处理,就相当于打了几行.几十行命令.下面以Nginx服务的停止脚本为例写一个bat批处理文件: 1.新建nginx ...
- p3c安装使用 编码规范扫描 阿里巴巴出品,挺好用的
https://github.com/alibaba/p3c/wiki/IDEA%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3
- Test Scenarios for sending emails
(test cases for composing or validating emails are not included)(make sure to use dummy email addres ...
- html DOM 方法和屬性
html利用javascript對節點執行動作: 每一個節點是一個節點對象,對節點的動作是通過方法和屬性接口實現的: 方法:就是執行的動作: 屬性就是節點的屬性(包括設置和獲取): 常見方法和作用: ...
- Highcharts之3D柱状图
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Android x86 下运行纯ARM版APP
Android x86 默认不带houdini,运行纯ARM版会提示: 很抱歉,”xxxx”已停止运行 设置->应用兼容性->打开 终端模拟器 $ su# enable_nativebri ...
- 与spring整合就是为了不用自己创建bean 让spring帮助我们创建bean
与spring整合就是为了不用自己创建bean 让spring帮助我们创建bean
- poj3320(尺取法)
题目大意:给你一串数字,找出最小的能够覆盖所有出现过的数字的区间长度: 解题思路:依旧是尺取法,但要用map标记下出现过的书: 代码:别用cin输入: #include<iostream> ...