Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M× N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.

As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.

Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".

Input

Line 1: Two space-separated integers: M and N 

Lines 2.. M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white

Output

Lines 1.. M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.

Sample Input

4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1

Sample Output

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

这个题挺不错,利用了枚举所有情况,枚举的时候利用二进制的性质,然后再暴力各个方向,考虑如果翻转之后对别的有什么影响,如果上一行是黑色,这个必然要翻,再判断一下最后一行是否为全白

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#define Inf 0x3f3f3f3f const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int Map[25][25];
int num[25][25];
int s[25][25];
int dir[5][2]={{0,0},{0,-1},{0,1},{-1,0},{1,0}};
int n,m;
bool check(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
{
return true;
}
else
{
return false;
}
}
int fun(int x,int y)
{
int res=Map[x][y];
for(int t=0;t<5;t++)
{
int xx=x+dir[t][0];
int yy=y+dir[t][1];
if(check(xx,yy))
{
res+=num[xx][yy];
}
}
return res%2;
}
int cal()
{
for(int t=1;t<n;t++)
{
for(int j=0;j<m;j++)
{
if(fun(t-1,j))
{
num[t][j]=1;
}
}
}
for(int t=0;t<m;t++)
{
if(fun(n-1,t))
{
return -1;
}
}
int res=0;
for(int t=0;t<n;t++)
{
for(int j=0;j<m;j++)
{
res+=num[t][j];
}
}
return res;
} int main()
{ cin>>n>>m;
for(int t=0;t<n;t++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&Map[t][j]);
}
}
int ans=Inf;
for(int t=0;t<1<<m;t++)
{
memset(num,0,sizeof(num));
for(int j=0;j<m;j++)
{
num[0][m-1-j]=t>>j&1;
} int cnt=cal();
if(cnt>=0&&cnt<ans)
{
ans=cnt;
memcpy(s,num,sizeof(num));
}
}
if(ans==Inf)
{
cout<<"IMPOSSIBLE\n";
}
else
{
for(int t=0;t<n;t++)
{
for(int j=0;j<m;j++)
{
if(j==m-1)
{
cout<<s[t][j]<<endl;
}
else
{
cout<<s[t][j]<<" ";
}
}
}
}
return 0;
}

Fliptile (dfs+二进制压缩)的更多相关文章

  1. UVA690-Pipeline Scheduling(dfs+二进制压缩状态)

    Problem UVA690-Pipeline Scheduling Accept:142  Submit:1905 Time Limit: 3000 mSec  Problem Descriptio ...

  2. HDU-1074.DoingHomework(撞鸭dp二进制压缩版)

    之前做过一道二进制压缩的题目,感觉也不是很难吧,但是由于见少识窄,这道题一看就知道是撞鸭dp,却总是无从下手....最后看了一眼博客,才顿悟,本次做这道题的作用知识让自己更多的认识二进制压缩,并无其它 ...

  3. poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析

    题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...

  4. poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推

    题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  5. POJ 3279 Fliptile (dfs+二进制)

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

  6. UVa 818 切断圆环链(dfs+二进制枚举)

    https://vjudge.net/problem/UVA-818 题意:有n个圆环,其中有一些已经扣在了一起.现在需要打开尽量少的圆环,使得所有圆环可以组成一条链,例如,有5个圆环,1-2,2-3 ...

  7. uva10160(dfs+状态压缩)

    题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...

  8. Codeforces Round #371 (Div. 2) C. Sonya and Queries —— 二进制压缩

    题目链接:http://codeforces.com/contest/714/problem/C C. Sonya and Queries time limit per test 1 second m ...

  9. 2101 可达性统计(拓扑排序/dfs+状态压缩)

    [题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...

随机推荐

  1. APP前端开发时应注意的一些问题

    在做APP前端开发时应注意的一些问题 在整个app开发流程中,app前端开发是一个必不可少的环节,也是一个在app开发过程中重量级的角色.说到这,那么在app应用的前端开发中,又要注意什么问题呢?一. ...

  2. R语言读取MySQL数据表

    1.R中安装RODBC包 install.packages("RODBC") 2.在Windows系统下安装MySQL的ODBC驱动 注意区分32位和64位版本: http://d ...

  3. selenium使用ChromeDriver

    什么是ChromeDriver? ChromeDriver是Chromium team开发维护的,它是实现WebDriver有线协议的一个单独的服务.ChromeDriver通过chrome的自动代理 ...

  4. Linux 上安装Gearman及其PHP扩展

    安装Gearman服务端 # yum install -y uuid-devel libuuid libuuid-devel uuid boost-devel libevent libevent-de ...

  5. Xcode弱网测试工具

    Network Link Conditioner Network Link Conditioner工具是Mac下提供的一个弱网测试工具. 安装Network Link Conditioner Xcod ...

  6. 编写高质量代码改善C#程序的157个建议——建议29:区别LINQ查询中的IEnumerable<T>和IQueryable<T>

    建议29:区别LINQ查询中的IEnumerable<T>和IQueryable<T> LINQ查询一共提供了两类扩展方法,在System.Linq命名空间下,有两个静态类:E ...

  7. 【JAVA】虚拟机指令集

    [JAVA]虚拟机指令集 – – – 0x00 nop 什么都不做 0x01 aconst_null 将null推送至栈顶 0x02 iconst_m1 将int型-1推送至栈顶 0x03 icons ...

  8. postfix 安装配置详解

    [ref: http://blog.51yip.com/server/1382.html] [http://blog.chinaunix.net/uid-174325-id-1744019.html] ...

  9. Android自动提示控件:AutoCompleteTextView和MultiAutoCompleteTextView

    在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,这种效果在Android中是用AutoCompleteTextView实现的. 一.AutoCompleteTextView:单一匹配 ...

  10. sed 增删改查详解以及 sed -i原理

    我为什么要详细记录sed命令:     sed 擅长取行.工作中三剑客使用频率最高,本篇文章将对sed命令常用的 增,删,改,查 进行详细讲解,以备以后工作中遗忘了查询,sed命令是作为运维人员来说, ...