(简单) 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 ...
随机推荐
- apicloud教程2 (转载)
本帖最后由 中山赢友网络科技有限公司 于 2015-10-17 15:38 编辑 继<APICloud之小白图解教程系列(一):认识APICloud>之后的第二篇教程. 本篇教程有以下知识 ...
- UITableView的子控件高度不确定处理
比如,tableView的tableFootView的控件数量是根据网络请求的数据而定的.那么tableView并不能准确的设置其contentSize.处理方法: 在tableFootView的类中 ...
- 【转】Java 内部类种类及使用解析
Java 内部类种类及使用解析 内部类Inner Class 将相关的类组织在一起,从而降低了命名空间的混乱. 一个内部类可以定义在另一个类里,可以定义在函数里,甚至可以作为一个表达式的一部分. Ja ...
- HDOJ--ACMSteps--2.1.8--Leftmost Digit-(取对数,数学)
Problem Description Given a positive integer N, you should output the leftmost digit of N^N. Input T ...
- byte 读写文件
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import ja ...
- POJ 2289 Jamie's Contact Groups
二分答案+网络最大流 #include<cstdio> #include<cstring> #include<cmath> #include<vector&g ...
- U3D游戏开发基础
向量: 1. 向量的长度,即向量的模.计算公式为向量各个分量的平方和,然后开平方. 在D3DX库中,方法为:FLOAT D3DXVec3Length(CONST D3DXVECTOR3 * pV) ...
- 第19章 网络通信----TCP程序设计基础
TCP网络程序设计是指利用Socket类编写通信程序.利用TCP协议进行通信的两个应用程序是有主次之分的,一个称为服务器程序,另一个称为客户机程序,两者的功能和编写方法大不一样. 1.InetAddr ...
- MBR区、DBR区、FAT区、DIR区和DATA区的区别
来自:互联网 磁盘上的数据按照其不同的特点和作用大致可分为5部分:MBR区.DBR区.FAT区.DIR区和DATA区.我们来分别介绍一下: (1)MBR区(主引导扇区) MBR(Main Boot R ...
- zf-关于表单不能提交的bug修改
因为使用onclick="submitForm();" 函数提交的 就表示 这里面有js代码 js代码 里面使用document.from1.submit()提交的 所以from ...