题目链接

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 ( 开关问题)的更多相关文章

  1. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  2. POJ 3279 Fliptile(翻格子)

    POJ 3279 Fliptile(翻格子) Time Limit: 2000MS    Memory Limit: 65536K Description - 题目描述 Farmer John kno ...

  3. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  4. POJ 3279(Fliptile)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...

  5. 【POJ 3279 Fliptile】开关问题,模拟

    题目链接:http://poj.org/problem?id=3279 题意:给定一个n*m的坐标方格,每个位置为黑色或白色.现有如下翻转规则:每翻转一个位置的颜色,与其四连通的位置都会被翻转,但注意 ...

  6. Fliptile POJ - 3279 (开关问题)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16483   Accepted: 6017 Descrip ...

  7. POJ - 3279 Fliptile(反转---开关问题)

    题意:有一个M*N的网格,有黑有白,反转使全部变为白色,求最小反转步数情况下的每个格子的反转次数,若最小步数有多个,则输出字典序最小的情况.解不存在,输出IMPOSSIBLE. 分析: 1.枚举第一行 ...

  8. POJ 3279 Fliptile[二进制状压DP]

    题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...

  9. poj 3279 Fliptile (简单搜索)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16558   Accepted: 6056 Descrip ...

随机推荐

  1. <Effective C++>读书摘要--Implementations<一>

    1.For the most part, coming up with appropriate definitions for your classes (and class templates) a ...

  2. [STAThread] 作用

    [STAThread]是一种线程模型,用在程序的入口方法上(在C#和VB.NET里是Main()方法),来指定当前线程的ApartmentState 是STA. [STAThread]是声明开始线程用 ...

  3. MVC 上传文件实例

    http://www.cnblogs.com/leiOOlei/archive/2011/08/17/2143221.html

  4. Log-spectral distance

    Log-spectral distance对数频谱距离 log-spectral distance(LSD),也指 log-spectral distortion,是两个频谱之间的距离度量(用分贝表示 ...

  5. 用select (多路复用)模拟一个 socket server

    需求:用select (多路复用)模拟一个 socket server.可以接收多并发. 1. 一开始是检测自己,如果我有活动了,就说明有客户端要连我了. #用select去模拟socket,实现单线 ...

  6. CentOS 转义字符

    常用转义字符 反斜杠(\):使反斜杠后面的一个变量变为单纯的字符串. 单引号(''):转义其中所有的变量为单纯的字符串. 双引号(""):保留其中的变量属性,不进行转义处理. 反引 ...

  7. Oracle 获取数据最新版本

    表数据如:  以CODE 作为版本分组字段,对创建时间进行按最新排序 ID  NAME CODE   CREATE_TIME 1ffg    abc    001    2014-01-01 1gff ...

  8. [洛谷P4781]【模板】拉格朗日插值

    题目大意:给你$n(n\leqslant2000)$个点,要你求$n-1$次经过这$n$个点的多项式在$k$处的值 题解:$Lagrange$插值:$$f_x=\sum\limits_{i=1}^ky ...

  9. NOIP 2018 -The Wound-

    "一招不慎,满盘皆输" 如果这个盘是整整一年的OI生涯的话 那么"一招"一定就是NOIP了 Update 2019/4/6 剧变的一年 noip这个成绩意味着我 ...

  10. YY的GCD 莫比乌斯反演

    ---题面--- 题解: $ans = \sum_{x = 1}^{n}\sum_{y = 1}^{m}\sum_{i = 1}^{k}[gcd(x, y) == p_{i}]$其中k为质数个数 $$ ...