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谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...
随机推荐
- 尝到awk
我前几天写的sed,这个时候继续了解它的兄弟,awk,两者都使用,一种感觉.既可以用来处理场.假设你想要做文本处理.sed删除.匹配,一些频繁更换使用,假设每一行文本,你想深入,一些每行和列处理的,例 ...
- crm采用soap删除记录
//抽样 function demo() { //操作记录id var targetId = "A8A46444-BA10-E411-8A04-00155D002F02&qu ...
- 学派Delphi方法(推荐)——————————【Badboy】
Delphi是一个新的可视化编程环境, 提供了一种方便.快捷的Windows使用顺序开发工具. 它使用了MicrosoftWindows图形用户界面的很多先进特性和设计思想. 本文就给读者引见学Del ...
- Spring Boot 基础
Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...
- Android更改checkbox的style
resouce文件夹下,value文件夹下,styles.xml文件中新增样式: <resources> <style name="radioButton"> ...
- JComboBox
package swing.combox; import java.awt.FlowLayout; import javax.swing.DefaultComboBoxModel; import ja ...
- A simple Test Client built on top of ASP.NET Web API Help Page
Step 1: Install the Test Client package Install the WebApiTestClient package from the NuGet Package ...
- datagrid直接编辑保存“设计缺陷”
当今使用easyUI的datagrid组件的时候,碰到了一些问题,记录下来以便下次高速解决. 需求是在一张表单里会关联有一个列表,能够增删查改 曾经没用easyUI的时候,这个增和改的页面我通常是用一 ...
- 计算机管理系统——VB与Excel联系
今天爆震室管理系统--学生查看机器状态的时候发现有一个"导出到excel"的button.我去.感情还得跟excel表链接. 于是我咬碎了一地小银牙.一个下午都在查询vb与exce ...
- linux下一个Oracle11g RAC建立(五岁以下儿童)
linux下一个Oracle11g RAC建立(五岁以下儿童) 四.建立主机之间的信任关系(node1.node2) 建立节点之间oracle .grid 用户之间的信任(通过ssh 建立公钥和私钥) ...