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 ...
随机推荐
- Python 元组 集合
1. 元组 >>> a = (1,2,3,4,5) >>> b = list(a) #转换成列表对象, 可以更改 >>> b [1, 2, 3, ...
- python中装饰器的原理以及实现,
python版本 3.6 1.python的装饰器说白了就是闭包函数的一种应用场景,在运用的时候我们遵循 #开放封闭原则:对修改封闭,对拓展开放 2.什么是装饰器 #装饰他人的器具,本身可以是任意可调 ...
- OSG学习:使用已有回调示例
回调的类型有很多种,一般很容易就想到的是UpdateCallBack,或者EventCallBack,回调的意思就是说,你可以规定在某件事情发生时启动一个函数,这个函数可能做一些事情.这个函数就叫做回 ...
- C#,Winform 文件的导入导出 File
1.导入 导入对话框:OpenFileDialog private void sbtnsb_Click(object sender, EventArgs e) { try { OpenFileDial ...
- Python35 升级 pip
使用pip安装插件的时候报错: You are using pip version 8.1.1, however version 9.0.1 is available.You should consi ...
- WPF一个对象显示多个属性
一个对象显示多个属性使用模板的方法: 如图: <dataTemplate x:key="MyDataTemplate">
- [转]matlab中squeeze函数的用法,numel的用法
squeeze的作用是移除单一维. 如果矩阵哪一个维数是1,B=squeeze(A)就将这个维数移除. 考虑2-by-1-by-3 数组Y = rand(2,1,3). 这个数组有单一维 —就是每页仅 ...
- Signal函数
Signal函数: 这个函数是一种系统调用,就是告诉系统发生中断的时候用该干嘛.第一个参数就是信号的编号,第二个参数就是信号的指针. 原型: #include <signal.h> voi ...
- 【题解】Atcoder ARC#90 F-Number of Digits
Atcoder刷不动的每日一题... 首先注意到一个事实:随着 \(l, r\) 的增大,\(f(r) - f(l)\) 会越来越小.考虑暴力处理出小数据的情况,我们可以发现对于左端点 \(f(l) ...
- LOJ6276:果树——题解
https://loj.ac/problem/6276#submit_code NiroBC 姐姐是个活泼的少女,她十分喜欢爬树,而她家门口正好有一棵果树,正好满足了她爬树的需求.这颗果树有N 个节点 ...