Problem Description

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.

SampleInput

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

SampleOutput

0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0 这题也是kuangbin搜索专题里面的一题,题意就是一个n*m的矩阵,0代表灯关,1代表灯开,按其中一个按钮,自身以及上下左右都会变成相反状态,就是和我们玩的关灯游戏一样,问你最少按几次就能全部关掉,多种情况的话输出字典序最小的。
这题目可以用递推的思路,从最上面开始操作,下一行的操作都会由上一行得出,最后我们判断最后一行是否都为0就行了。 而对于第一行来说,最坏一共有2^m种情况,我们只需要枚举每一种情况即可,然后若是有多组,输出字典序小值就好。
其他的话就没什么解释的啦,代码里面我加了注释,直接看代码吧=7=
代码:
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <bitset>
#include <ctime>
#include <fstream>
#include <limits.h>
#include <numeric> using namespace std; #define F first
#define S second
#define mian main
#define ture true #define MAXN 1000000+5
#define MOD 1000000007
#define PI (acos(-1.0))
#define EPS 1e-6
#define MMT(s) memset(s, 0, sizeof s)
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef stringstream sstm;
const int INF = 0x3f3f3f3f; int mp[][],tp[][],s[][];
int n,m;
int fx[][] = {{,},{,},{,-},{,},{-,}}; int fun(int x,int y){ //判断x、y旁边的5个位置的颜色得出x、y位置的颜色
int temp = mp[x][y];
for(int i = ; i < ; i++){
int next_x = x+fx[i][];
int next_y = y+fx[i][]; if(next_x < || next_x > n || next_y < || next_y > m)
continue;
temp += tp[next_x][next_y];
}
return temp%;
} int dfs(){
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
if(fun(i-,j)) //上一行位置灯开状态,此位置就必须开灯使上一行熄灭
tp[i][j] = ; for(int i = ; i <= m; i++)  //最后一行全部为0,直接结束
if(fun(n,i))
return -; int res = ;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)  //得出大小,因为后面会比较字典序
res += tp[i][j];
return res;
} int main(){
ios_base::sync_with_stdio(false);
cin.tie();
cout.tie();
while(cin>>n>>m){
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
cin>>mp[i][j]; int flag = ;
int ans = INF;
for(int i = ; i < <<m; i++){ //枚举第一行的所有状态
memset(tp,,sizeof(tp)); for(int j = ; j <= m; j++)
tp[][m-j+] = i>>(j-) & ;
int cnt = dfs();
if(cnt >= && cnt < ans){  //得出字典序最小的
flag = ;
ans = cnt;
memcpy(s,tp,sizeof(tp));
}
}
if(!flag)
cout<<"IMPOSSIBLE"<<endl;
else{
for(int i = ;i <= n;i ++){
for(int j = ;j <= m;j ++){
if(j != )
cout<<" ";
cout<<s[i][j];
}
cout<<endl;
}
}
}
return ;
}

Fliptile(枚举+DFS)的更多相关文章

  1. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

  2. (POJ-3279)Fliptile (dfs经典---也可以枚举)

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

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

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

  4. UVA818-Cutting Chains(二进制枚举+dfs判环)

    Problem UVA818-Cutting Chains Accept:393  Submit:2087 Time Limit: 3000 mSec  Problem Description Wha ...

  5. POJ 3279 Fliptile(DFS+反转)

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

  6. POJ 1753 (枚举+DFS)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40632   Accepted: 17647 Descr ...

  7. HDU 4770 Lights Against Dudely 暴力枚举+dfs

    又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...

  8. POJ 3050 枚举+dfs+set判重

    思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...

  9. Fliptile POJ-3279 DFS

    题目链接:Fliptile 题目大意 有一个01矩阵,每一次翻转(0->1或者1->0)一个元素,就会把与他相邻的四个元素也一起翻转.求翻转哪些元素能用最少的步骤,把矩阵变成0矩阵. 思路 ...

随机推荐

  1. .Net Mvc过滤器观察者模式记录网站报错信息

    基本介绍: 观察者模式是一种对象行为模式.它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.在观察者模式中,主题是通知的发布者,它发出通知时并不 ...

  2. 90001PS相关操作

    第一章   PS基础操作 1.1 PS界面介绍 (1)界面包含:菜单栏.状态样式栏.工具栏.绘图区域.工作区. (2)布局可以在左上角进行切换,区分主要为工作区的侧重点不同. (3)布局想恢复可在窗口 ...

  3. 12款好用超赞的国外搜索资源网站 ,开发者们的标配,你都知道吗?不知道就OUT了

    简介 看了 看了网上有好多推荐插件的文章,很少有推荐搜索资源网站,于是今天决定推荐一波搜索资源网站.这些网站带给我开阔视眼增长知识.所以在这里整理一下,分享给朋友和博友们. 学习技术过程我们经常需要使 ...

  4. Asp.NetCore源码学习[1-2]:配置[Option]

    Asp.NetCore源码学习[1-2]:配置[Option] 在上一篇文章中,我们知道了可以通过IConfiguration访问到注入的ConfigurationRoot,但是这样只能通过索引器IC ...

  5. 从强转 byte 说起

    折腾的心,颤抖的手,只因在 main 函数中执行了一次 int 强转 byte 的操作,输出结果太出所料,于是入坑,钻研良久,遂有此篇. 我们都知道,Java中有8中基本数据类型,每种类型都有取值范围 ...

  6. 爬虫——网页解析利器--re & xpath

    正则解析模块re re模块使用流程 方法一 r_list=re.findall('正则表达式',html,re.S) 方法二  创建正则编译对象 pattern = re.compile('正则表达式 ...

  7. IdentityServer3学习记录(搭建IdentityServer项目)

    记录下自己尝试搭建identityServer3的过程,便于自己记录遗忘时翻看,也能便于刚接触的新手简单了解下搭建的过程. 更详细的可以参考 https://www.jianshu.com/p/792 ...

  8. win8,右键添加notepad++

    1.下载并安装notepad++ 2.创建txt文件test1.txt,内容如下: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\ ...

  9. Treap + 无旋转Treap 学习笔记

    普通的Treap模板 今天自己实现成功 /* * @Author: chenkexing * @Date: 2019-08-02 20:30:39 * @Last Modified by: chenk ...

  10. HDU 6394 Tree 分块 || lct

    Tree 题意: 给你一颗树, 每一个节点都有一个权值, 如果一个石头落在某个节点上, 他就会往上跳这个的点的权值步. 现在有2种操作, 1 把一个石头放在 x 的位置 询问有跳几次才跳出这棵树, 2 ...