https://vjudge.net/problem/UVA-1603

题目

The left figure below shows a complete$3 \times 3$grid made with2$ \times(3 \times 4)$(=24) matchsticks. The lengthsof all matchsticks are one. You can find many squares of different sizes in the grid. The size of a squareis the length of its side. In the grid shown in the left figure, there are 9 squares of size one, 4 squaresof size two, and 1 square of size three.Each matchstick of the complete grid is identified with a unique number which is assigned from leftto right and from top to bottom as shown in the left figure. If you take some matchsticks out from thecomplete grid, then some squares in the grid will be destroyed, which results in an incomplete $3\times 3$ grid. The right figure illustrates an incomplete $3\times 3$ grid after removing three matchsticks numberedwith 12, 17 and 23. This removal destroys 5 squares of size one, 3 squares of size two, and 1 square of size three. Consequently, the incomplete grid does not have squares of size three, but still has 4 squaresof size one and 1 square of size two.

As input, you are given a (complete or incomplete) $n \times n$ grid made with no more than $2n(n + 1)$ matchsticks for a natural number $n \leqslant 5$. Your task is to compute the minimum number of matchsticks taken out to destroy all the squares existing in the input $n \times n$ grid.

题解

做这个题做到自闭……

首先判断正方形的部分就不好写(写了3小时)……然后搜索的复杂度也猜不出来,状态数高达$2 ^ {220}$,BFS存不下状态

换用迭代加深搜,一直TLE

TLE代码

#include<bits/stdc++.h>
using namespace std;
#define REP(i,x,y) for(register int i=(x); i<(y); i++)
#define REPE(i,x,y) for(register int i=(x); i<=(y); i++)
#define MAXN 30
#ifdef sahdsg
#define DBG(...) printf(__VA_ARGS__);
#else
#define DBG(...)
#endif
typedef pair<int,int> pii;
bool arr[220];
int md;
int n,tot;
int counter(bool *arr, pii *stick) {int s=0;bool k=true;k=true;REPE(sz,1,n) {REPE(ax,0,n-sz) REPE(ay,0,n-sz) {k=true;REP(i,0,sz) {if(arr[ax+ay*(n+n+1)+i]) {k=false;break;}}if(k)REP(i,0,sz) {if(arr[ax+(ay+i)*(n+n+1)+n] || arr[ax+(ay+i)*(n+n+1)+n+sz]) {k=false;break;}}if(k)REP(i,0,sz) {if(arr[ax+(ay+sz)*(n+n+1)+i]) {k=false;break;}}if(k){s++;REPE(bx,0,n-sz) REPE(by,0,n-sz) {REP(i,0,sz) {stick[bx+by*(n+n+1)+i].first++;}REP(i,0,sz) {stick[bx+(by+i)*(n+n+1)+n].first++;stick[bx+(by+i)*(n+n+1)+n+sz].first++;}REP(i,0,sz) {stick[bx+(by+sz)*(n+n+1)+i].first++;}}}}}return s;
}
bool solve(int d, bool *oarr) {pii stick[220];REP(i,0,tot) stick[i].second=i,stick[i].first=0;int s=counter(oarr,stick);if(s==0) return true;sort(stick,stick+tot,greater<pii>());if(stick[0].first==1) {md+=s;return true;}int sum=0, k=999999;REP(i,0,tot) {sum+=stick[i].first;if(sum>=s) {k=i+1; break;}}if(k+d>md) return false;REP(i,0,tot) {if(oarr[stick[i].second]) continue;oarr[stick[i].second]=1;if(solve(d+1,oarr)) return true;oarr[stick[i].second]=0;}return 0;
}
int main() {
#ifdef sahdsgfreopen("in.txt", "r", stdin);
#endifint T;scanf("%d",&T);while(0<T--) {scanf("%d",&n);tot=2*n*(n+1);int d;scanf("%d",&d);memset(arr,0,sizeof arr);REP(i,0,d) {int t;scanf("%d",&t);arr[t]=1;}for(md=0; ; md++) {DBG("#%d", md);if(solve(0,arr)) {printf("%d\n", md);break;}}}return 0;
}

看了答案 最重要的判断正方形的部分太慢了……

需要预处理,对每个正方形编号,然后保存每个完整正方形的火柴数目,然后维护正方形的实时火柴棍数目,或者使用DLX算法

(今天去听大学的意义的讲座,也是服了,为什么要去听这种讲座浪费人参啊……)

