Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4031   Accepted: 1539

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

Source

 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int ans[MS][MS];
int pic[MS][MS];
int flag[MS][MS];
int M,N;
int dir[][]={{,},{,},{,},{,-},{-,}}; int color(int x,int y)
{
int sum=pic[x][y];
for(int i=;i<;i++)
{
int r=x+dir[i][];
int c=y+dir[i][];
if(r>=&&r<M&&c>=&&c<N)
sum+=flag[r][c];
}
return sum&;
} int calc()
{
int res=;
for(int i=;i<M;i++)
{
for(int j=;j<N;j++)
{
if(color(i-,j))
{
flag[i][j]=;
}
}
}
for(int i=;i<N;i++)
if(color(M-,i))
return -;
for(int i=;i<M;i++)
for(int j=;j<N;j++)
res+=flag[i][j];
return res;
} void solve()
{
int res=-;
for(int i=;i<<<N;i++)
{
memset(flag,,sizeof(flag));
for(int j=;j<N;j++)
flag[][N--j]=(i>>j)&;
int cnt=calc();
if(cnt>=&&(res<||res>cnt))
{
res=cnt;
memcpy(ans,flag,sizeof(flag));
}
}
if(res<)
printf("IMPOSSIBLE\n");
else
{
for(int i=;i<M;i++)
{
for(int j=;j<N;j++)
{
if(j)
printf(" ");
printf("%d",ans[i][j]);
}
printf("\n");
}
}
} int main()
{
scanf("%d%d",&M,&N);
for(int i=;i<M;i++)
for(int j=;j<N;j++)
scanf("%d",&pic[i][j]);
solve();
return ;
}

Fliptile 开关问题 poj 3279的更多相关文章

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

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

  2. POJ 3279(Fliptile)题解

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

  3. POJ 3279 Fliptile(翻格子)

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

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

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

  5. 【枚举】POJ 3279

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

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

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

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

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

  8. POJ 3279 Fliptile ( 开关问题)

    题目链接 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give ...

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

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

随机推荐

  1. JVM系列一:JVM内存组成及分配

    java内存组成介绍:堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时 ...

  2. G450 Ubuntu14 无线网卡解决

    安装了Ubuntu14,与win7共存. grub界面启动. G450的本子,安装完之后发现无线网卡不能被驱动,但能被之别到,因此激活一次broadcom sta wireless driver 命令 ...

  3. 找回使用过的QQ头像

    多么渴望那双眼睛能在万紫千红中发现自己:然而眼睛从来就不曾对自己留意:于是换种落寞再次接受垂直打击:然后然后的然后尼玛再换个逗比的头像证明老资无所谓老资无所谓嘿嘿: 我们换头像的时候,发现之前的一个最 ...

  4. 在Ribbon中,中文换行位置不对怎么办

    在Ribbon代码的编写时,关于UI上面设置Button的中文字,经常会出现不想换行的地方换行的情况,比如下图: 我如果希望中国一行,然后只有一行怎么办呢. 网上流传的很多方法就是直接加空格,但是很遗 ...

  5. Java中返回参数值的几种状态

    Java 中无参无返回值方法的使用 第一步,定义方法 例如:下面代码定义了一个方法名为 show ,没有参数,且没有返回值的方法,执行的操作为输出 " welcome to imooc. & ...

  6. [转]Kerberos简介

    Kerberos协议: Kerberos协议主要用于计算机网络的身份鉴别(Authentication), 其特点是用户只需输入一次身份验证信息就可以凭借此验证获得的票据(ticket-grantin ...

  7. (剑指Offer)面试题15:链表中倒数第k个结点

    题目: 输入一个链表,输出该链表中倒数第k个结点. 例如:链表中有6个结点,从头到尾依次为1,2,3,4,5,6,则该链表的倒数第3个结点为4. 链表结点定义: struct ListNode{ in ...

  8. javaScript-原型、继承-01

    为什么会有原型这个概念: 1.优雅的创建对象: 2.继承: 先看js 之前创建对象的方式存在的问题: 创建对象方式 1.字面量 var obj={name:"join",age:1 ...

  9. 用ICSharpCode.SharpZipLib进行压缩

    今天过中秋节,当地时间(2013-09-08),公司也放假了,正好也闲着没事,就在网上学习学习,找找资料什么的.最近项目上可能会用到压缩的功能,所以自己就先在网上学习了,发现一个不错的用于压缩的DLL ...

  10. Educational Codeforces Round 4 B. HDD is Outdated Technology 暴力

    B. HDD is Outdated Technology 题目连接: http://www.codeforces.com/contest/612/problem/B Description HDD ...