zoj2977Strange Billboard (国家压缩+罗列)
Strange Billboard
Time Limit: 2 Seconds
Memory Limit: 65536 KB
The marketing and public-relations department of the Czech Technical University has designed a new reconfigurable mechanical Flip-Flop Bill-Board (FFBB). The billboard is a regular two-dimensional grid of
R * C square tiles made of plastic. Each plastic tile is white on one side and black on the other. The idea of the billboard is that you can create various pictures by flipping individual tiles over. Such billboards will hang above all entrances
to the university and will be used to display simple pictures and advertise upcoming academic events.
To change pictures, each billboard is equipped with a "reconfiguration device". The device is just an ordinary long wooden stick that is used to tap the tiles. If you tap a tile, it flips over to the other side, i.e., it changes from white to black or vice
versa. Do you agree this idea is very clever?
Unfortunately, the billboard makers did not realize one thing. The tiles are very close to each other and their sides touch. Whenever a tile is tapped, it takes all neighboring tiles with it and all of them flip over together. Therefore, if you want to change
the color of a tile, all neighboring tiles change their color too. Neighboring tiles are those that touch each other with the whole side. All inner tiles have 4 neighbors, which means 5 tiles are flipped over when tapped. Border tiles have less neighbors,
of course.
name=0000%2F2977%2Fbillboard_0.jpg" alt="">
For example, if you have the billboard configuration shown in the left picture above and tap the tile marked with the cross, you will get the picture on the right. As you can see, the billboard reconfiguration is not so easy under these conditions. Your
task is to find the fastest way to "clear" the billboard, i.e., to flip all tiles to their white side.
Input
The input consists of several billboard descriptions. Each description begins with a line containing two integer numbers
R and C (1 <= R,C <= 16) specifying the billboard size. Then there are
R lines, each containing C characters. The characters can be either an uppercase letter "X" (black) or a dot "." (white). There is one empty line after each map. The input is terminated by two zeros in place of the board size.
Output
For each billboard, print one line containing the sentence "You have to tap T tiles.", where T is the minimal possible number of taps needed to make all squares white. If the situation cannot be solved, output the string "Damaged billboard." instead.
Sample Input
5 5
XX.XX
X.X.X
.XXX.
X.X.X
XX.XX 5 5
.XX.X
.....
..XXX
..X.X
..X.. 1 5
...XX 5 5
...X.
...XX
.XX..
..X..
..... 8 9
..XXXXX..
.X.....X.
X..X.X..X
X.......X
X.X...X.X
X..XXX..X
.X.....X.
..XXXXX.. 0 0
Sample Output
You have to tap 5 tiles.
Damaged billboard.
You have to tap 1 tiles.
You have to tap 2 tiles.
You have to tap 25 tiles.
题意:用最少的翻转把全部的X变成点。每翻第(x,y)点同一时候也会带动周围四个点。
解法:枚举第一行的翻转状态,依据第i-1行翻转第i行。
#include<stdio.h>
#include<iostream>
using namespace std;
#define mulit(j) (1<<j)
#define inf 999999999
int row[18],trow[18],n,m;
int dfs(int i,int prerow,int step)
{
if(i==n)
{
if(prerow)return inf; else return step;
}
int now=trow[i],j;
for(j=0;j<m;j++)
if(prerow&(1<<j))
{
step++; now^=mulit(j);
if(j>0)now^=mulit(j-1);
if(j<m-1)now^=mulit(j+1);
if(i+1<n)trow[i+1]^=mulit(j);
}
return dfs(i+1,now,step);
}
void answer()
{
int MIN=inf,step;
for(int i=0;i<(1<<m);i++)
{
step=0;
for(int j=0;j<n;j++)
trow[j]=row[j];
for(int j=0;(1<<j)<=i;j++)
if(i&(1<<j))
{
step++; trow[0]^=(1<<j);
if(j>0)trow[0]^=(1<<(j-1));
if(j<m-1)trow[0]^=(1<<(j+1));
if(n>1) trow[1]^=(1<<j);
}
step=dfs(1,trow[0],step);
if(step<MIN)MIN=step;
}
if(MIN==inf)printf("Damaged billboard.\n");
else printf("You have to tap %d tiles.\n",MIN);
}
int main()
{
char ss[18];
while(scanf("%d%d",&n,&m)>0&&n+m!=0)
{
for(int i=0;i<n;i++)
{
scanf("%s",ss);
row[i]=0;
for(int j=0;j<m;j++)
if(ss[j]=='X')
row[i]|=(1<<j);
}
answer();
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
zoj2977Strange Billboard (国家压缩+罗列)的更多相关文章
- HDU 1557 权利指数 国家压缩 暴力
HDU 1557 权利指数 状态压缩 暴力 ACM 题目地址:HDU 1557 权利指数 题意: 中文题,不解释. 分析: 枚举全部集合,计算集合中的和,推断集合里面的团体是否为关键团队. 代码: ...
- Hdu-1565 电网接入(1) (国家压缩dp获得冠军
正方形格通路(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu - 5045 - Contest(国家压缩dp)
意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...
- HDU 1885 Key Task 国家压缩+搜索
点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- poj 3254 Corn Fields 国家压缩dp
意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对 ...
- IIS7构造Gzip压缩
IIS7构造Gzip压缩 本文来自Kevin Yang博客 作者:Kevin Yang 开启配置HTTP压缩(GZip) 在IIS7中配置Gzip压缩相比IIS6来说实在easy了很多.并且默认情况下 ...
- UVALive 3953 Strange Billboard (状态压缩+枚举)
Strange Billboard 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/A Description The marke ...
- HDU 1882 Strange Billboard(状态压缩+转置优化)
状态压缩,我们枚举第一行的所有状态,然后根据第一行去转变下面的行,枚举或者深搜直到最后最后一行,可以判断是不是所有的1都可以全部转换为0,记录所有的解,输出最小的一个就可以. 这里有一个很重要的优化, ...
- [转] ACM中国国家集训队论文集目录(1999-2009)
国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
随机推荐
- C语言程序代写(Linux下线程)
联系QQ:928900200 CSCI 3120 Operating Systems Summer 2014 Handout 3Assignment 2Date Due: June 5, 2014 b ...
- python中使用traceback来追踪异常
test1.py中,当分母为0的时候,调用系统退出 #!/usr/bin/python import sys def division(a=1, b=1): if b==0: print 'b eq ...
- 转: 第二章 IoC Annotation注入
http://blog.csdn.net/p_3er/article/details/9231307 1.命名空间 使用Annotation的方式,需要在spring的配置文件中配置命名空间.命名空间 ...
- 【我的书】Unity Shader的书 — 文件夹(2015.12.21更新)
写在前面 感谢全部点进来看的朋友.没错.我眼下打算写一本关于Unity Shader的书. 出书的目的有以下几个: 总结我接触Unity Shader以来的历程,给其它人一个借鉴.我非常明确学Shad ...
- #define XXX do{ XXX } while(0) 为什么使用
#define XXX do{ XXX } while(0) 为什么使用 时常会遇到一个非常"奇怪的宏定义", rt.(欧西巴...思考不够深刻啊, 皮鞭, 啪啪啪) 近期又遇到这 ...
- C# 6.0 (C# vNext) 的新功能:Exception-Handling Improvements
于 C# 6.0 包裹在异常处理的新功能,有两个方面的改进: 异步处理(async and await)能力 catch block 总结使用.于 C# 5.0 释放 async and await, ...
- 2014年CCNU-ACM暑期集训总结
2014年CCNU-ACM暑期集训总结 那个本期待已久的暑期集训居然就这种.溜走了.让自己有点措手不及.很多其它的是对自己的疑问.自己是否能在ACM这个领域有所成就.带着这个疑问,先对这个暑假做个总结 ...
- 我收集的sonar参考资料
sonarQube代码质量管理工具环境筹建笔记 http://www.myexception.cn/open-source/1307345.html 配置sonar.jenkins进行持续审查 htt ...
- android中获取屏幕的信息
获取屏幕信息比较简单,可以通过android的sdk自带的工具类DisplayMetrics.话不多说,上代码: // 获取屏幕的信息 DisplayMetrics dm = new DisplayM ...
- Android-管理Activity生命周期
用户在浏览,退出,返回app时,app中的Activity实例会在不同状态之间切换.比如,当activity第一次启动,然后来到系统前台,受到用户的注意,这个过程中,android系统调用了一系列ac ...