UVA12113-Overlapping Squares(二进制枚举)
Problem UVA12113-Overlapping Squares
Accept:116 Submit:596
Time Limit: 3000 mSec
Problem Description
Input
The input consists of several test cases. Each test case is contained in five lines and each line contains nine characters. If the horizontal border of a filled square is visible it is denoted with ‘ ’ (ASCII value 95) sign and if vertical border of a filled square is visible then it is denoted with ‘|’ (ASCII value 124) character. The board contains no other character than ‘ ’, ‘|’ and of course ‘ ’ (ASCII Value 32). The border lines of the squares can only be along the grid lines. Each board lines end with a ‘#’ (Hash character) which denotes the end of line. This character is not a part of the grid or square. The last test case is followed by a single zero, which should not be processed.
Output
For each test case, print the case number and ‘Yes’ or ‘No’, depending on whether it’s possible to form the target.
Sample Input

Sample Ouput
Case 1: Yes
Case 2: Yes
Case 3: No
Case 4: Yes
题解:感觉最近做的题都十分考验代码能力(然而我很水),想到一共只有九种摆放方案之后这个题的思维就基本上结束了,所有的挑选方案只有2^9,直接二进制枚举,对于相同的挑选方案,不同的摆放顺序也会带来不同的覆盖结果,解决方法就是next_permutation(),预处理出来不同小正方形的覆盖格子的标号,接下来暴力就好。
#include <bits/stdc++.h> using namespace std; const int maxn = (<<);
const int N = ,M = ;
const int kind = ; int edge[][] = {{,,,,,,,}};
int core[][] = {{,,,}};
int bits[kind+],target[N*M]; int read(){
char str[];
int cnt = ,edges = ;
for(int i = ;i < N;i++){
gets(str);
if(str[] == '') return -;
for(int j = ;j < M;j++){
if(str[j] == ' ') target[cnt++] = ;
else target[cnt++] = ,edges++;
}
}
return edges;
} void init(){
for(int i = ;i < ;i++){
for(int j = ;j < ;j++){
if(!i && !j) continue;
int plus,minus;
if(j == ) plus = ,minus = ;
else plus = ,minus = ;
for(int k = ;k < ;k++){
edge[i*+j][k] = edge[i*+j-minus][k]+plus;
}
for(int k = ;k < ;k++){
core[i*+j][k] = core[i*+j-minus][k]+plus;
}
}
}
} int bitcount(int s){
return s == ? : bitcount(s>>)+(s&);
} void bitpos(int s){
int cnt = ;
for(int i = ;i < ;i++){
if(s&(<<i)) bits[cnt++] = i;
}
} int iCase = ; int main()
{
#ifdef GEH
freopen("helloworld.01,inp","r",stdin);
#endif
init();
int edge_cnt;
while(edge_cnt=read()){
if(edge_cnt == -) break;
int tmp[M*N];
bool ok = false;
for(int s = ;s < maxn;s++){
int n = bitcount(s);
bitpos(s);
if(n> || n*<edge_cnt) continue;
do{
memset(tmp,,sizeof(tmp));
for(int i = ;i < n;i++){
for(int j = ;j < ;j++){
tmp[edge[bits[i]][j]] = ;
}
for(int j = ;j < ;j++){
tmp[core[bits[i]][j]] = ;
}
} if(memcmp(tmp,target,sizeof(target)) == ){
ok = true;
break;
}
}while(next_permutation(bits,bits+n));
if(ok) break;
}
printf("Case %d: ",iCase++);
if(ok) printf("Yes\n");
else printf("No\n");
}
return ;
}
UVA12113-Overlapping Squares(二进制枚举)的更多相关文章
- UVA-12113 Overlapping Squares (回溯+暴力)
题目大意:问能不能用不超过6张2x2的方纸在4x4的方格中摆出给定的图形? 题目分析:暴力枚举出P(9,6)种(最坏情况)方案即可. 代码如下: # include<iostream> # ...
- UVA 1151二进制枚举子集 + 最小生成树
题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...
- Good Bye 2015B(模拟或者二进制枚举)
B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Poj(2784),二进制枚举最小生成树
题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2436 二进制枚举+位运算
题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...
- hdu 3118(二进制枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...
- HDU 5025Saving Tang Monk BFS + 二进制枚举状态
3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...
- 南阳OJ-91-阶乘之和---二进制枚举(入门)
题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=91 题目大意: 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为 ...
- 关于二进制枚举-计蒜客-得到整数X
某君有 n个互不相同的正整数,现在他要从这 n 个正整数之中无重复地选取任意个数,并仅通过加法凑出整数 X.求某君有多少种不同的方案来凑出整数 X. 输入格式 第一行,输入两个整数 n,X(1≤n≤2 ...
随机推荐
- quartz定时任务实例
一.spring注解方式 <!--<!–配置文件添加允许Spring注解–>--> <!--<task:annotation-driven scheduler=&q ...
- ConstraintLayout使用
引言 ConstraintLayout是一个ViewGroup,允许您以灵活的方式定位和调整小部件的方法,项目中的布局嵌套问题对项目性能有着不小的威胁,布局能实现扁平化的话会让软件性能得到很大的提升, ...
- JavaScript中Map和ForEach的区别
译者按: 惯用Haskell的我更爱map. 原文: JavaScript — Map vs. ForEach - What’s the difference between Map and ForE ...
- gulp插件构建项目 压缩js、css、image、zip、web服务、跨域等插件
推荐一个很好文: https://github.com/lin-xin/blog/issues/2 匹配符 *.**.!.{} gulp.src('./js/*.js') // * 匹配js文件夹下所 ...
- wamp本地可以访问,远程无法访问,报错:client denied by server configuration
出错原因:配置文件限制非本机访问 对策:修改httpd.conf,选择合适的模式,一般局域网环境的话,可以完全放开,使用 <Directory "..../wamp/www" ...
- Elasticsearch alias别名管理小结
Elasticsearch alias别名管理小结 By:授客 QQ:1033553122 建创测试数据 1 创建别名 2 移除别名 3 创建测试数据 4 批量操作 5 例1. 5 例2. 把多个索引 ...
- Android Touch事件传递机制 二:单纯的(伪生命周期) 这个清楚一点
转载于:http://blog.csdn.net/yuanzeyao/article/details/38025165 在前一篇文章中,我主要讲解了Android源码中的Touch事件的传递过程,现在 ...
- Android Studio 无法预览xml布局视图:failed to load AppCompat ActionBar with unkNown error
问题如下: 解决方法: 找到res-->values-->styles.xml 文件 可以看到主题Them设置如下: 修改为: 界面预览可以正常显示
- Android面试题总结(不定期更新、附答案)
1.Activity的启动模式? activity一共有4种启动模式:standard.singleTop singleTask .singleInstance standard:(标准模式)默认的就 ...
- [Java]Socket和ServerSocket学习笔记
对于即时类应用或者即时类的游戏,HTTP协议很多时候无法满足于我们的需求.这会,Socket对于我们来说就非常实用了.下面是本次学习的笔记.主要分异常类型.交互原理.Socket.ServerSock ...