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 ...
随机推荐
- HBase Shell手动移动Region
在生产环境中很有可能有那么几个Region比较大,但是都运行在同一个Regionserver中. 这个时候就需要手动将region移动到负载低的Regionserver中. 步骤: 1.找到要移动的r ...
- 非常实用的Ubuntu常用终端命令
先介绍关于文件和目录的命令: ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd ...
- openstack 云平台API
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAKrCAIAAACV8EEMAAAgAElEQVR4nOydeVgUaZ7n/W9299nd7n
- thymeleaf的属性优先级
所有Thymeleaf属性定义一个数字优先,建立他们的顺序执行的标签.这个顺序是: Order Feature Attributes 1 Fragment inclusion th:includeth ...
- IE中的文档兼容性
文档兼容性可定义 Internet Explorer 呈现网页的方式, 具体可以参考 https://msdn.microsoft.com/zh-cn/library/cc288325(v=vs.85 ...
- ManagementFactory (简介)
Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitorin ...
- 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
如果你是maven项目,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包,你需要设置一下eclipse:项目 —> 属性 -> Deployment Assembly ...
- NGUI学习笔记(五):缓动
在Unity3D中可以使用自带的Animation制作任意形式的动画,不过我们这篇笔记主要是学习和使用NGUI提供的Tween动画.NGUI提供的Tween库功能较为简单,主要是用来实现NGUI自身需 ...
- poj 3501 Escape from Enemy Territory 二分+bfs
水题,不解释. #include<stdio.h> #include<math.h> #include<cstring> #include<algorithm ...
- 百度地图 >> 自定义控件
前言 百度地图API中预定义的UI控件,比如NavigationControl平移缩放控件,CopyrightControl版权控件,MapTypeControl地图类型控件....,这些都继承自抽象 ...