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

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 题意:只含有0和1的格子,每次都可以反转其中一个,但反转的时候,这个格子的上下左右的四个格子也被反转了,就是一次反转像“十”一样的五个格子,求出最小步数完成时的每个格子的翻转次数,最小步数的解有多个时,输出字典序最小的一组。不存在的话就输出IMPOSSIBLE
思路:一个格子翻转两次会恢复原状,所以多次翻转的多余的。这道题目我们可以先翻转第一行,如果第一行翻转好了再翻转第二行,最后一行应该是不用翻转的,因为翻转势必会使得前一行已经翻转好的重新变1,所以最后要判断一下最后一行是不是都是0,如果有1的话那就意味着不存在可行的操作方法
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long ll;
const int maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
}
const int dx[]={-,,,,};
const int dy[]={,-,,,};
int M,N,tile[maxn][maxn];
int opt[maxn][maxn];   //保存最优解
int flip[maxn][maxn];    //保存中间结果
//查询(x,y)的颜色
int get(int x,int y)
{
int c=tile[x][y];
for(int d=;d<;d++)
{
int nx=x+dx[d],ny=y+dy[d];
if(<=nx && nx<M && <=ny && ny<N)
c+=flip[nx][ny];
}
return c%;
}
  //求出第一行确定情况下的最小操作次数
  //不存在解就返回-1
int calc()
{
  //求出从第二行开始的翻转方法
for(int i=;i<M;i++)
for(int j=;j<N;j++)
if(get(i-,j)!=) //(i-1,j)是黑色格子就必须要翻转
flip[i][j]=;
  //判断最后一行是否是全白
for(int j=;j<N;j++)
if(get(M-,j)!=)
return -;
  //统计翻转的次数
int res=;
for(int i=;i<M;i++)
for(int j=;j<N;j++)
res+=flip[i][j];
return res;
} void solve()
{
int res=-;
//按照字典序尝试第一行的所有可能性
for(int i=;i<(<<N);i++)
{
memset(flip,,sizeof(flip));
for(int j=;j<N;j++)
flip[][N-j-]=i>>j&;
int num=calc();
if(num>= && (res< || res>num))
{
res=num;
memcpy(opt,flip,sizeof(flip));
} }
if(res<)
printf("IMPOSSIBLE\n");
else
{
for(int i=;i<M;i++)
{
for(int j=;j<N;j++)
{
if(j==)
printf("%d",opt[i][j]);
else
printf(" %d",opt[i][j]);
}
printf("\n");
}
}
}
int main()
{
cin>>M>>N;
for(int i=;i<M;i++)
for(int j=;j<N;j++)
{
cin>>tile[i][j];
}
solve();
return ;
}

Fliptile POJ - 3279 (开关问题)的更多相关文章

  1. Enum:Fliptile(POJ 3279)

    Fliptile 题目大意:农夫想要测牛的智商,于是他把牛带到一个黑白格子的地,专门来踩格子看他们能不能把格子踩称全白 这一题其实就是一个枚举题,只是我们只用枚举第一行就可以了,因为这一题有点像开关一 ...

  2. poj 3279(开关问题)(待完成)

    传送门:Problem 3279 #include<iostream> #include<cstdio> #include<cstring> using names ...

  3. Fliptile(POJ 3279)

    原题如下: Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16494   Accepted: 6025 D ...

  4. kuangbin专题 专题一 简单搜索 Fliptile POJ - 3279

    题目链接:https://vjudge.net/problem/POJ-3279 题意:格子有两面,1表示黑色格子,0表示白色格子,奶牛每次可以踩一个格子,踩到的格子和它周围的上下左右格子都会翻面,也 ...

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

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

  6. POJ 3279(Fliptile)题解

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

  7. POJ 3279 Fliptile(翻格子)

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

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

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

  9. 【枚举】POJ 3279

    直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...

随机推荐

  1. JDBC让java程序连上数据库(mysql数据库)

    一.小论异常: 其实JDK已经提供了一组API让java程序连上数据库,并执行SQL语句,其实说起来也蛮简单的,但是绝对是一个细致活,因为稍不留神,异常就铺天盖地的来了,下面说说这些异常吧(声明一下: ...

  2. java如何导入Excel文件

    Java使用POI导入Excel文件,操作起来比较简单,支持xlsx格式. 下载POI资源包 从官网https://poi.apache.org/下载POI,笔者选择的是版本是3.17,下载后文件名是 ...

  3. JavaScript初始

    一.JavaScript基础 1.引入方式 <!--1 直接编写--> <script> alert('hello yuan') </script> <!-- ...

  4. Vue-mixins选项

    Vue-mixins选项 Mixins用于: 1.已经写好了构造器,还要增加方法或者临时的活动时使用的方法,用混入能减少源代码的污染. 2.公用方法,用混入的方法可以减少代码量,实现代码重用.(使用全 ...

  5. Browser History

    History 对象中包含用户(在浏览器窗口中)访问过的URL History 对象是window对象的一部分,可通过window.history属性对其进行访问. 注释:没有应用于History对象 ...

  6. 用C#来控制高级安全Windows防火墙

    有的时候我们需要在自己的产品中检测<高级安全Windows防火墙>的状态,并有可能需要加入一些规则甚至需要关闭掉高级安全Windows防火墙. 下面就告诉如何来做: <高级安全Win ...

  7. 常用CSS3属性整理

    常用CSS3属性整理 文本 文本超出部分折叠 white-space:nowarp; overflow:hidden; text-overflow:ellipsis word-warp 边界换行 no ...

  8. isset或array_key_exists,检查数组键是否存在

    今天在导出报表的时候遇到了一个问题,undefined index:pid,然后就纳闷了,我的数组里面根本就没有pid,为什么会出现这个错误呢,我遍历了一下数组,发现果然有pid这个键,奇怪呀,我有做 ...

  9. 真正理解 git fetch, git pull 以及 FETCH_HEAD(转)

    转自http://www.cnblogs.com/ToDoToTry/p/4095626.html 真正理解 git fetch, git pull 要讲清楚git fetch,git pull,必须 ...

  10. yum安装软件并保留下载的软件

    使用yum插件downloadonly下载安装软件需要的依赖包并保留到指定的文件 安装yum-downloadonly或 yum-plugin-downloadonly 软件包. yum instal ...