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更好写而且应 ...
随机推荐
- ubuntu 下正确安装android手机驱动
1. 查看手机ID号. charlesxue@THSHIBA:~/setup/cocos2d-x/cocos2d-x-/projects/simpleGame/proj.android/bin$ ls ...
- 银行家算法之JavaScript实现
上学期有个课程叫做操作系统,期末的时候这课程还有个课程设计,其中有个题目叫做银行家算法. 什么是银行家算法我就不解释了! 看着同学们的设计,大同小异甚至前篇一律. 清一色的控制台程序,清一色的蛋疼输入 ...
- Luogu 3206 [HNOI2010]城市建设
BZOJ 2001 很神仙的cdq分治 先放论文的链接 顾昱洲_浅谈一类分治算法 我们考虑分治询问,用$solve(l, r)$表示询问编号在$[l, r]$时的情况,那么当$l == r$的时候 ...
- hdu 1905 Pseudoprime numbers
#include<stdio.h> #include<math.h> #define ll long long ll mod; bool Judge(int x) { ;i&l ...
- Sqlserver中的几把锁和.net中的事务级别
当数据表被事务锁定后,我们再进行select查询时,需要为with(锁选项)来查询信息,如果不加,select将会被阻塞,直到锁被释放,下面介绍几种SQL的锁选项 SQL的几把锁 NOLOCK(不加锁 ...
- Customizing Site-Wide Behavior for ASP.NET Web Pages (Razor) Sites
Customizing Site-Wide Behavior for ASP.NET Web Pages (Razor) Sites By Tom FitzMacken|February 17, 20 ...
- BZOJ 3083 遥远的国度(树链剖分+LCA)
Description 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要z ...
- adb命令安装及卸载应用
一.手机连接电脑,检测手机是否已开启授权并连接成功 adb devices 二.安装应用 adb install UYUN-CARRIER-Android.apk 三.卸载应用 1.查看应用包名 ad ...
- window7 下配置python2.7+tornado3.3开发环境
玩python的人大都在linux下进行开发,由于长期习惯在windows下开发代码,今天蛋疼尝试在window7下配置python2.7+tornado3.3开发环境,必然的中间遇到各种报错,但是最 ...
- Tomcat调优总结
Tomcat 优化分为系统优化,Java虚拟机调优,Tomcat本身的优化. Tomcat 如何起停 ./catalina.sh stop ./catalina.sh start/sbin/servi ...