7-15 Square Destroyer 破坏正方形 uva1603
先是处理所有的正方形 从边长为1开始 将其边存好 满边存好
然后不断扫描正方形 并且进行拆除 直到拆完或者 步数小于等于9(启发方程 因为n小于等于5 九次足以将所有的拆完)
代码实施有很多细节 lrj的代码非常巧妙 !
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; const int maxs = ; // number of squares: 25+16+9+4+1=55
const int maxm = ; // number of matches: 2*5*(5+1)=60 int n, exists[maxm]; // matches
int s, size[maxs], fullsize[maxs], contains[maxs][maxm]; // squares
int best; inline int row_match(int x, int y) {
return (*n+)*x+y;
} inline int col_match(int x, int y) {
return (*n+)*x+n+y;
} // number of matches in a full n*n grid
inline int match_count(int n) {
return *n*(n+);
} void init() {
int m, v;
scanf("%d%d", &n, &m);
for(int i = ; i < match_count(n); ++i) exists[i] = ;
while(m--) {
scanf("%d", &v);
exists[v-] = ;
} // collect full squares
s = ;
memset(contains, , sizeof(contains));
for(int i = ; i <= n; i++) // side length
for(int x = ; x <= n-i; x++)
for(int y = ; y <= n-i; y++) {
size[s] = ;
fullsize[s] = *i; // number of matches in a complete square
for(int j = ; j < i; j++) {
int a = row_match(x, y+j); // up
int b = row_match(x+i, y+j); // down
int c = col_match(x+j, y); // left
int d = col_match(x+j, y+i); // right
contains[s][a] = ;
contains[s][b] = ;
contains[s][c] = ;
contains[s][d] = ;
size[s] += exists[a] + exists[b] + exists[c] + exists[d]; // number of matches now
}
++s;
}
} int find_square() {
for(int i = ; i < s; i++)
if(size[i] == fullsize[i]) return i;
return -;
} void dfs(int dep) {
if(dep >= best) return; int k = find_square();
if(k == -) {
best = dep;
return;
} // remove a match in that square
for(int i = ; i < match_count(n); i++)
if(contains[k][i]) {
for(int j = ; j < s; j++)
if(contains[j][i]) size[j]--;
dfs(dep + );
for(int j = ; j < s; j++) //dfs切记改变了要变回来
if(contains[j][i]) size[j]++;
}
} int main() {
int T;
scanf("%d", &T);
while(T--) {
init();
best = n*n;
dfs();
printf("%d\n", best);
}
return ;
}
7-15 Square Destroyer 破坏正方形 uva1603的更多相关文章
- 破坏正方形UVA1603
题目大意 有一个由火柴棍组成的边长为n的正方形网格,每条边有n根火柴,共2n(n+1)根火柴.从上至下,从左到右给每个火柴编号,现在拿走一些火柴,问在剩下的后拆当中ongoing,至少还要拿走多少根火 ...
- 【例7-15 UVA-1603】Square Destroyer
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出所有的正方形(长度为1,2...n的)所包含哪些边. 然后记录每个正方形的应有边长和实际边长(有些边被删掉了); 然后搜的 ...
- (中等) POJ 1084 Square Destroyer , DLX+可重复覆盖。
Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The ...
- UVa 1603 破坏正方形
https://vjudge.net/problem/UVA-1603 题意:有一个火柴棍组成的正方形网格,计算至少要拿走多少根火柴才能破坏所有正方形. 思路:从边长为1的正方形开始遍历,将正方形的边 ...
- 【POJ】1084 Square Destroyer
1. 题目描述由$n \times n, n \in [1, 5]$的正方形由$2 \times n \times (n+1)$根木棍组成,可能已经有些木棍被破坏,求至少还需破坏多少木根,可以使得不存 ...
- UVA - 1603 Square Destroyer (DLX可重复覆盖+IDA*)
题目链接 给你一个n*n的由火柴组成的正方形网格,从中预先拿掉一些火柴,问至少还需要拿掉多少火柴才能破坏掉所有的正方形. 看到这道题,我第一反应就是——把每根火柴和它能破坏掉的正方形连边,不就是个裸的 ...
- 473 Matchsticks to Square 火柴拼正方形
还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到.输入为小女孩拥有火柴的 ...
- [DLX反复覆盖] poj 1084 Square Destroyer
题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...
- 50.Maximal Square(最大正方形)
Level Medium 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square conta ...
随机推荐
- 翻译:GLSL的顶点位移贴图
翻译:GLSL的顶点位移贴图 翻译自: Vertex Displacement Mapping using GLSL 译者: FreeBlues 说明: 之所以选择这篇文档, 是因为现在但凡提到位移贴 ...
- Git之原有基础开发新功能
场景描述 当一个项目已经上线,同时又在原有基础上新增功能模块,于是乎就要在原有代码的基础上进行开发,在新增模块功能的开发的过程中,项目发现了一个紧急Bug,需要修复.操作流程如下:
- Grep学习笔记
Grep(Global search Regular Expression and Print out the line)是一种强大的文本搜索工具. 1. 正则表达式的基本组成部分 正则表达式 描述 ...
- 手写简化版printf函数
2019.02.01更新:经同学提醒,myprintf函数应有返回值为输出的字符数. 期末的大作业,手写一个myprintf函数,支持如下一些操作. 也就是 % -(负号控制左右对齐) 数(控制字段 ...
- 20155210潘滢昊 2016-2017-2 《Java程序设计》第5周学习总结
20155210 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 try with resources 关闭多个资源时用分号分隔 java.lang.Auto ...
- .NET 下第一次接触Redis数据库
关于Redis 1.简介 Redis是著名的NOSQL(Not Only SQL)数据库,是键值对结构.(我只用过键值对结构的) 他为存储键值对做了优化,在大型网站中应用广泛.Redis提供了数据的自 ...
- 【多视图几何】TUM 课程 第1章 数学基础:线性代数
在 YouTube 上找到了慕尼黑工业大学(Technische Universitaet München)计算机视觉组 Daniel Cremers 教授的 Multiple View Geomet ...
- 戴尔游匣5577安装 ubuntu/mint
这里以mint为例. 做好usb启动盘后, 启动到读秒的时候按上下方向键唤出如下选项: 在第二个选项里按 tab 键(上图界面有提示) 进去后有如下界面: 在后面加上参数: nouveau.modes ...
- ### mysql系统结构_3_Mysql_Learning_Notes
mysql系统结构_3_Mysql_Learning_Notes 存储层,内存结构 全局(buferpool) 只分配一次 全局共享 连接/会话(session) 针对每个会话/线程分配 按需动态分配 ...
- C# 各种类型的转换
/// <summary> /// 一些常用的方法 /// 1.一些高效的转换方法 /// </summary> public class Util { #region Obj ...