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. Flask 系列之 Bootstrap-Flask

    说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 通过使用 Bootstrap-Flask 来进行页面美化 ...

  2. PHP7.27: Cookie and Session

    <?php // 有的浏览器不支持Cookie,这要考虑的 $cFile="count.txt"; $acctime=time(); if(file_exists($cFil ...

  3. 《JavaScript高级程序设计》笔记:在HTML中使用Javascript(二)

    script元素 向html页面中插入js的主要方法就是使用<script>元素.使用<script>元素的方式有两种:直接在页面中嵌入js代码和包含外部js文件.直接在页面中 ...

  4. Python变量和简单数据类型

    变量的命名和使用 在Python中使用变量时 ,需要遵守一定的规则和指南. 变量名只能包含字母‘数字和下划线 变量名不能包含空格,但可以用下划线分割其中单词 不要将Python关键字和函数名用作变量名 ...

  5. Android项目实战(五十一):浅谈GreenDao

    比较出名的数据库框架 GreenDao使用步骤: 1.app目录下的build.gradle文件 添加依赖 compile 'org.greenrobot:greendao:3.2.0' 顶部添加插件 ...

  6. VS code 配置为 Python R LaTeX IDE

    VS code配置为Python R LaTeX IDE VS code的中文断行.编辑功能强大,配置简单. VSC的扩展在应用商店搜索安装,快捷键ctrl+shift+x调出应用商店. 安装扩展后, ...

  7. Android-textview图文混排(网络图片)

    工作太忙,不做过多的解释了,核心是用到了 SpannableStringBuilder  Glide  和 Rxjava 直接上代码了,就两个类. public class ImageSpanAsyn ...

  8. Spring Boot 特性以及代码模板实例

    SpringBoot工程 参数解析 HTTP Method Request / Response / Session Error/重定向 Logger IoC AOP/Aspect   1:Sprin ...

  9. CSS之表格边框合并、兄弟标签外边距合并、父子标签的外边距合并

    本文内容: 表格边框合并 兄弟标签外边距合并 父子标签的外边距合并 首发日期:2018-05-01 表格边框合并: 发生情况: 当设置了cellpadding="0" cellsp ...

  10. javascript的隐式类型转换

    首先简单了解js的typeof,会返回六种类型 即 number string boolen function object undefined 也就是六种基本数据类型 显示类型转换大概有以下几种: ...