zoj2977Strange Billboard (状态压缩+枚举)
来源:互联网 发布:广州软件培训学校 编辑:程序博客网 时间:2024/04/28 00:25
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 ofR * 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 numbersR and C (1 <= R,C <= 16) specifying the billboard size. Then there areR 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 5XX.XXX.X.X.XXX.X.X.XXX.XX5 5.XX.X.......XXX..X.X..X..1 5...XX5 5...X....XX.XX....X.......8 9..XXXXX...X.....X.X..X.X..XX.......XX.X...X.XX..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)点同时也会带动周围四个点。
#include<stdio.h>#include<iostream>using namespace std;#define mulit(j) (1<<j)#define inf 999999999int 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 (状态压缩+枚举)
- HDU 1882 Strange Billboard 状态压缩+简单位运算+枚举~
- uva1508(状态压缩 +枚举)
- hdu4770 状态压缩+枚举
- hdu 4033 状态压缩枚举
- Emergency relief (枚举+状态压缩)
- POJ 1753 Flip Game 状态压缩 枚举
- 部分枚举+递推+状态压缩+uva11464
- UVA 11825 集合枚举 状态压缩 dp
- UVA1508-Equipment(状态压缩+暴力枚举)
- BFS+状态压缩DP+二分枚举+TSP
- UVa 1508 - Equipment (状态压缩 + 枚举子集)
- HDU 5339 Untitled (状态压缩枚举)
- POJ 3279 (状态压缩暴力枚举)
- poj 1753 Flip Game(状态压缩枚举)
- HDU-4462(状态压缩,枚举)
- poj 2923 Relocation (枚举+背包 | 状态压缩+01背包)
- hdu 4628 Pieces (状态压缩+二进制枚举+dp)
- 一些易错的题目及扩充问题(书没看完,陆续更新)
- HDU 1875 畅通工程再续 (最小生成树 水)
- Android:GestureDetector手势识别类
- Excel导入SQL SERVER,数字和字符会被系统自动置为NULL的解决方法
- __cyg_profile_func_enter print function nam
- zoj2977Strange Billboard (状态压缩+枚举)
- Python3基础(五) 函数
- 操作Excel的一个有用实例及从EXCEL第N行开始取数据
- "><img src=x onerror=alert(1);>
- poj 1018 Communication System
- 新疆莎车暴恐案细节:打馕老人一句话智退暴徒
- ORACLE 查看CPU 使用率
- 大话设计模式(C++实现)--简单工厂模式
- 传Win9桌面Charms菜单栏大改造 类似win7融合win8