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矩阵. 思路 ...
随机推荐
- 颜色下拉菜单(combox)
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...
- .net测试篇之测试神器Autofixture Generator使用与自定义builder
有了上一节自定义配置,很多问题都能解决了,但是如果仅仅是为了解决一个简单问题那么创建一个类显得有点繁重.其实AutoFixture在创建Fixture对象时有很多方便的Fluent配置,我们这里介绍一 ...
- @WebInitParam注解
Servlet注解——@WebInitParam多个InitParam的写法 使用@WebInitParam配置多个InitParam,使某些页面不被拦截.在过滤器Filter.java下添加注解:@ ...
- C#代码实现IoC(控制反转)设计,以及我对IoC的理解
一. 什么是IoC 当在A类中要使用B类的时候,我们一般都是采用new的方式来实例化B类,这样一来这两个类就有很强的依赖关系,不符合低耦合的设计思想.这时候我们可以通过一个中间容器来实例化对象,需要的 ...
- JAVA 泛型中的通配符 T,E,K,V,?
前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许开发者在编译时检测到非法的类型. 泛型的本质是参数化类型,也就是说所操作的数据 ...
- Git学习笔记-相关命令记录
内容来自:https://www.liaoxuefeng.com/wiki/896043488029600/896067074338496 1.Linux安装Git 首先,你可以试着输入git,看看系 ...
- 上海学生事务中心&新华路派出所的位置
上海学生事务中心: 地址:冠生园路401号 工作时间:9:00-17:00 电话:021-64829191 新华路派出所: 地址:新华路590弄21号 工作时间:周一至周六 9:00 ...
- lightoj 1060 - nth Permutation(组合数+贪心)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1060 题解:如果是不重复数的这些操作可以用康托展开的逆来求,如果是有重复数字出 ...
- Convolution model by吴恩达
# GRADED FUNCTION: model def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.009, num_epoc ...
- Node基础-CommonJS模块化规范
1.在本地项目中基于NPM/YARN安装第三方模块 第一步:在本地项目中创建一个"package.json"的文件 作用:把当前项目所有依赖的第三方模块信息(包含:模块名称以及版本 ...