poj 3279 Fliptile(二进制搜索)
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
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
题意:有一个n*m的矩形 里面有0/1矩阵 现在需要翻转(每次上下左右中翻转)成全0的矩阵 问最少需要多少次
思路:二进制枚举第一行的所有翻转情况 然后保证1~(m-1)行没有1出现 这样我们只要遍历最后一行 只要没有1就是一种方案
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-,,};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int m,n;
int G[][];
int temp[][];
int ans[][];
int get(int x,int y){ //计算当前位置是否为1
int c=G[x][y];
for(int i=;i<;i++){
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx>=&&xx<=m&&yy>=&&yy<=n)
c+=temp[xx][yy];
}
return c%;
}
int solve(){
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
if(get(i-,j)) //如果上面一个位置是1 就需要翻转当前位置
temp[i][j]=;
for(int i=;i<=n;i++)
if(get(m,i))
return -inf;
int cnt=;
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
if(temp[i][j]) cnt++;
return cnt;
}
int main(){
ios::sync_with_stdio(false);
while(cin>>m>>n){
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
cin>>G[i][j];
int anss=-inf;
for(int i=;i<(<<n);i++){ //二进制枚举第一行的所有情况
memset(temp,,sizeof(temp));
for(int j=;j<=n;j++)
temp[][j]=(i>>(j-))&;
int te=solve();
if(te>anss){
for(int p=;p<=m;p++)
for(int q=;q<=n;q++)
ans[p][q]=temp[p][q];
anss=te;
break;
}
}
if(anss==-inf) cout<<"IMPOSSIBLE"<<endl;
else{
for(int i=;i<=m;i++){
for(int j=;j<=n;j++)
if(j==) cout<<ans[i][j];
else cout<<" "<<ans[i][j];
cout<<endl;
}
}
}
return ;
}
poj 3279 Fliptile(二进制搜索)的更多相关文章
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- poj 3279 Fliptile (简单搜索)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16558 Accepted: 6056 Descrip ...
- poj 3279 Fliptile(二进制)
http://poj.org/problem?id=3279 在n*N的矩阵上,0代表白色,1代表黑色,每次选取一个点可以其颜色换过来,即白色变成黑色,黑色变成白色,而且其上下左右的点颜色也要交换,求 ...
- POJ 3279 Fliptile[二进制状压DP]
题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...
- POJ 3279 Fliptile (二进制枚举)
<题目链接> <转载于 >>> > 题目大意: 给定一个M*N矩阵,有些是黑色(1表示)否则白色(0表示),每翻转一个(i,j),会使得它和它周围4个格变为另 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile (二进制+搜索)
[题目链接]click here~~ [题目大意]: 农夫约翰知道聪明的牛产奶多. 于是为了提高牛的智商他准备了例如以下游戏. 有一个M×N 的格子,每一个格子能够翻转正反面,它们一面是黑色,还有一面 ...
随机推荐
- awr format
AWR-Format工具 在Chrome高版本中配置使用AWR-Format for Chrome插件
- 372.Definition of ListNode
单项列表只能把后一个node中的所有数据copy到当前node再delete后一node. /** * Definition of ListNode * class ListNode { * publ ...
- Oracle增删改查sql语句
--创建表空间 create tablespace waterboss datafile 'd:\waterboss.dbf' size 100m autoextend on next 10m --创 ...
- day 7-18 mysql case when语句
概述: sql语句中的case语句与高级语言中的switch语句,是标准sql的语法,适用于一个条件判断有多种值的情况下分别执行不同的操作. 首先,让我们看一下CASE的语法.在一般的SELECT中, ...
- Day 5-7 exec 和元类的创建.
exec方法 元类 exec(str_command,globals,locals)参数1:字符串形式的命令参数2:全局作用域(字典形式). 如果不指定,默认globals参数3:局部作用(字典形式) ...
- Yii2后台管理系统常规单据模块最佳实践
后台管理系统的常规单据通常包括数据,页面,功能:其中数据,页面,功能又可以细分如下: 分类 二级分类 主要内容 注意事项 例如 数据 数据库迁移脚本 用于数据表生成及转态回滚 1.是否需要增 ...
- SQL Server中获取指定时间段内的所有月份
例如查询 2012-1-5 到 2012-11-3 之间所有的月份 declare @begin datetime,@end datetime set @begin='2012-1-5' set @e ...
- How to enable usb on vbox
Device-->Install Guest Addition Shared Folders Settings-->Advanced-->Shared Clipboard--> ...
- hdu-4763(kmp+拓展kmp)
题意:给你一个串,问你满足最大字串既是前后缀,也在字符串除去前后缀的位置中出现过: 思路:我用的是拓展kmp求的前后缀,只用kmp也能解,在字符串2/3的位置后开始遍历,如果用一个maxx保存前2/3 ...
- 【数学建模】day08-数理统计III
2. 回归分析 回归分析与曲线拟合区分. 曲线拟合是,根据得到的若干有关变量的一组数据,寻找因变量与(一个或几个)自变量之间的一个函数,使这个函数对那组数据拟合得好.通常,函数的形式可以由经验.先验知 ...