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更好写而且应 ...
随机推荐
- 【总结整理】关于房产app的比较
从切换城市的分类方式就能看出来,因覆盖城市很多,搜房网(房天下)跟安居客都用上了拼音选房,而链家因城市很少,只需简单罗列即可. 搜房网(房天下)覆盖城市多达651个,覆盖范围最广,安居客为500个,两 ...
- maven安装错误履历
1\:maven cannot find entry:"/src/main/java" 先删除source下的文件夹 再新建文件夹
- IDEA03 连接数据库、自动生成实体类
1 版本说明 JDK:1.8 MAVEN:3.5 SpringBoot:2.0.4 IDEA:旗舰版207.2 MySQL:5.5 2 利用IDEA连接数据库 说明:本案例以MySQL为例 2.1 打 ...
- 一个可用模板的Xml描述
<?xml version="1.0" encoding="utf-8"?> <VMTEMPLATE> <ID>48< ...
- Solidity 没名字的function(){...}作用
官方解释: 这个叫做fallback function,当有人 1. 只发送以太币给合约而不带任何输入数据:2. 调用smart contract时调起了一个不存在的方法.会触发执行这个方法. Wha ...
- Linux扩展根目录
一.简介 使用linux系统的过程中,有时发现系统根目录(/)的空间不足,导致系统运行很慢,针对该现象,本文详细介绍根目录(/)的空间扩展方法. 二.操作步骤 1)查看根目录大小 df 2)查找系 ...
- export default {} 和new Vue()区别
1.export default 的用法:相当于提供一个接口给外界,让其他文件通过 import 来引入使用. 而对于export default 和export的区别: 在JavaScript ...
- python3--列表生成式
# Auther: Aaron Fan # 原始的写法:a = []for i in range(10): a.append(i*2)print(a) # 用列表生成式完成上面的写法:a = [i*2 ...
- Entity Framework 6.0 Tutorials(3):Code-based Configuration
Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can c ...
- delphi窗体启动外部exe
uses Winapi.Windows; WinExec(PAnsiChar(Application.ExeName), sw_normal); // PAnsiChar : string to ...