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. cs231n---语义分割 物体定位 物体检测 物体分割

    1 语义分割 语义分割是对图像中每个像素作分类,不区分物体,只关心像素.如下: (1)完全的卷积网络架构 处理语义分割问题可以使用下面的模型: 其中我们经过多个卷积层处理,最终输出体的维度是C*H*W ...

  2. Javascript中将数字转换为中文的方法

    //js实现将数字1234转化为汉字字符串(一千二百三十四)(或大写汉字壹仟贰佰叁拾肆): /*阿拉伯数字转中文数字 中文数字的特点: 每个计数数字都跟着一个权位,权位有:十.百.千.万.亿. 以“万 ...

  3. js-获取屏幕的中各种宽高

    网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetWid ...

  4. Kendo-Grid for Vue API and Template

    写此博客的原因:在做项目时前端用的vue,后端用的jfinal.在前端veu中调用了kendo grid插件,但是在官方文档中对kendo grid for vue 的api和template都不太详 ...

  5. mysql数据库的水平拆分与垂直拆分

    近端时间在面试,发现很多面试官或者面试都把数据的水平拆分合垂直拆分给搞混了,今天特意写了一篇博客来说说水平拆分和垂直拆分希望对程序猿们有所帮助. 数据库水平与垂直拆分: 垂直(纵向)拆分:是指按功能模 ...

  6. java 计算器

    初识java:利用swing制作一个简单的计算器,仿造window10内置计算器标准模式下的界面. 涉及学习内容: 设置窗口 设计界面按键 设置文本框:只读 String字符串操作:与double类型 ...

  7. 番茄日志发布1.0.3版本-增加Kafka支持

    番茄日志(TomatoLog)能做什么 可能你是第一次听说TomatoLog,没关系,我可以从头告诉你,通过了解番茄日志,希望能帮助有需要的朋友,番茄日志处理将大大降低你采集.分析.处理日志的过程. ...

  8. 记录一则AIX使用裸设备安装OracleRAC的问题

    需求背景:在AIX6.1上安装Oracle 10g RAC,一线工程师反馈节点2运行root脚本无法成功,跟进排查发现实际上底层存储磁盘的准备工作就存在问题. 客户要求底层存储选用裸设备方式,所以必须 ...

  9. C#中的扩展方法(向已有类添加方法,但无需创建新的派生类型)

    C#中的扩展方法 扩展方法使你能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样 ...

  10. Spring学习之旅(一)--初始Spring

    之前从博客.视频断断续续的学到了 Spring 的相关知识,但是都是一个个碎片化的知识.刚好最近在读 <Sprign实战(第四版)>,所以借此机会重新整理下Spring 系列的内容. Sp ...