开关题   尺度法
      Fliptile
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3394   Accepted: 1299

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
 #include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int ms=;
int dx[]={-,,,,};
int dy[]={,-,,,};
int M,N;
int tile[ms][ms];
int opt[ms][ms];//保存最优解
int flip[ms][ms];
int get(int x,int y)
{
int c=tile[x][y];
for(int d=;d<;d++)
{
int x2=x+dx[d];
int y2=y+dy[d];
if(x2>=&&x2<M&&y2>=&&y2<N)
c+=flip[x2][y2];
}
return c%;
}
// 求出第一行确定的情况下,最小的操作次数
// 第一行确定了所有行都确定了。
int calc()
{
for(int i=;i<M;i++)
{
for(int j=;j<N;j++)
{
if(get(i-,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++)
printf("%d%c",opt[i][j],j+==N?'\n':' ');
return ;
}
int main()
{
scanf("%d%d",&M,&N);
for(int i=;i<M;i++)
for(int j=;j<N;j++)
{
scanf("%d",&tile[i][j]);
}
solve();
return ;
}

Fliptile的更多相关文章

  1. Enum:Fliptile(POJ 3279)

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

  2. 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile

    [源码下载] 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile 作者:webabcd 介绍与众不同 windows ...

  3. Fliptile 开关问题 poj 3279

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

  4. POJ 3279(Fliptile)题解

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

  5. 1647: [Usaco2007 Open]Fliptile 翻格子游戏

    1647: [Usaco2007 Open]Fliptile 翻格子游戏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 423  Solved: 173[ ...

  6. [Usaco2007 Open]Fliptile 翻格子游戏

    [Usaco2007 Open]Fliptile 翻格子游戏 题目 Farmer John knows that an intellectually satisfied cow is a happy ...

  7. Fliptile 翻格子游戏[Usaco2007 Open]

    题目描述 Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. ...

  8. [Usaco2007 Open]Fliptile 翻格子游戏 状态压缩

    考试想到了状压,苦于T1废掉太长时间,于是默默输出impossible.. 我们知道,一个格子的翻转受其翻转次数和它相邻翻转次数的影响. 由每一个位置操作两次相当于把它翻过来又翻回去,所以答案中每一个 ...

  9. Fliptile 翻格子游戏

    问题 B: [Usaco2007 Open]Fliptile 翻格子游戏 时间限制: 5 Sec  内存限制: 128 MB 题目描述 Farmer John knows that an intell ...

随机推荐

  1. Windwos Server 2008: 当网卡有多个IP地址时,如何指定缺省地址?

    这实际是一个当应用向外发起连接时,协议栈对源IP地址的选择问题.如果你的应用没有显式绑定一个本地地址,协议栈会选择一个"最佳"的本地地址来使用. 从 Vista 之后这个选择策略发 ...

  2. How to fix “X: user not authorized to run the X server, aborting.”? -摘自网络

    This is just a simple tips to solve a error message when you start your X session with “startx” comm ...

  3. 搭建linux下teamspeak3多人语音服务器

    最近项目中新的需求,需要支持多人在线实时通话.就安装测试一下teamspeak.http://www.teamspeak.com/ 主页有服务器版本和客户端版本供下载安装.软硬件环境: melot@m ...

  4. HDU 5919 -- Sequence II (主席树)

    题意: 给一串数字,每个数字的位置是这个数第一次出现的位置. 每个询问对于序列的一个子区间,设一共有k个不同的数,求第ceil(k/2)个数的位置. 因为强制在线,所以离线乱搞pass掉. 主席树可解 ...

  5. Linux下的scp拷贝命令详解

    相同Linux系统中对文件复制拷贝可以用CP命令: cp [options] source dest cp [options] source... directory 说明:将一个档案拷贝至另一档案, ...

  6. URAL 2040 Palindromes and Super Abilities 2 (回文自动机)

    Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...

  7. #c word转换PDF

    需要引用Microsoft.Office.Interop.Word,版本是07之上的. 这个版本会判断文件是否被占用. using Microsoft.Office.Interop.Word; usi ...

  8. poj 3041 Asteroids(最小点覆盖)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  9. json网页预览插件

  10. 腾讯云centos6.5下部署django环境

    基于腾讯云CentOS6.5的环境 首先说下需要用到的软件 1.gcc环境 腾讯云默认是没有gcc编译器的,需要手动安装一下:yum install gcc 2.python环境 因为我用的cento ...