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 (国家压缩+罗列)的更多相关文章

  1. HDU 1557 权利指数 国家压缩 暴力

    HDU 1557 权利指数 状态压缩 暴力 ACM 题目地址:HDU 1557 权利指数 题意:  中文题,不解释. 分析:  枚举全部集合,计算集合中的和,推断集合里面的团体是否为关键团队. 代码: ...

  2. Hdu-1565 电网接入(1) (国家压缩dp获得冠军

    正方形格通路(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. hdu - 5045 - Contest(国家压缩dp)

    意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...

  4. HDU 1885 Key Task 国家压缩+搜索

    点击打开链接 Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. poj 3254 Corn Fields 国家压缩dp

    意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对 ...

  6. IIS7构造Gzip压缩

    IIS7构造Gzip压缩 本文来自Kevin Yang博客 作者:Kevin Yang 开启配置HTTP压缩(GZip) 在IIS7中配置Gzip压缩相比IIS6来说实在easy了很多.并且默认情况下 ...

  7. UVALive 3953 Strange Billboard (状态压缩+枚举)

    Strange Billboard 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/A Description The marke ...

  8. HDU 1882 Strange Billboard(状态压缩+转置优化)

    状态压缩,我们枚举第一行的所有状态,然后根据第一行去转变下面的行,枚举或者深搜直到最后最后一行,可以判断是不是所有的1都可以全部转换为0,记录所有的解,输出最小的一个就可以. 这里有一个很重要的优化, ...

  9. [转] ACM中国国家集训队论文集目录(1999-2009)

    国家集训队1999论文集 陈宏:<数据结构的选择与算法效率——从IOI98试题PICTURE谈起>来煜坤:<把握本质,灵活运用——动态规划的深入探讨>齐鑫:<搜索方法中的 ...

随机推荐

  1. HD2 Tmobile 重新分区代码(使用clk 1.6.5 de)

    fastboot oem part-resize misc: fastboot oem part-resize recovery: fastboot oem part-resize boot: fas ...

  2. Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算(转)

    Java 系统性能分析 命令 1. cpu分析 top , pidstat(sysstat) pid -p PID -t 1 10 vmstat 1 CPU上下文切换.运行队列.利用率 ps Hh - ...

  3. 解决iOS app集成共享QQ场地,微信的朋友,朋友等功能圈,不能采用苹果公司的审计问题

    最近提交的应用程序App Store时刻,由于App综合QQ登录.份额QQ场地.微信等功能.被拒绝.很郁闷:在最佳.想想办法,这个问题是可以解决. 当共享平台列表显示.根据推断当前设备被安装在一节Ap ...

  4. python网络爬虫学习笔记

    python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...

  5. Cocos发展Visual Studio下一个libcurl图书馆开发环境的搭建

    我们解释win32在Visual Studio下一个libcurl图书馆开发环境的搭建.Cocos2d-x发动机实际上与Win32在访问libcurl库.Cocos2d-x 3.x在libcurl库文 ...

  6. 在C#主线程和子线程将数据传递给对方如何实现

    在C#中主线程和子线程怎样实现互相传递数据 老帅 在C#中创建线程Thread时,能够有多种方法,而主线程和子线程之间又怎样实现互相传递数据,每种创建方法传递參数的效果是不同的,逐一看一下:  一.不 ...

  7. [渣译文] SignalR 2.0 系列: 支持的平台

    原文:[渣译文] SignalR 2.0 系列: 支持的平台 英文渣水平,大伙凑合着看吧,并不是逐字翻译的…… 这是微软官方SignalR 2.0教程Getting Started with ASP. ...

  8. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  9. 有一个NSStirng类型,retain时尚宣言name财产setter内部方法的每一行代码的作用?

    - (void)setName:(NSString *)name {        推断原有对象和新对象是否是同一个对象,假设是同一个,就没有必要再又一次赋值,否则会先release 再retain, ...

  10. 【原创】构建高性能ASP.NET站点 开篇

    原文:[原创]构建高性能ASP.NET站点 开篇 构建高性能ASP.NET站点 开篇 前言:有段时间没有写ASP.NET的东西了,心里总是觉得缺少了什么,毕竟自己对ASP.NET还是情有独钟的. 在本 ...