(简单) 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 ...
随机推荐
- 【0-1 背包模板】 poj 3624
先看个未经优化的二维空间dp: #include <iostream> #include <cstdio> #include <cmath> #include &l ...
- Lua 迭代器
第一种:lua迭代器的实现依赖于闭包(closure)特性 1.1 第一个简单的写法 --迭代器写法 function self_iter( t ) local i = 0 return functi ...
- “strcmp()” Anyone?
“strcmp()” Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two ...
- HTML一些小细节
这里主要是记录一些看起来不重要,但是其实作用不小或者使用起来某种情况下好用的东西,又或者是重要的但容易忽略的基础知识 1. HTML5之后的第一个标签是<!DOCTYPE html> 这个 ...
- lucene-SpanQuery跨度查询基础
1.跨度查询SpanQuery5个子类 SpanQuery类型 描述 SpanTermQuery 和其他跨度查询结合 ...
- java 工作内存
所谓线程的“工作内存”到底是个什么东西?有的人认为是线程的栈,其实这种理解是不正确的.看看JLS(java语言规范)对线程工作 内存的描述,线程的working memory只是cpu的寄存器和高速缓 ...
- 转:loadrunner关联及web_reg_save_param方法浅析
一.什么是关联 关联(correlation):脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关联规则),在服务器所响应的内容中查找,得到相应的值,已变量的形式替换录制时的静态 ...
- listview的条目(item)如何做出卡片效果
卡片,其实就是一张背景图片,但做也还需要注意一点. 错误做法: <?xml version="1.0" encoding="utf-8"?> < ...
- emacs command
eval-buffer用来执行.emacs不要再重启了,或cxce执行光标前的一行 eval-region load-file ~/.emacs goto-line global-set-key定义快 ...
- [jQueryUI] – Chosen:select下拉选择框美化插件及问题
Chosen 是一个支持jquery的select下拉框美化插件,它能让丑陋的.很长的select选择框变的更好看.更方便.不仅如此,它更扩展了select,增加了自动筛选的功能.它可对列表进行分组, ...