(简单) 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 ...
随机推荐
- poi 合并单元格、设置边框
HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...
- Linux试题及答案
一. 单选题: 1.添加一条静态路由,使到网络196.199.3通过eth2接口出去,用: A. route add -net 196.199.3.0 B. route add -net 196.19 ...
- 深入解析FileInputStream和FileOutputStream
http://swiftlet.net/archives/1363 FileInputStream和FileOutputStream类属于字节类,可以操作任意类型的文件.在数据流的处理过程中,有两种情 ...
- java 环境的配置
JAVA_HOMEC:\Program Files\Java\jdk1.6.0_02 PATHC:\Program Files\Java\jdk1.6.0_02\bin CLASSPATH.;%JAV ...
- Android多线程下安全访问数据库
http://zhiwei.neatooo.com/blog/detail?blog=5343818a9d4869f0310000de github 原文https://github.com/dmyt ...
- Linux 监控文件事件
某些应用程序需要对文件或者目录进行监控,来侦测其是否发生了某些事件.Linux很贴心的为我们提供了inotify API,也是Linux的专有. inotify API 在使用之前一定要有一个inot ...
- dfs和bfs的简单总结
首先是dfs,又名深度优先搜索.看名字就知道,它的核心思想就是一直搜索,先在一条路上面一路撸到底,如果到底没有办法前进了,那么判断是否到达终点,如果没有到达,那么就回溯到之前的点再撸. dfs的要点: ...
- ThinkPHP使用方法
1.下载ThinkPHP模板,整个导入到项目根目录下. 2.修改index.php文件,内容如下: <?php /***临时配置,项目完成开发后,这些配置会取消*******/define('A ...
- PHP无限极分类的几种方法
导读:项目开发,经常栏目要做到无限极分类,几种方法PHP无限极分类的几种方法 复制代码 代码如下:namespace Util;class Category{static public functio ...
- js实现类似于add(1)(2)(3)调用方式的方法
群里有人说实现类似add(1)(2)(3)调用方式的方法,结果马上有人回答: var add = function(a){ return function(b){ return function(c) ...