POJ 3279 Fliptile (dfs+二进制)
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.
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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define inf 0x3f3f3f3f
int map[][],flip[][],ans[][],preans[][];
//preans表示中间的得到的可行的答案,每次比较步数取最优
int n,m;
int calc (int s)
{
for (int i=;i<m;++i)
{
if (s&(<<i))
{
preans[][i]=;
flip[][i]^=;//x^=1由于异或特性完美模拟翻块!!!0^1=1,1^1=0
if (<n) flip[][i]^=;//如果其他相邻块在界内,翻过去
if (i->=) flip[][i-]^=;
if (i+<m) flip[][i+]^=;
}
}
for (int i=;i<n;++i)
{
for (int j=;j<m;++j)
{
if (flip[i-][j])//如果某个块的正上面的块是1,那么反转这个块
{
preans[i][j]=;
flip[i][j]^=;
flip[i-][j]^=;
if (i+<n) flip[i+][j]^=;
if (j->=) flip[i][j-]^=;
if (j+<m) flip[i][j+]^=;
}
}
}
for (int i=;i<m;++i)
{
if (flip[n-][i])
return inf;
}
int res=;//统计当前情况的步数
for (int i=;i<n;++i)
for (int j=;j<m;++j)
res+=flip[i][j];
return res;
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&m))
{
for (int i=;i<n;++i)
for (int j=;j<m;++j)
cin>>map[i][j];
int res=inf;
memset(ans,,sizeof ans);
for (int i=;i< <<m;++i)//用二进制的方法表示状态
{
memcpy(flip,map,sizeof map);
memset(preans,,sizeof preans);
int temp=calc(i);
if (temp<res)//更新最少的ans状态
{
res=temp;
for (int i=;i<n;++i)
for (int j=;j<m;++j)
ans[i][j]=preans[i][j];
}
}
if (res==inf)
{
printf("IMPOSSIBLE\n");
}
else
{
for (int i=;i<n;++i)
{
for (int j=;j<m;++j)
{
printf("%d",ans[i][j]);
if (j!=m-)
printf(" ");
}
printf("\n");
}
}
}
return ;
}
1 0 0 1
1 0 0 1
0 0 0 0 讲道理这个题感觉不算dfs,算了无所谓。
题意就是给你一个n*m的图,1表示黑色,0表示白色,你可以翻转一些块,是这个块和它周围的块(上下左右,如果不出界)1变成0,0变成1。
问最少几步,能全变成0,输出翻转了那些块,否则IMPOSSIBLE.
讲一下思路,枚举第一行状态每个块都有翻与不翻两种情况,一共1<<m种状态。
对于上述每一种状态,接下来怎么做?考虑到每翻转一个块,它的上下左右块都受到影响,先按照这个翻转策略:如果这个块的上面的那个块是1那么
就翻转这个块。这样思考每一次都能解决上一行的问题。我们最后只要看一下最后一行是否都是0就行了!
PS:这个题一开始写了发600+ms,结果看还有70ms过的,打开看看用了异或1来模拟状态,后来改了改200+ms过的。
代码如下:
POJ 3279 Fliptile (dfs+二进制)的更多相关文章
- POJ 3279 Fliptile (二进制+搜索)
[题目链接]click here~~ [题目大意]: 农夫约翰知道聪明的牛产奶多. 于是为了提高牛的智商他准备了例如以下游戏. 有一个M×N 的格子,每一个格子能够翻转正反面,它们一面是黑色,还有一面 ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 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(二进制)
http://poj.org/problem?id=3279 在n*N的矩阵上,0代表白色,1代表黑色,每次选取一个点可以其颜色换过来,即白色变成黑色,黑色变成白色,而且其上下左右的点颜色也要交换,求 ...
- POJ 3279 Fliptile(DFS+反转)
题目链接:http://poj.org/problem?id=3279 题目大意:有一个n*m的格子,每个格子都有黑白两面(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(反转 +二进制枚举)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13631 Accepted: 5027 Descrip ...
随机推荐
- php str_split()函数 语法
php str_split()函数 语法 str_split()函数怎么用 php str_split()函数用于把字符串分割到数组中,语法是str_split(string,length),将字符串 ...
- iptables防火墙相关命令详解
前提基础: 当主机收到一个数据包后,数据包先在内核空间中处理,若发现目的地址是自身,则传到用户空间中交给对应的应用程序处理,若发现目的不是自身,则会将包丢弃或进行转发. iptables实现防火墙功能 ...
- 在Windows系统使用Gpg4win进行加密解密
GPG,又称为GnuPG,全称是Gnu Private Guard,即GNU隐私卫士.GPG是以PGP算法为核心的强大的加密软件.但GPG项目是一套命令行程序,而且是为 Linux 等开源操作系统设计 ...
- [CSP-S模拟测试]:chinese(数学)
题目传送门(内部题25) 输入格式 一行三个整数$n,m,k$. 输出格式 一行一个整数表示答案. 样例 样例输入: 2 2 2 样例输出: 数据范围与提示 样例解释: $f_0=10,f_1=4,f ...
- [CSP-S模拟测试]:platform(后缀数组+二分+线段树)
题目传送门 题目描述 走过奈何桥有一个名叫望乡台的土台,望乡台有个名曰孟婆的老妇人在卖孟婆汤.一生爱恨情仇,一世浮沉得失,都可以随这碗孟婆汤遗忘得干干净净.现在有$n$碗孟婆汤摆成一排,汤的品种不超过 ...
- php面试专题---4、流程控制考点
php面试专题---4.流程控制考点 一.总结 一句话总结: 理解循环内部机制(指针操作),更易于记忆foreach的reset特性,分支结构中理解了switch...case的执行步骤(跳转表)也就 ...
- play framework 在idea简单运行配置(mac为例)
文章目录 play 最基本的构建 在idea中配置 配置jdk相关 配置play 运行 运行 play 最基本的构建 https://blog.csdn.net/dataiyangu/article/ ...
- MultipartFile 图片上传到Linux服务器Tomcat下的webapps目录
第一次接触 linux 服务器,做图片上传的时候遇到了些坑,搞了些天总算成功了,记录一下 /** * 上传图片 * * @param request * @param file * @return * ...
- LeetCode 102. Binary Tree Level Order Traversal 动态演示
按层遍历树,要用到queue class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { ...
- HBase最佳实践-读性能优化策略
任何系统都会有各种各样的问题,有些是系统本身设计问题,有些却是使用姿势问题.HBase也一样,在真实生产线上大家或多或少都会遇到很多问题,有些是HBase还需要完善的,有些是我们确实对它了解太少.总结 ...