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(二进制枚举)的更多相关文章

  1. UVA-12113 Overlapping Squares (回溯+暴力)

    题目大意:问能不能用不超过6张2x2的方纸在4x4的方格中摆出给定的图形? 题目分析:暴力枚举出P(9,6)种(最坏情况)方案即可. 代码如下: # include<iostream> # ...

  2. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  3. Good Bye 2015B(模拟或者二进制枚举)

    B. New Year and Old Property time limit per test 2 seconds memory limit per test 256 megabytes input ...

  4. Poj(2784),二进制枚举最小生成树

    题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  5. POJ 2436 二进制枚举+位运算

    题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...

  6. hdu 3118(二进制枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3118 思路:题目要求是去掉最少的边使得图中不存在路径长度为奇数的环,这个问题等价于在图中去掉若干条边, ...

  7. HDU 5025Saving Tang Monk BFS + 二进制枚举状态

    3A的题目,第一次TLE,是因为一次BFS起点到终点状态太多爆掉了时间. 第二次WA,是因为没有枚举蛇的状态. 解体思路: 因为蛇的数目是小于5只的,那就首先枚举是否杀死每只蛇即可. 然后多次BFS, ...

  8. 南阳OJ-91-阶乘之和---二进制枚举(入门)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=91 题目大意: 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为 ...

  9. 关于二进制枚举-计蒜客-得到整数X

    某君有 n个互不相同的正整数,现在他要从这 n 个正整数之中无重复地选取任意个数,并仅通过加法凑出整数 X.求某君有多少种不同的方案来凑出整数 X. 输入格式 第一行,输入两个整数 n,X(1≤n≤2 ...

随机推荐

  1. 【Tomcat】Tomcat的类加载机制

    在Tomcat中主要有以下几种类加载器:(图片来自网络) tomcat启动时,会创建几种类加载器: 1 Bootstrap 引导类加载器 加载JVM启动所需的类,以及标准扩展类,位于jre/lib/e ...

  2. Spring Security Oauth2 示例

    所有示例的依赖如下(均是SpringBoot项目) pom.xml <dependencies> <dependency> <groupId>org.springf ...

  3. 有哪些Java性能优化方法?

    面试官:"有性能优化经验没?"  应聘者:"有一点."   面试官:"那你们从哪些方面做了优化?"   应聘者:"sql优化.JV ...

  4. EJS-初识

    项目中使用了EJS,因此,也开始接触了EJS. EJS官方定义:it's just plain JavaScript. 总的来说,上手较快(毕竟我是个菜鸟). 第一步:安装: 第二部使用: 在html ...

  5. 如何创建.gitignore文件,忽略git不必要提交的文件

    touch .gitignore 在项目目录里输入以上名利后,会自动生成一个文件 .gitignore,可在文件里写入忽略的文件名,例如 node_modules coverage .idea npm ...

  6. K8S 调度器,预选策略,优选函数

    Kubernetes Scheduler 提供的调度流程分三步: 预选策略(predicate) 遍历nodelist,选择出符合要求的候选节点,Kubernetes内置了多种预选规则供用户选择. 优 ...

  7. K8S 部署 ingress-nginx (二) 部署后端为 tomcat

    在上面已经部署了 ingress-nginx, https://www.cnblogs.com/klvchen/p/9903480.html 创建 service 和 pods cd vi tomca ...

  8. [转]Javascript实现图片的预加载详解

    下面的函数实现了一个我们想要的最基本的图片预加载效果 function preloadimages(arr){ var newimages=[] var arr=(typeof arr!=" ...

  9. Java之static作用的全方位总结

    1.深度总结 引用一位网友的话,说的非常好,如果别人问你static的作用:如果你说静态修饰 类的属性 和 类的方法 别人认为你是合格的:如果是说 可以构成 静态代码块,那别人认为你还可以: 如果你说 ...

  10. 「Android」系统架构概述

    目录: 1.Android系统架构 2.Android类库 3.四大组件 --------------------------------------------------------------- ...