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. JS Scoping and Hoisting

    参考了这篇文章 http://www.jb51.net/article/30719.htm var v='Hello World'; (function(){ console.log(v); })() ...

  2. C#分页类

    using System.Linq; using System.Collections.Generic; namespace CommonLibrary { public class PagedLis ...

  3. Java编程思想(11~17)

    [注:此博客旨在从<Java编程思想>这本书的目录结构上来检验自己的Java基础知识,只为笔记之用] 第十一章 持有对象 11.1 泛型和类型安全的容器>eg: List<St ...

  4. 【第四篇】说说layer的iframe弹窗给里面的标签赋值的问题

    说到这一篇,真的是颠覆了我的思维. 官方文档,没有介绍这一部分的操作,大致上提了一下. 我的思路是把页面的数据传过去,在iframe弹窗的页面拿到接收到的数据,然后赋值,但是这样就会有个问题, 怎么传 ...

  5. cookie随便写的一点笔记(抄书的)

    cookie是保存在客户端的文本,能够在一定程度上提高用户体验.Servlet API 中提供了Cookie类,可以创建Cookie对象,并通过响应中的addCookie方法,将cookie保存到客户 ...

  6. Android Studio 学习 - 基本控件的使用;Intent初学

    Android Studio学习第三天. 今天主要学习 1. RadioButton.CheckBox.RatingBar.SeekBar等基础控件的使用. 结合Delphi中相类似的控件,在这些基本 ...

  7. Heritrix源码分析(二) 配置文件order.xml介绍(转)

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

  8. 树莓派 安装 php

    执行如下命令(注意红色字部分是关键!) sudo apt-get install apache2 php5 libapache2-mod-php5 然后把网页文件复制到 /usr/www 中即可 参考 ...

  9. [偏微分方程教程习题参考解答]4.1Duhamel 原理

    1. 如果已知下述常微分方程的特定初值问题 $$\bex \sedd{\ba{ll} -y''+y=0,&x>0,\\ y(0)=0,\quad y'(0)=1 \ea} \eex$$ ...

  10. Java 设计模式学习总结(上)

    在编写和维护公司的现有代码的时候,我经常思考的问题是:怎样的系统算一个好的系统?如何设计具有扩展.可维护.复用的系统,它能最大限度的应对产品经理不断变化的需求.答案似乎是:设计模式. Remember ...