[BZOJ 1647][USACO 2007 Open] Fliptile 翻格子游戏
1647: [Usaco2007 Open]Fliptile 翻格子游戏
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 702 Solved: 281
[Submit][Status][Discuss]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 x 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".
约翰知道,那些高智力又快乐的奶牛产奶量特别高.所以他做了一个翻瓦片的益智游戏来娱乐奶牛.在一个M×N(1≤M,N≤15)的骨架上,每一个格子里都有一个可以翻转的瓦片.瓦片的一面是黑色的,而另一面是白色的.对一个瓦片进行翻转,可以使黑变白,也可以使白变黑.然而,奶牛们的蹄子是如此的巨大而且笨拙,所以她们翻转一个瓦片的时候,与之有公共边的相邻瓦片也都被翻转了.那么,这些奶牛们最少需要多少次翻转,使所有的瓦片都变成白面向上呢?如杲可以做到,输出字典序最小的结果(将结果当成字符串处理).如果不能做到,输出“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
第1行输入M和N,之后M行N列,输入游戏开始时的瓦片状态.0表示白面向上,1表示黑面向上.Output
* Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
输出M行,每行N个用空格隔开的整数,表示对应的格子进行了多少次翻转.Sample Input
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1Sample Output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0OUTPUT DETAILS:
After flipping at row 2 column 1, the board will look like:
0 0 0 1
1 0 1 0
1 1 1 0
1 0 0 1After flipping at row 2 column 4, the board will look like:
0 0 0 0
1 0 0 1
1 1 1 1
1 0 0 1After flipping at row 3 column 1, the board will look like:
0 0 0 0
0 0 0 1
0 0 1 1
0 0 0 1After flipping at row 3 column 4, the board will look like:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0Another solution might be:
0 1 1 0
0 0 0 0
0 0 0 0
0 1 1 0
but this solution is lexicographically higher than the solution above.
第一眼看到数据范围可能很多人会想到状压 $DP$ , 然而看起来似乎是最大 $15 \times 15 = 225$ 的状压量根本无法枚举.
但是实际上我们可以推知, 根据上一行的黑白情况我们可以推知下一行的翻转情况. 因为在上一行已经确定的情况下下一行必须保证在黑色瓦片正下方进行翻转. 所以我们的所有情况都可以从第一行的黑白情况推知. 这时枚举量就从 $2^{k^2}$ 降到了 $2^k$ . 然后我们根据第一行枚举得出的黑白情况计算该种情况是否可以得出解. 如果得出解的话更新答案, 无解忽略即可.
枚举中途可以选择进行剪枝, 保存最低翻转数, 当已使用的翻转数大于这个值时停止继续求值.
参考代码
GitHub
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> const int MAXN=; int n;
int m;
int sum;
int flip;
int minfl;
int black;
bool ans[MAXN][MAXN];
bool raw[MAXN][MAXN];
bool tmp[MAXN][MAXN];
bool data[MAXN][MAXN]; void Change(int,int);
void Initialize();
bool Check(int);
bool Compare(); int main(){
freopen("fliptile.in","r",stdin);
freopen("fliptile.out","w",stdout);
bool solve=false;
Initialize();
for(int i=;i<(<<n);i++){
memset(tmp,,sizeof(tmp));
memcpy(data,raw,sizeof(raw));
flip=;
black=sum;
if(Check(i)){
memcpy(ans,tmp,sizeof(tmp));
minfl=flip;
solve=true;
}
}
if(solve){
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
printf("%d ",ans[i][j]?:);
}
putchar('\n');
}
}
else
puts("IMPOSSIBLE");
return ;
} inline void Change(int x,int y){
tmp[x][y]=true;
data[x][y]=!data[x][y];
black+=data[x][y]?:-;
if(x>){
data[x-][y]=!data[x-][y];
black+=data[x-][y]?:-;
}
if(x<n){
data[x+][y]=!data[x+][y];
black+=data[x+][y]?:-;
}
if(y>){
data[x][y-]=!data[x][y-];
black+=data[x][y-]?:-;
}
if(y<m){
data[x][y+]=!data[x][y+];
black+=data[x][y+]?:-;
}
} void Initialize(){
scanf("%d%d",&n,&m);
int tmp=;
minfl=0x7FFFFFFF;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
scanf("%d",&tmp);
raw[i][j]=tmp;
sum+=tmp;
}
}
} bool Check(int x){
for(int i=;i<n;i++){
if(x&(<<i)){
Change(,i+);
flip++;
if(flip>=minfl)
return false;
}
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(data[i-][j]){
Change(i,j);
flip++;
if(flip>=minfl)
return false;
}
}
}
if(black>)
return false;
else
return true;
}
Backup
以及日常凸包图包
[BZOJ 1647][USACO 2007 Open] Fliptile 翻格子游戏的更多相关文章
- 【BZOJ 1647】[Usaco2007 Open]Fliptile 翻格子游戏 模拟、搜索
第一步我们发现对于每一个格子,我们只有翻和不翻两种状态,我们发现一旦确定了第一行操作,那么第二行的操作也就随之确定了,因为第一行操作之后我们要想得到答案就得把第一行全部为0,那么第二行的每一个格子的操 ...
- 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 翻格子游戏
问题 B: [Usaco2007 Open]Fliptile 翻格子游戏 时间限制: 5 Sec 内存限制: 128 MB 题目描述 Farmer John knows that an intell ...
- [Usaco2007 Open]Fliptile 翻格子游戏题解
问题 B: [Usaco2007 Open]Fliptile 翻格子游戏 时间限制: 5 Sec 内存限制: 128 MB 题目描述 Farmer John knows that an intell ...
- 【BZOJ】1647: [Usaco2007 Open]Fliptile 翻格子游戏(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1647 自己太弱...看题解.. 竟然是枚举第一行的放法,,,因为一定要全部变0,所以将前一行1的在这 ...
- BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戏:部分枚举 位运算
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1647 题意: 在一个n*m(1 <= n,m <= 15)的棋盘上,每一个格子 ...
- bzoj 1647: [Usaco2007 Open]Fliptile 翻格子游戏【dfs】
这个可以用异或高斯消元,但是我不会呀我用的暴搜 2的m次方枚举第一行的翻转情况,然后后面的就定了,因为对于一个j位置,如果i-1的j位置需要翻,那么一定要翻i的j,因为这是i-1的j最后翻的机会 按字 ...
- Fliptile 翻格子游戏[Usaco2007 Open]
题目描述 Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. ...
随机推荐
- You have not concluded your merge. (MERGE_HEAD exists)。(转)
自己简直就是一个git小白,碰到问题,一点点的解决吧,可能不太系统,但也只能勤能补拙了 Git本地有修改如何强制更新 本地有修改和提交,如何强制用远程的库更新更新.我尝试过用git pull -f,总 ...
- 3行代码,为QQ轻游戏加上语音互动能力
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云 发表于云+社区专栏 游戏和社交往往有着密不可分的关系,QQ轻游戏就是一款集成在手Q里面的游戏平台,直接通过手Q入口就能随开 ...
- Java泛型的协变
在上篇<Java泛型的基本使用>这篇文章中遗留以下问题,即将子类型也能添加到父类型的泛型中,要实现这种功能必须借助于协变. 实验准备 现在在上篇文章展示的Decorator类型的基础上,增 ...
- forms身份认证仍然能访问html页面解决办法
asp.net的forms身份认证保护是一个非常棒的东西,用VS2010创建一个Web应用程序即可看到范例 在web.config中配置 <authentication mode="F ...
- 7. Reverse Integer 反向输出整数 easy 9. Palindrome Number 判断是否是水仙花数 easy
Given a 32-bit signed integer, reverse digits of an integer. 将32位整数反向输出. Note:Assume we are dealing ...
- mybatis 排坑记录
1. mapper xml resultMap 中定义 property 时不能出现空格 否则会出现反射错误,找不到 do 对应的 set 方法
- plSql读取Oracle数据库中文乱码
新建环境变量,设置变量名:NLS_LANG,变量值:SIMPLIFIED CHINESE_CHINA.ZHS16GBK,确定即可
- vmware 虚拟机导入OVF出现路径错误
现状: 需要将原有数据中心的VM虚拟机导出本地OVF模板,然后迁移至新的机房虚拟化环境后从新导入. 问题: 导入OVF时候,先出现错误问题1,修复完成后,出现错误问题2 1. OVF迁移至本地以后,导 ...
- Java 开源博客 Solo 1.5.0 发布 - 新皮肤
Solo 1.5.0 正式发布了!这个版本主要是加入了 一款新皮肤 next,感谢一直以来关注和支持我们的朋友! 只需一个命令即可启动(不需要安装数据库.部署容器):也可以通过 war 方式部署容器, ...
- 水库抽样Reservoir Sampling(蓄水池问题)
知识复习 空间亚线性算法:由于大数据算法中涉及到的数据是海量的,数据难以放入内存计算,所以一种常用的处理办法是不对全部数据进行计算,而只向内存里放入小部分数据,仅使用内存中的小部分数据,就可以得到 ...