开关题   尺度法
      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. bzoj 2751 [HAOI2012]容易题(easy)(数学)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2751 [题意] m个位置,已知每个位置的可能取值,问所有可能情况的每个位置的乘积的和. ...

  2. Tkinter教程之Canvas篇(4)

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1812091 '''Tkinter教程之Canvas篇(4)''''''22.绘制弧形'''#  ...

  3. Tkinter教程之Toplevel篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811341 '''Tkinter教程之Toplevel篇'''#TopLevel与Frame类 ...

  4. nodejs+express +jade模板引擎 新建项目

    先 安装 nodejsiDEAAM 2015/7/16 22:47:25然后安装 npm install expressiDEAAM 2015/7/16 22:47:35然后安装 npm instal ...

  5. 激活Windows 10

    激活Windows 10按 win+X 组合键,打开“命令提示符(管理员)”,输入以下代码:slmgr /ipk NKJFK-GPHP7-G8C3J-P6JXR-HQRJR  然后按Enter键回车即 ...

  6. Azure 公网及内网ip绑定方法

    此文章为我的云服务绑定情况,仅供参考,适用于已经创建vm,但开始未绑定vip,后期进行vip的绑定,注意:绑定ip会造成虚拟机暂时离线. -Location(vm所在地) 注意区分 北部和东部: Ch ...

  7. [iOS微博项目 - 1.2] - 导航栏搜索框

    A.导航栏搜索框 1.需求 在“发现”页面,在顶部导航栏NavigationBar上添加一个搜索框 左端带有“放大镜”图标 github: https://github.com/hellovoidwo ...

  8. delphi 窗口最大化后控件的大小变化怎么设置

    设置按钮的Anchors属性.可以通过此属性设置其边界是否随父类一起变化.默认akleft+aktop即左边界和上边界随窗口变化,也就是说如果窗口位置移动了,按钮将保持其left和top边界与窗口的距 ...

  9. Starter Set of Functional Interfaces

    Java Development Kit 8 has a number of functional interfaces. Here we review the starter set-the int ...

  10. type=INNODB和engine=INNODB的区别

    我在网站下载了一份源码,学习中, 发现type=INNODB,这个数据库引擎老实出错,,后来才一查资料才是: 在MYSQL5.5及以后版本中type=InnoDB 由ENGINE=InnoDB 代替. ...