Strange Billboard

题目链接:

http://acm.hust.edu.cn/vjudge/contest/129733#problem/A

Description


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 twodimensional 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.
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

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

</big>

##Sample Output
<big>

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.

</big>

##Source
<big>
2016-HUST-线下组队赛-2
</big> <br/>
##题意:
<big>
在有n*m个格子的游戏盘上有若干黑白方块,每次可以选取任一格子反转其颜色,但是同时会导致其周围四个格子也被翻转.
求用最少的操作使得所有的格子变白.
</big> <br/>
##题解:
<big>
考虑最少操作:当第一行确定后,第二行我们翻转其上方还有黑色的格子,这样可以保证第一行全白. 依次进行,如果最后一行操作后后全白,那就是一种可行操作.
所以只需要枚举第一行的状态即可,枚举量在 2^16 以内.
这里枚举翻转情况比枚举结果状态要好. (每个位置要么翻要么不翻,不可能翻两次--等于没翻).
用状态压缩来处理起来还是蛮方便的. 不过这种做法的耗时在1500ms上下.(过不了HDU-1882)
看了一些耗时较少的代码(100ms内),大多是用高斯消元做的.
</big> <br/>
##代码:
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 20
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std; typedef pair<int,int> pii;
int n,m;
int init_state[maxn];
int state[maxn]; bool is_ok(int x, int y) {
return x>=0 && y>=0 && x<n && y<m;
} void flip(int x, int y) {
if(is_ok(x, y)) state[x] ^= 1 << y;
if(is_ok(x+1, y)) state[x+1] ^= 1 << y;
if(is_ok(x-1, y)) state[x-1] ^= 1 << y;
if(is_ok(x, y+1)) state[x] ^= 1 << (y+1);
if(is_ok(x, y-1)) state[x] ^= 1 << (y-1);
} int solve() {
int ans = inf;
for(int s=0; s<(1<<m); s++) { //枚举第一行的翻转情况 (这里不要枚举结果状态)
int cnt = 0;
for(int i=0; i<n; i++) state[i] = init_state[i];
for(int i=0; i<m; i++) if(s & (1<<i)) {
flip(0, i); cnt++;
} for(int i=1; i<n; i++) {
for(int j=0; j<m; j++) if(state[i-1] & (1<<j)){
flip(i, j); cnt++;
}
if(cnt >= ans) break;
} if(!state[n-1]) ans = min(ans, cnt);
} return ans;
} int main(int argc, char const *argv[])
{
//IN; while(scanf("%d %d", &n,&m) != EOF && (n||m))
{
for(int i=0; i<n; i++) {
char str[maxn];
scanf("%s", str);
init_state[i] = 0;
for(int j=0; j<m; j++) if(str[j] == 'X')
init_state[i] |= 1 << j;
} int ans = solve();
if(ans == inf) printf("Damaged billboard.\n");
else printf("You have to tap %d tiles.\n", ans);
} return 0;
}

UVALive 3953 Strange Billboard (状态压缩+枚举)的更多相关文章

  1. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  2. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  3. hdu 4033 状态压缩枚举

    /* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...

  4. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  5. 状态压缩+枚举 UVA 11464 Even Parity

    题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...

  6. POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)

    题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a ...

  7. 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举

    题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...

  8. hdu 2489 最小生成树状态压缩枚举

    思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

  9. HDU 6607 Time To Get Up(状态压缩+枚举)

    题目网址: http://acm.hdu.edu.cn/showproblem.php?pid=6077 思路: 先预处理一下,将每个数字块的“X”看作1,“.”看作0,进行状态压缩转换成二进制数,用 ...

随机推荐

  1. 用Visio画UML顺序图

    1.顺序图 顺序图又称为时序图,顾名思义,它着重表现的是对象间消息传递的时间顺序.顺序图描述的对象也是一个用例,即一组行为操作,而它表现的是这组行为的先后关系(纵坐标),以及每个行为是属于哪个对象的( ...

  2. C#中的lock关键字;就是lock住一个大家都共同访问的(静态static的)东东就行了

    public class ChatService : IChat //继承IChat接口或者说IChat的实现类 { //定义一个静态对象用于线程部份代码块的锁定,用于lock操作 private s ...

  3. 正则化,数据集扩增,Dropout

    正则化方法:防止过拟合,提高泛化能力 在训练数据不够多时,或者overtraining时,常常会导致overfitting(过拟合).其直观的表现如下图所示,随着训练过程的进行,模型复杂度增加,在tr ...

  4. 【转】QT中QWidget、QDialog及QMainWindow的区别

    QWidget类是所有用户界面对象的基类. 窗口部件是用户界面的一个基本单元:它从窗口系统接收鼠标.键盘和其它事件,并且在屏幕上绘制自己.每一个窗口部件都是矩形的,并且它们按Z轴顺序排列.一个窗口部件 ...

  5. 【JavaScript学习笔记】点击消失

    <!DOCTYPE html> <html> <head> <script type="text/javascript" src=&quo ...

  6. 朴素贝叶斯(Naive Bayes)

    1.朴素贝叶斯模型 朴素贝叶斯分类器是一种有监督算法,并且是一种生成模型,简单易于实现,且效果也不错,需要注意,朴素贝叶斯是一种线性模型,他是是基于贝叶斯定理的算法,贝叶斯定理的形式如下: \[P(Y ...

  7. 【英语】Bingo口语笔记(78) - let系列

  8. Heritrix源码分析(八) Heritrix8个处理器(Processor)介绍(转)

    本博客属原创文章,欢迎转载!转载请务必注明出处:http://guoyunsky.iteye.com/blog/643367       本博客已迁移到本人独立博客: http://www.yun5u ...

  9. Android 线程与消息 机制 15问15答

    1.handler,looper,messagequeue三者之间的关系以及各自的角色? 答:MessageQueue就是存储消息的载体,Looper就是无限循环查找这个载体里是否还有消息.Handl ...

  10. Android adb shell命令大全

    1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器): androi ...