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. Python多线程的threading Event

    Python threading模块提供Event对象用于线程间通信.它提供了一组.拆除.等待用于线程间通信的其他方法. event它是沟通中最简单的一个过程之中,一个线程产生一个信号,号.Pytho ...

  2. sql server事物控制

    一.多个数据库 1.存储过程 2.Commit写在 Try...Catch后面 protected void Button1_Click(object sender, EventArgs e)    ...

  3. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  4. char与unsigned char 差别

    char 与 unsigned char的本质差别 http://bbs.csdn.net/topics/270080484 同一个内存内容:10010000      你用char*   解释是-1 ...

  5. [视频解说]0基础课程-运营商-Java它J2se

    本节解说 运营商应用 Java 算被分成: 算术运算符 颂值运营商 逻辑运算符 位运算符 元运算符 这里录制了 视频解说这几大类运算符,并有练习题提供大家 面试题: 1. 最有效率的方式算出2乘以8等 ...

  6. 使用 WPF 创建单实例应用程序

    一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...

  7. 传智播客成都校园php纪律指控

    继传智播客成都校区php第一期班圆满开班,说明php的火爆一点儿也不亚于java! 经传智播客商讨决定,传智播客成都校区php学科收费标准例如以下: 採用下面不论什么一种方式都能够享受优惠价: 一.自 ...

  8. ORA-12012: error on auto execute of job &quot;ORACLE_OCM

    ALERT日志中报错例如以下: Sun Mar 30 06:05:40 2014 Errors in file /oracle/app/oracle/diag/rdbms/zscims/zscims1 ...

  9. Android复制iPhone日期和时间选择器

    看效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluZ2xvbmd4aW4yNA==/font/5a6L5L2T/fontsize/400/fi ...

  10. DMA为什么比轮询、中断方式性能要卓越非常多?(你不懂)

    本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/35735397 假设是计算机专业出身的同学,都听过一个 ...