Fliptile (dfs+二进制压缩)
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+二进制压缩)的更多相关文章
- UVA690-Pipeline Scheduling(dfs+二进制压缩状态)
Problem UVA690-Pipeline Scheduling Accept:142 Submit:1905 Time Limit: 3000 mSec Problem Descriptio ...
- HDU-1074.DoingHomework(撞鸭dp二进制压缩版)
之前做过一道二进制压缩的题目,感觉也不是很难吧,但是由于见少识窄,这道题一看就知道是撞鸭dp,却总是无从下手....最后看了一眼博客,才顿悟,本次做这道题的作用知识让自己更多的认识二进制压缩,并无其它 ...
- poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析
题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...
- poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推
题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 3279 Fliptile (dfs+二进制)
Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more ...
- UVa 818 切断圆环链(dfs+二进制枚举)
https://vjudge.net/problem/UVA-818 题意:有n个圆环,其中有一些已经扣在了一起.现在需要打开尽量少的圆环,使得所有圆环可以组成一条链,例如,有5个圆环,1-2,2-3 ...
- uva10160(dfs+状态压缩)
题意:给出n个点,以及m条边,这些边代表着这些点相连,修一个电力站,若在某一点修一个站,那么与这个点相连的点都可以通电,问所有的点都通电的话至少要修多少个电力站........ 思路:最多给出的是35 ...
- 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 ...
- 2101 可达性统计(拓扑排序/dfs+状态压缩)
[题目描述] 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. [题目链接] 2101 可达性统计 [算法] 拓扑排序之后逆序计算(感觉dfs更好写而且应 ...
随机推荐
- jQuery div鼠标移动效果
<head runat="server"> <meta http-equiv="Content-Type" content="tex ...
- Mybatis——缓存机制
MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制.缓存可以极大的提升查询效率. MyBatis系统中默认定义了两级缓存. 一级缓存和二级缓存. 1.默认情况下,只有一级缓存( ...
- 在线破解PDF
在线破解PDF密码的6个网站 有很多PDF文件只能查看却不能被编辑和打印,因为它们已被保护.你并不知道被保护的PDF文档的密码却又急着向上司交差,怎么办?让 Ap PDF Password Recov ...
- js 清空 input[type=file]的值
js 不能操作 input[type=file]但你可以将这个 input 的 dom 元素删除掉,再新增一个,或者替换掉 $("#UploadFile").replaceWith ...
- myeclipse自动化提示
jsp自动提示:1.快捷键提示代码 window-->Preferences的General-->Keys下修改Content Assist的快捷键为Alt+/,这样就可以通过快捷键得到提 ...
- reportng定制修改
定制目的 最近接口测试和UI自动化测试都有用到reportng来做测试报告的展示,发现了几个不是很方便的地方: 报告没有本地化的选项 主页的测试结果显示的不够清晰 测试详情中的结果是按照名称排列的,想 ...
- python核心编程第5章课后题答案
5-8Geometry import math def sqcube(): s = float(raw_input('enter length of one side: ')) print 'the ...
- (转)不定义JQuery插件,不要说会JQuery
原文地址:http://www.cnblogs.com/xcj26/p/3345556.html 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#" ...
- ubuntu 14.04 x64下安装libreoffice
LibreOffice是ubuntu 上的办公软件很多人都知道微软公司的的Word.Excel.PowerPoint和Outlook,但是很少有人知道LibreOffice. LibreOffice靠 ...
- 【C#】MVC+EF+LINQ 综合小项目
第一,创建数据库 create table category(id int primary key,name nvarchar(20)) create table news(id int primar ...