Fliptile(枚举+DFS)
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
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
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)的更多相关文章
- 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 ...
- (POJ-3279)Fliptile (dfs经典---也可以枚举)
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He ha ...
- poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析
题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...
- UVA818-Cutting Chains(二进制枚举+dfs判环)
Problem UVA818-Cutting Chains Accept:393 Submit:2087 Time Limit: 3000 mSec Problem Description Wha ...
- POJ 3279 Fliptile(DFS+反转)
题目链接:http://poj.org/problem?id=3279 题目大意:有一个n*m的格子,每个格子都有黑白两面(0表示白色,1表示黑色).我们需要把所有的格子都反转成黑色,每反转一个格子, ...
- POJ 1753 (枚举+DFS)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40632 Accepted: 17647 Descr ...
- HDU 4770 Lights Against Dudely 暴力枚举+dfs
又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...
- POJ 3050 枚举+dfs+set判重
思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...
- Fliptile POJ-3279 DFS
题目链接:Fliptile 题目大意 有一个01矩阵,每一次翻转(0->1或者1->0)一个元素,就会把与他相邻的四个元素也一起翻转.求翻转哪些元素能用最少的步骤,把矩阵变成0矩阵. 思路 ...
随机推荐
- ZooKeeper实现同步屏障(Barrier)
按照维基百科的解释:同步屏障(Barrier)是并行计算中的一种同步方法.对于一群进程或线程,程序中的一个同步屏障意味着任何线程/进程执行到此后必须等待,直到所有线程/进程都到达此点才可继续执行下文. ...
- Appium+python自动化(三十三)- 环境(超详解)
简介 在前边所有涉及启动app的时候有这样一行代码driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps),很多小 ...
- git 常规业务场景 使用
一般每个开发者都会有个自己的分支,有个test分支,合并代码用,两条分支相互备份,就算merge的时候被覆盖,也不用担心 建立自己的分支 // 创建本地分支, git checkout -b dev_ ...
- Redis集群环境下的键值空间监听事件实现方案
一直想记录工作中遇到的问题和解决的方法,奈何没有找到一方乐土,最近经常反思,是否需要记录平时的点滴,后台还是决定下定决心记录一些,以便以后用到的时候找不着,实现这样的一个功能主要也是业务所需要的. 需 ...
- Nacos(六):多环境下如何“管理”及“隔离”配置和服务
前言 前景回顾: Nacos(五):多环境下如何"读取"Nacos中相应环境的配置 Nacos(四):SpringCloud项目中接入Nacos作为配置中心 现如今,在微服务体系中 ...
- pyhton介绍、发展趋势、安装
pyhton介绍.发展趋势.安装 一.python起源 pyhton的创始人为吉多·范罗苏姆(Gudio van Rossum) (后文简称龟叔) 1. 1989年的圣诞节期间,龟叔为了在阿姆 ...
- 使用Mybatis-Generator 自动生成代码
前提:已经有SpringBoot的工程,且在上面实现了MyBatis的应用,只不过全是以手动方式创建mapper.xml映射文件.pojo类等. 在POM中添加MyBatis.generator依赖 ...
- JDBC之LOB数据类型
JDBC之LOB数据类型 一.Oracle LOB LOB,即Large Objects(大对象),是用来存储大量的二进制和文本数据的一种数据类型(一个LOB字段可存储可多达4GB的数据). LOB ...
- 表单模糊查询的三种简单方式(springboot-h2-mybatis)
前几天运营提到说后台管理系统有几个地方想要模糊查询.. 想了下是简单的,就是要注意以前方法的被调用情况,进行增量改动,以免牵一发而动全身.整理一波记录下(本次案例是按名字模糊查询学生信息). 三种 ...
- hdu 5903 Square Distance(dp)
Problem Description A string is called a square string if it can be obtained by concatenating two co ...