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 ...
随机推荐
- React.js基础入门
本文主要是针对React的一些demo教程.参考了菜鸟教程中的react教程,做了一些总结.Demo的下载链接是 https://github.com/RealAndMe/react-demo 下面要 ...
- mysql 复制表数据,表结构的3种方法
什么时候我们会用到复制表?例如:我现在对一张表进行操作,但是怕误删数据,所以在同一个数据库中建一个表结构一样,表数据也一样的表,以作备份.如果用mysqldump比较麻烦,备份.MYD,.MYI这样的 ...
- 6.redis的分布式锁
https://www.cnblogs.com/linjiqin/p/8003838.html
- bzoj千题计划117:bzoj1026: [SCOI2009]windy数
http://www.lydsy.com/JudgeOnline/problem.php?id=1026 数位DP 如果前一位填的是0, 0是前导0,下一位可以随便填 0不是前导0,下一位不能填1 为 ...
- okhttp 简单用法
1.gradle 依赖 github 中查找最新的 2.MyApplication oncreate 中: @Override public void onCreate () { super.onCr ...
- [php]php总结(1)
1.变量可以连续传递赋值2.var_dump()打印变量信息3.isset()与unset()4.可变变量$p = "temp";$$p则表示$temp变量,即最右边的变量的值为下 ...
- Bzoj1939 [Croatian2010] Zuma
Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 43 Solved: 31 Description 有一行 N 个弹子,每一个都有一个颜色.每次可以让超过 ...
- QByteArray储存二进制数据(包括结构体,自定义QT对象)
因为利用QByteArray可以很方便的利用其API对内存数据进行访问和修改, 构建数据库blob字段时必不可少; 那如何向blob内写入自定义的结构体和类 //自定义person结构体 typede ...
- 2016.5.19——Excel Sheet Column Title
Excel Sheet Column Title 本题收获: 1.由int型转换为整型(string),如何转化, res = 'A'+(n-1)%26和之前由A-z转化为十进制相反,res = s[ ...
- 高级C#信使(译) - Unity维基百科
高级C#信使 作者:Ilya Suzdalnitski 译自:http://wiki.unity3d.com/index.php/Advanced_CSharp_Messenger 描述 前言 Mis ...