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".
Input
Line 1: Two space-separated integers: M and N
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
Lines 1..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
1 0 0 1
1 0 0 1
0 0 0 0
题意
有一个N×M的格子,每个格子可以翻转正反面,它们一面是黑色,另一面是白色。黑色的格子翻转后就是白色,白色的格子翻转过来就是黑色。现在要把所有的格子都翻转成白色。每次翻转一个格子时,与它上下左右相邻接的格子也会被翻转。求出把所有格子翻转成白色的最可能少的次数。如果这种方案有多个的话,输出字典序最小的一组。解不存在的话,就输出“IMPOSSIBLE”。
分析:
首先,同一个格子翻转两次的话就会恢复原状,所以多次翻转是多余的。此外,翻转的格子的集合相同的话,其次序是无关紧要的。因此,总共有2^N×M中翻转的方法。但是这个解的空间太大了。
回顾前面的一个问题。在这道题中,让最左端的牛翻转的方法只有一种,于是用直接判断的方法确定就可以了。我们看一下同样的方法在这里能不能行得通?
不妨先看看最左上角的格子。在这里,除了翻转(1,1)之外,翻转(1,2)和(2,1)也可以吧这个格子翻转,所以想之前那样直接确定的方法行不通。
于是不妨先指定好最上面一层的翻转方法。此时能够翻转(1,1)的只剩下(2,1)了,所以可以直接判断(2,1)是否需要翻转。类似地(2,1)(2,N)都能够这样判断,如此反复下去就可以确定所有格子的翻转方法。最后(M,1)(M,N)如果并非全为白色,就意味着存在不可行的操作方法。
像这样,先确定第一行的翻转方式,然后可以很容易判断这样是否存在解以及解的最小步数是多少,这样将第一行的所有翻转方式都尝试一次就能求出整个问题的最小步数。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 300
int dx[]= {-1,0,0,0,1};
int dy[]= {0,1,0,-1,0};
int map[15][15],flip[15][15],ans[15][15];
int m,n;
int get(int x,int y)///(x,y)的颜色,这个可以根据map[x][y]本身的颜色以及周围的翻转情况来确定
{
int res=map[x][y];
for(int i=0; i<5; i++)///便利本身以及上下左右四个方向的翻转情况
{
int a=x+dx[i],b=y+dy[i];
if(a>=0&&a<m&&b>=0&&b<n)
{
res+=flip[a][b];
}
}
return res&1;
}
int calc()///求出第一行确定的情况下的最小操作次数,
{
for(int i=1; i<m; i++)
{
for(int j=0; j<n; j++)
{
if(get(i-1,j))///(i-1,j)如果上面的那块是黑色的话,本身就肯定要被上面的那个影响而进行翻转的
{
flip[i][j]=1;///标记本身需要翻转
}
//printf("%d ",flip[i][j]);
}
//printf("\n");
}
// printf("\n");
for(int i=0; i<n; i++) ///无解,只要最后一行的存在黑色
{
if(get(m-1,i)) return INF;
}
int res=0;///整个图中的翻转次数
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
res+=flip[i][j];
return res;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
printf("%d\n",1<<n);
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
scanf("%d",&map[i][j]);
int res=INF;
memset(ans,0,sizeof(ans));
for(int i=0; i< 1<<n; i++) ///枚举第一行的所有翻转情况
{
memset(flip,0,sizeof(flip));
for(int j=0; j<n; j++)
{
flip[0][j]= i>>j&1;///在i的状态下,第一行的j是否翻转
}
int temp=calc();
if(temp<res)
{
res=temp;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++) ans[i][j]=flip[i][j];
}
}
if(res==INF)
{
printf("IMPOSSIBLE\n");
continue;
}
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
{
printf("%d",ans[i][j]);
if(j!=n-1) printf(" ");
else printf("\n");
}
}
}
POJ 3279 Fliptile ( 开关问题)的更多相关文章
- 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*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...
- Fliptile POJ - 3279 (开关问题)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16483 Accepted: 6017 Descrip ...
- POJ - 3279 Fliptile(反转---开关问题)
题意:有一个M*N的网格,有黑有白,反转使全部变为白色,求最小反转步数情况下的每个格子的反转次数,若最小步数有多个,则输出字典序最小的情况.解不存在,输出IMPOSSIBLE. 分析: 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: 16558 Accepted: 6056 Descrip ...
随机推荐
- 第三章——供机器读取的数据(XML)
本书使用的文件.代码:https://github.com/huangtao36/data_wrangling 机器可读(machine readable)文件格式: 1.逗号分隔值(Comma-Se ...
- 3ds Max学习日记(六)
到了周六就不想再忙实验室的活了,于是玩了一下3ds max,第5和第6章每章都只有4个视频,于是就一起弄完了,什么网格建模,曲面建模啥的,nurbs啥的. 附上今日的劳动成果: 叉子(用长方体 ...
- 移动端调试和fiddler移动端抓包使用
这里介绍一款移动端的调试工具以及抓包工具fiddler的使用.也是初次接触,算是初次接触的总结. 1,移动端调试工具.手机截图如下 代码实现 <!DOCTYPE html> <htm ...
- 多个表单数据提交下的serialize()应用
在实际开发场景中,难免遇到需要多个表单的数据传递问题. 之所以要进行多表单的数据传递是因为可以进行数据分组,便于数据的维护. 这个时候,出于不依赖jquery的考虑,有一个原生js函数来解决这个问题无 ...
- ping traceroute原理
ping命令工作原理 ping命令主要是用于检测网络的连通性. Ping命令发送一个ICMP请求报文给目的IP,然后目的IP回复一个ICMP报文. 原理:网络上的机器都有唯一确定的IP地址,我们给目标 ...
- jquery animate() stop() finish() 方法使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- imfilter与fspecial
saliencyMap = imfilter(saliencyMap,fspecial('gaussian',round(scale/64*3),min(scale/64*3*5/4))); fspe ...
- 生成模型(Generative Model)Vs 判别模型(Discriminative Model)
概率图分为有向图(bayesian network)与无向图(markov random filed).在概率图上可以建立生成模型或判别模型.有向图多为生成模型,无向图多为判别模型. 判别模型(D ...
- iphone手机与PC蓝牙出现感叹号且无法修复解决方案
解决方案如下: 1.需要下载Windows Mobile 6.5 的驱动 drvupdate-amd64.exe ,下载需要正版验证,手动安装驱动,具体步骤Google 2. 如果在BlueTooth ...
- DFS染色解决区域分块问题UVALive 6663
怪我比赛的时候想法太过于杂乱了. 注重于区域的属性了.甚至还想用状态压缩或者是hash来描述分块的区域. 其实我们的可以宏观的角度去审视这个问题.就是求分区的问题.那么我们完全可以标记边框的值为1.即 ...