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. [改变自己wordpress.2]至wordpress再加上简单的debug sql调试.

    或者说,同事. 需要帮助她打印出来sql 调试输出到页面sql 在这里,我们使用插件或一个的方式来启动配置文件wordpress的debug 在插件文件夹 wordpress/wp-content/p ...

  2. Code-Based Configuration (EF6 onwards)

    https://msdn.microsoft.com/en-us/data/jj680699#Using

  3. ios在SQLite3基本操作

    iOS关于sqlite3操作 iPhone中支持通过sqlite3来訪问iPhone本地的数据库. 详细用法例如以下 1:加入开发包libsqlite3.0.dylib 首先是设置项目文件.在项目中加 ...

  4. 【NO.3】 c program to caculate and display sum of two matrix

    source code: #include "stdafx.h" /* display sum of two matrix*/ int _tmain(int argc, _TCHA ...

  5. ExtJs迄今datefield控制设置默认值

    假设extjs4 datefield日期控件设置默认值.为当天的前一月,和后一月 Ext.Date.MONTH      月 Ext.Date.YEAR         年 Ext.Date.DAY  ...

  6. socket计划编制的原则

    socket编程原理 1.问题的引入 1) 普通的I/O操作过程: UNIX系统的I/O命令集,是从Maltics和早期系统中的命令演变出来的,其模式为打开一读/写一关闭(open-write-rea ...

  7. SP2010 3D标签云Web部分--很酷的效果,强烈推荐!!

    SP2010 3D标签云Web部分--很酷的效果.强烈推荐! ! 项目描述叙事         基于简单Flash的3D标签云Web部件.SP Server 2010使用. 建立在内置标签云Web部件 ...

  8. HDU 4313 Matrix

    水题:在一个双连通的树上有一些点很有破坏性,我们要把这些带破环性的点隔开,就是破坏一些边使这些点之间不连通,破坏一条边需要一点时间,问最少需要多少时间(同一时间只能破坏一个地方,且忽略位置转移的时间) ...

  9. 答读者问(8):相关Java问题涉及到学习

    近来的,我收到一个研究生朋友的电子邮件,一般内容如下面: 日 (本人微博:http://weibo.com/zhouzxi?topnav=1&wvr=5,我们的聊天号码:245924426.欢 ...

  10. 关于csrss.exe和winlogon.exe进程多、占用CPU高的解决办法

    原地址 http://blog.sina.com.cn/s/blog_912e77480101nuif.html   最近VPS的CPU一直处在100%左右,后台管理上去经常打不开,后来发现上远程都要 ...