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 ...
随机推荐
- lintcode-33-N皇后问题
33-N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的n皇后放置布局,其中" ...
- 大型网站架构演化(六)——使用反向代理和CDN加速网站响应
随着网站业务不断发展,用户规模越来越大,由于中国复杂的网络环境,不同地区的用户访问网站时,速度差别也极大.有研究表明,网站访问延迟和用户流失率正相关,网站访问越慢,用户越容易失去耐心而离开.为了提供更 ...
- PAT 1052 卖个萌
https://pintia.cn/problem-sets/994805260223102976/problems/994805273883951104 萌萌哒表情符号通常由“手”.“眼”.“口”三 ...
- Window命令行工具操作文件
1,cd 命令用来切换目录 2,mkdir用来创建文件夹 3,rmdir用来删除空文件夹 4,创建指定类型的文件 type nul>"文件名和后缀" 5,打开指定文件用sta ...
- phpcms黄页,不能选择行业。解决办法
用phpcms黄页模块,发布产品的时候.不能选择 产品分类,点开之后,啥都没有,是空的. 这个是因为:js的问题. 解决办法:将 网站根目录的 js 文件中的两个文件,更换为 官方的两个js文件.即可 ...
- Java中的日志——Java.util.logging、log4j、commons-logging
Java中给项目程序添加log主要有三种方式,一使用JDK中的java.util.logging包,一种是log4j,一种是commons-logging.其中log4j和commons-loggin ...
- [剑指Offer] 54.字符流中的第一个不重复的字符
题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- Bootstrap 基本模板理解
<!-- 声明文档类型 为 html5 --> <!DOCTYPE html> <!-- 声明页面内容主要为 中文简体 --> <html lang=&quo ...
- WPF一个对象显示多个属性
一个对象显示多个属性使用模板的方法: 如图: <dataTemplate x:key="MyDataTemplate">
- jsp当做第二个servlet request的生命周期 请求 响应 不管中间经历多少个servlet 只要最后一个serlvt执行后 则生命周期结束 request的域消失
jsp当做第二个servlet request的生命周期 请求 响应 不管中间经历多少个servlet 只要最后一个serlvt执行后 则生命周期结束 request的域消失