(简单) POJ 3279 Fliptile,集合枚举。
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".
题目大意就是说一个棋盘的黑白棋,反转一个周围的也会反转,然后问都变成0的最小次数的时候,每一个分别反转多少次。
首先应该想到说每一个最多反转1次,因为反转2次和0次是等价的,所以说每一个就是0或1了。
然后就是说第一行确定了的话第二行也会确定下来,因为第一行某一个的上左右都确定下来了,所以下也会确定,所以可以通过第一行推出之后的所有。
然后就是第一行的问题了。
N和M都小于等于15,2^N不会超时。直接枚举第一行就行。
代码如下:
#include<iostream>
#include<cstring>
#include<cmath> using namespace std; int ans[][];
int map1[][];
int lasans[][];
int M,N;
int minans; int getdata()
{
for(int i=;i<=M;++i)
for(int j=;j<=N;++j)
cin>>map1[i][j];
} inline void slove()
{
minans=1e8;
int L=(<<N);
int rem;
bool ok; for(int i=;i<L;++i)
{
rem=; for(int j=;j<=N;++j)
if((<<(j-))&i)
{
ans[][j]=;
++rem;
}
else
ans[][j]=; for(int j=;j<=M;++j)
{
for(int k=;k<=N;++k)
{
if((map1[j-][k]+ans[j-][k]+ans[j-][k]+ans[j-][k-]+ans[j-][k+])%)
{
ans[j][k]=;
++rem;
if(rem>minans)
goto next1;
}
else
ans[j][k]=;
}
} ok=;
for(int j=;j<=N;++j)
if((map1[M][j]+ans[M][j]+ans[M-][j]+ans[M][j-]+ans[M][j+])%)
{
ok=;
break;
} if(ok)
if(rem<minans)
{
minans=rem;
for(int i=;i<=M;++i)
for(int j=;j<=N;++j)
lasans[i][j]=ans[i][j];
} next1:;
}
} int main()
{
ios::sync_with_stdio(false); while(cin>>M>>N)
{
memset(ans,,sizeof(ans));
memset(map1,,sizeof(map1)); getdata();
slove(); if(minans==1e8)
cout<<"IMPOSSIBLE\n";
else
for(int i=;i<=M;++i,cout<<endl)
for(int j=;j<=N;++j)
cout<<lasans[i][j]<<' ';
} return ;
}
(简单) POJ 3279 Fliptile,集合枚举。的更多相关文章
- POJ - 3279 Fliptile (枚举)
http://poj.org/problem?id=3279 题意 一个m*n的01矩阵,每次翻转(x,y),那么它上下左右以及本身就会0变1,1变0,问把矩阵变成全0的,最小需要点击多少步,并输出最 ...
- POJ 3279 Fliptile (二进制枚举)
<题目链接> <转载于 >>> > 题目大意: 给定一个M*N矩阵,有些是黑色(1表示)否则白色(0表示),每翻转一个(i,j),会使得它和它周围4个格变为另 ...
- POJ 3279 Fliptile【枚举】
题意: 又是农夫和牛的故事...有m*n个黑白块,黑块的背面是白块,白块背面是黑块,一头牛踩一块,则这个块的上下左右的方块都会转动,问至少踩多少块,才会使所有块都变成白色? 分析: 还是开关问题,同样 ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- 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 Sample Input 4 4 1 0 0 1 0 1 1 0 0 1 1 0 1 0 0 1 Sample Output 0 ...
随机推荐
- OpenGL—Android 开机动画源码分析二
引自http://blog.csdn.net/luoshengyang/article/details/7691321/ BootAnimation类的成员函数的实现比较长,我们分段来阅读: 第三个开 ...
- 修改apache配置文件去除thinkphp url中的index.php
例如你的原路径是 http://localhost/test/index.php/index/add那么现在的地址是 http://localhost/test/index/add如何去掉index. ...
- 什么是dtd文件,为什么需要
DTD为英文Document Type Definition,中文意思为“文档类定义”.DTD肩负着两重任务:一方面它帮助你编写合法的代码,另一方面它让浏览器正确地显示器代码.也许你会问它们居然有这样 ...
- NSAttributedString in Swift
转载自: https://www.invasivecode.com/weblog/attributed-text-swift/ I have been talking quite a lot in ...
- 命令行从Android手机中导出已安装APK的方法调研
一.背景 二.步骤 一.背景 很多时候,APK文件只存在于应用市场,在PC上无法直接下载.用手机下载下来后就直接安装了,也不能保存原始的APK文件. APK安装到手机后,Android系统会保存一份和 ...
- 《割绳子》《蜡笔物理学》《Contre Jour》《顽皮鳄鱼爱洗澡》等游戏用Box2D引擎实现物理部分的方法(转)
从最热门游戏排行榜和flash游戏网站上,你能看到什么?许多2D游戏都有非常出色的物理学和美术设计.现在我们要学习那些游戏使用了什么物理学以及如何用Box2D制作它们. 除了知道是“什么”,更重要的是 ...
- 2015年4月29日 dayofweek
#include <stdio.h>#include <stdlib.h>int DayofYear(int year, int month, int day);#define ...
- make -jN
今天又一次尝试编译安卓,想测试一下编译的速度如何? 考虑机器是4核8线程,就用上了 make -j8,感觉上上速度是很快,刷屏就下来了,不过错误了,错误的提示大概是某个文件的规则没找到,想想了多线程并 ...
- gen_grant_sel.sql
set echo off feedback off verify off pagesize 0 linesize 120 define v_grantee=&1 define v_grant_ ...
- jQuery常用及基础知识总结(二)
JQuery Effects 方法说明 show( ) 显示隐藏的匹配元素.show( speed, [callback] ) 以优雅的动画显示所有匹配的元素,并在显示完成后可选地触发一个回调函数.h ...