UVALive 3953 Strange Billboard (状态压缩+枚举)
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 (状态压缩+枚举)的更多相关文章
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...
- hdu 4033 状态压缩枚举
/* 看别人的的思路 搜索搜不出来我太挫了 状态压缩枚举+好的位置 */ #include<stdio.h> #include<string.h> #define N 20 i ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- 状态压缩+枚举 UVA 11464 Even Parity
题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...
- 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 ...
- 洛谷P1036 选数 题解 简单搜索/简单状态压缩枚举
题目链接:https://www.luogu.com.cn/problem/P1036 题目描述 已知 \(n\) 个整数 \(x_1,x_2,-,x_n\) ,以及 \(1\) 个整数 \(k(k& ...
- hdu 2489 最小生成树状态压缩枚举
思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...
- HDU 6607 Time To Get Up(状态压缩+枚举)
题目网址: http://acm.hdu.edu.cn/showproblem.php?pid=6077 思路: 先预处理一下,将每个数字块的“X”看作1,“.”看作0,进行状态压缩转换成二进制数,用 ...
随机推荐
- mysql JDBC URL格式各个参数详解
mysql JDBC URL格式如下: jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值 ...
- Java之网络编程笔记
网络通讯要素: 1.IP地址 IP地址:用于标记一台计算机的身份证. IP地址由网络地址(确定网络)和主机地址(网络中的主机)组成. IP地址分为A类地址.B类地址.C类地址(常用).D类地址.E类地 ...
- UML和模式应用
引言 Applying UML and Patterns,以一个商店POS系统NextGen和一个掷骰子游戏Monopoly为例,围绕OOA/D的基本原则GRASP,以迭代作为基本方法.以UML为表达 ...
- Linux Shell编程(5):整数运算
http://blog.sina.com.cn/s/blog_6db275da0101asmf.html #!/bin/sh let a=$1+$2 b=$[$1+$2] ((c=$1+$2)) d= ...
- 多层感知机及其BP算法(Multi-Layer Perception)
Deep Learning 近年来在各个领域都取得了 state-of-the-art 的效果,对于原始未加工且单独不可解释的特征尤为有效,传统的方法依赖手工选取特征,而 Neural Network ...
- 【英语】Bingo口语笔记(30) - 表示“拒绝”
- ora-0000 normal跟/etc/hosts有关
当hosts文件配置错误时,用sqlplus登录后startup nomount,就会报错ORA-00000 [oracle11g@testdb2 dbs]$ sqlplus "/ as s ...
- c++ vector 释放内存
1.释放单个指针 关于Vector中存放指针的问题,在进行清空的时候比较安全的一种做法是: std::vector<ClassName *> ClassNameVec; ... ...
- Java自动装箱拆箱
一.装箱.拆箱定义 如果一个int型量被传递到需要一个Integer对象的地方,那么,编译器将在幕后插入一个对Integer构造方法的调用,这就叫做自动装箱.而如果一个Integer对象被放到需要in ...
- 实现输出h264直播流的rtmp服务器
RTMP(Real Time Messaging Protocol)是常见的流媒体协议,用来传输音视频数据,结合flash,广泛用于直播.点播.聊天等应用,以及pc.移动.嵌入式等平台,是做流媒体开发 ...