转载于:https://www.cnblogs.com/sahdsg/p/10459064.html

UVA 1603 Square Destroyer相关推荐

  1. Square Destroyer UVA - 1603 IDA*

    紫书的代码 写起来很麻烦 以后自己独立再写. 把完整图里面的正发形都枚举出来,算出每一个正方形是否差了边 然后ida*一直搜就好了    有意思的是 ida*的maxd是放在dfs()里面实现的 内附 ...

  2. UVa 201 Square

    这道题,没什么,就查找正方形, 特别注意一下 \n 的使用. //UVa 201 //Square //#define LOCAL #include <stdio.h> #include ...

  3. xor方程组消元 UVA 11542 Square

    题目传送门 题意:给n个数,选择一些数字乘积为平方数的选择方案数.训练指南题目. 分析:每一个数字分解质因数.比如4, 6, 10, 15,, , , , 令,表示选择第i个数字,那么,如果p是平方数 ...

  4. 【例7-15 UVA-1603】Square Destroyer

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出所有的正方形(长度为1,2...n的)所包含哪些边. 然后记录每个正方形的应有边长和实际边长(有些边被删掉了); 然后搜的 ...

  5. UVA 356 - Square Pegs And Round Holes

    题目:在一个2n*2n的网格中间画一个直径为2n-1的圆,问圆内部的格子以及和圆相交的格子个数. 思路:只要考虑1 / 4圆的点就行,用点到原点距离与半径比较,当格子左下方和右上方都在格子里时,格子在 ...

  6. UVA 356 - Square Pegs And Round Holes

    求圆的4分之1的结果乘4即可,比较点到圆心距离和半径 代码如下: #include <bits/stdc++.h>using namespace std; int n; double A( ...

  7. UVA, 356 Square Pegs And Round Holes

    题目: 给一个n.构建一个2n*2n的方格.里面放一个半径为n的圆.求落在边上的和完全在内部的格子个数 注意:输出的时候.几组数之间有空行.最后一组后面没有 只需要算1/4的.由于对称性 #inclu ...

  8. UVA 11542 Square(求增广矩阵的秩)

    题目链接 给定素因子不大于500的n(n<=100)个正整数,从中选出任意个,其乘积是完全平方数,问有都少中选法. 分析:若直接枚举所有子集,有2^n种难以承受,换种思路,我们可以用Xi来表示是 ...

  9. UVA 12520 Square Garden

    https://vjudge.net/problem/UVA-12520 题意:n*n网格中染色m个格子,染色格子的最长轮廓线 贪心 将格子分为4类 1.隔一个选一个,互不相邻的格子 2.4个角上的格 ...

最新文章

  1. 浅谈对html css的理解,HTML+CSS入门 CSS选择器 、属性和值浅谈
  2. C语言如何实现泛型编程?
  3. JAVa读取文件的附加属性,Java 读取属性文件
  4. SpringDataRedis环境搭建(详细图文教程)
  5. vue生成包报错error from UglifyJs
  6. JavaScript正则表达式的坑很深
  7. 自动驾驶领域常用的数据集(检测,分割,定位)
  8. python爬虫实例
  9. geotoolkit 测井曲线显示时间、深度双刻度
  10. python django 商品进销存管理系统(毕设、课设、学习)
  11. Java实现的快递柜管理系统(附上完整代码可用作期末课程设计)
  12. 2021-03-21
  13. Linux积累 - scp 远程复制 加端口
  14. 项目管理:PMP、IPMP、CPMP之间区别
  15. 恶魔猎手(Demon Hunter)尤迪安(Illidan)的点点滴滴
  16. 防火墙iptables原理及简单应用
  17. 精英云集,看国内外12家顶级公司大数据实践
  18. c/c++ 洛谷 P5035 金坷垃
  19. php中db是指什么意思,phpmyadmin的作用是什么意思_数据库
  20. 基于51单片机的烟雾报警器设计

热门文章

  1. MySQL中幻读现象
  2. 关键业务服务器存在的“硬道理”
  3. How Broswer Work
  4. 最简单的直播源抓取方法
  5. 纯前端实现xls表格下载
  6. VMware安装win7后,安装VMware Tools时报错安装程序无法继续
  7. excel学习07(笔记自用)
  8. Unity中碰撞检测小结
  9. 教程:这两个小妙招手把手教会你白底图片怎么抠图
  10. 哲学家进餐_我如何通过预算学习计划进餐