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 ...
随机推荐
- JSTL_Core标记库
一. 说明 如有转载,请标明出处 本博讲解JSTL中的core库 对标记属性进行介绍时,首先介绍必写的属性,然后带有默认值的属性,其次是其余属性,这三类属性中间用空行隔开 二:core标记库库 C ...
- canvas-8searchLight4.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- elementUI vue v-model的修饰符
v-model的修饰符 v-model.lazy 只有在input输入框发生一个blur时才触发 v-model.trim 将用户输入的前后的空格去掉 v-model.number 将用户输入的字符串 ...
- 通过JS生成由字母与数字组合的随机字符串
在项目中可能需要随机生成字母数字组成的字符,如生成3-32位长度的字母数字组合的随机字符串(位数不固定)或者生成43位随机字符串(位数固定) 使用Math.random()与toString()方法的 ...
- 2018-11-16 中文代码示例之Programming in Scala笔记第四五六章
续前文: 中文代码示例之Programming in Scala学习笔记第二三章. 同样仅节选有意思的例程部分作演示之用. 源文档仍在: program-in-chinese/Programming_ ...
- Mysql LIMIT 分页
格式: LIMIT index, size // index:从哪一行(第几条)开始查,size:多少条 分页: LIMIT (currentPage-1)*pageSize, pageSiz ...
- Bitmap压缩图片
代码实现: public class MainActivity extends AppCompatActivity { private ImageView img; @Override prot ...
- CSS布局---居中方法
在web页面布局中居中是我们常遇到的情况,而居中分为水平居中与垂直居中 文本的居中 CSS中对文本的居中做的非常友好,我们是需要text-align, line-height 两个属性就可以控制文本的 ...
- Spring入门详细教程(一)
一.spring概述 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用.Spring是于2003 年兴起的一个轻量级的 ...
- JS学习之路之JavaScript match() 方法
match() 方法,在字符串内找到相应的值并返回这些值,()内匹配字符串或者正则表达式. 该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置. d ...