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 ...
随机推荐
- Nlog写日志到数据库
https://github.com/nlog/NLog/wiki/Database-Target
- 织梦自定义表单通过ajax提交的实现方法
自定义表单通过ajax判断,提交不用跳转页面,提高用户体验.具体方法如下: html表单代码部分,就提交按钮改成botton,,添加onclick事件 表单代码: <form action=&q ...
- why inline functions must be put in header files?
[why inline functions must be put in header files?] 编译中有2个过程:compile.link.先进行compile,compile中把源代码编译成 ...
- weblogic 反序列化补丁绕过漏洞的一个批量检测shell脚本(CVE-2017-3248 )
~ 以下内容,仅供学习参考 ~ weblogic 反序列化补丁绕过漏洞已经出了两个月了,balabala ~~~ 废话不说,拿到该漏洞的利用工具weblogic.jar,但只能一个个检测ip和端口,效 ...
- python练习-Socket实现远程cmd命令
需求:基于tcp的套接字实现远程执行命令的操作 代码示例: # 编辑者:闫龙 #Client端部分 import socket #导入骚凯特模块 CmdObj = socket.socket(sock ...
- datatable表格框架服务器端分页查询设置
更多内容推荐微信公众号,欢迎关注: js代码如下: $('#mytable').dataTable( { "bServerSide": true, //开启服务器模式,使用服务器端 ...
- 【疑点】<p></p>标签为什么不能包含块级标签?还有哪些特殊的HTML标签?
最近,在码代码的时候,就是下面的这段代码,我犯了一个很不起眼,但犯了就致命的BUG. <body> <p> <ol> <li>Hello</li& ...
- jQuery和Prototype的兼容性和冲突的五种解决方法
第一种情况:先加载Prototype,再加载jQuery方法一:jQuery 库和它的所有插件都是在jQuery名字空间内的,包括全局变量也是保存在jQuery 名字空间内的. 使用jQuery.no ...
- Html5使用history对象history.pushState()和history.replaceState()方法添加和修改浏览历史记录
根据网上参考自己做个笔记:参考网址:http://javascript.ruanyifeng.com/bom/history.html history.pushState() HTML5为histor ...
- Unity3D Instantiate慢的问题
1.NGUI直接打开界面卡 2.角色放技能的时候卡 3.载入模型的时候卡 http://www.xuanyusong.com/archives/2925