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. Hadoop应用开发实战案例 第2周

    比如,封面,是一网页,可以看出用户在此网页上,鼠标呈现F形状. 海量Web日志分析 用Hadoop提取KPI统计指标 更详细原文博客:http://blog.fens.me/hadoop-mapred ...

  2. 100offer 为专业程序打造的招聘平台

    引用一段100offer的简介 优秀程序员最高效的求职方式 您是名优秀的程序员,很多公司都想邀请您加入,您也有一颗躁动的心.可是,换一份工作对于您,是件机会成本很高的事情.您想一次性看很多个不错的机会 ...

  3. AndroidStudio debug

    1. view as text

  4. gdb之watch命令

    [gdb之watch命令] 什么是watchpoint? watchpoint,顾名思义,其一般用来观察某个变量/内存地址的状态(也可以是表达式),如可以监控该变量/内存值是否被程序读/写情况. 在g ...

  5. SQL语句查找重复记录

    select * from AM_C4_ENTRY t where t.created_by in ( select t.created_by from AM_C4_ENTRY t group by ...

  6. MSSQL手札三 MSSQL存储过程

    --存储过程完成一段sql代码的封装 create proc trim --参数列表,多个间用逗号分隔 ) as --自定义代码段 ) set @str1=LTRIM(RTRIM(@str)) pri ...

  7. Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)

    题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...

  8. HDU 1392 Surround the Trees 构造凸包

    又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...

  9. jquery easyui-linkButton获取和设置按钮text并且解决火狐不支持innerText的方法

    <a href="javascript:test" id="btn" class="easyui-linkbutton" iconCl ...

  10. android Scroller类的理解

    Scroller 一个特例: **点击Button后可能View没有移动,要关闭硬件加速,这段代码中int detalX = (int) (event.getX() - downX)要更正. demo ...