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
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
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的更多相关文章
- Enum:Fliptile(POJ 3279)
Fliptile 题目大意:农夫想要测牛的智商,于是他把牛带到一个黑白格子的地,专门来踩格子看他们能不能把格子踩称全白 这一题其实就是一个枚举题,只是我们只用枚举第一行就可以了,因为这一题有点像开关一 ...
- 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile
[源码下载] 与众不同 windows phone (36) - 8.0 新的瓷贴: FlipTile, CycleTile, IconicTile 作者:webabcd 介绍与众不同 windows ...
- Fliptile 开关问题 poj 3279
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4031 Accepted: 1539 Descript ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- 1647: [Usaco2007 Open]Fliptile 翻格子游戏
1647: [Usaco2007 Open]Fliptile 翻格子游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 423 Solved: 173[ ...
- [Usaco2007 Open]Fliptile 翻格子游戏
[Usaco2007 Open]Fliptile 翻格子游戏 题目 Farmer John knows that an intellectually satisfied cow is a happy ...
- Fliptile 翻格子游戏[Usaco2007 Open]
题目描述 Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. ...
- [Usaco2007 Open]Fliptile 翻格子游戏 状态压缩
考试想到了状压,苦于T1废掉太长时间,于是默默输出impossible.. 我们知道,一个格子的翻转受其翻转次数和它相邻翻转次数的影响. 由每一个位置操作两次相当于把它翻过来又翻回去,所以答案中每一个 ...
- Fliptile 翻格子游戏
问题 B: [Usaco2007 Open]Fliptile 翻格子游戏 时间限制: 5 Sec 内存限制: 128 MB 题目描述 Farmer John knows that an intell ...
随机推荐
- SDN环境搭建(mininet,OVS,ryu安装及命令)
1.mininet安装与使用 1.1mininet安装 ubuntu 12.04/14.04/14.10 命令行 sudo apt-get install mininet 1.2 mini ...
- 现代程序设计homework——04
题目: 详见:http://www.cnblogs.com/xinz/p/3341551.html 题目本身确实很难,“很难想到一个比较优雅的算法”,这是一个老师请来专门讲解这道题的大牛的原话.确实, ...
- HDU 5806 NanoApe Loves Sequence Ⅱ (模拟)
NanoApe Loves Sequence Ⅱ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5806 Description NanoApe, t ...
- [C语言 - 6] static & extern
A. extern函数 一个c文件生成一个obj文件 外部函数:允许其他文件访问.调用的函数(默认函数为外部函数),不允许存在同名的外部函数 my.c //define a extern fu ...
- HDU1874畅通工程续(floyd||dijkstra)
看了看floyd和dijkstra,然后就找了两个练习来捉 #include<iostream> #include<stdio.h> #include<string.h& ...
- poj2155 树状数组 Matrix
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14826 Accepted: 5583 Descripti ...
- UOJ Test Round #2
昨天晚上打的这个比赛,简直一颗赛艇啊-- 感觉发挥的并不好.比赛的时候比较紧张,最后一题还脑残写了个离散化结果爆零了,哎我怎么这么逗逼-- 讲讲比赛经过吧. 比赛之前逗逼地以为是8:00开始,然后淡定 ...
- C#学习笔记(十五):预处理指令
C#和C/C++一样,也支持预处理指令,下面我们来看看C#中的预处理指令. #region 代码折叠功能,配合#endregion使用,如下: 点击后如下: 条件预处理 条件预处理可以根据给出的条件决 ...
- 行内onclick使用遇坑--------作用域与传入字符串
问题一:行内onclick触发的函数放在$(funtion(){})内报错,错误代码如下: <input type="button" value="确定" ...
- OOP设计模式[JAVA]——03职责链模式
职责链模式 Responsibility of Chain 在职责链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求 ...