先是处理所有的正方形 从边长为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的更多相关文章

  1. 破坏正方形UVA1603

    题目大意 有一个由火柴棍组成的边长为n的正方形网格,每条边有n根火柴,共2n(n+1)根火柴.从上至下,从左到右给每个火柴编号,现在拿走一些火柴,问在剩下的后拆当中ongoing,至少还要拿走多少根火 ...

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

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

  3. (中等) POJ 1084 Square Destroyer , DLX+可重复覆盖。

    Description The left figure below shows a complete 3*3 grid made with 2*(3*4) (=24) matchsticks. The ...

  4. UVa 1603 破坏正方形

    https://vjudge.net/problem/UVA-1603 题意:有一个火柴棍组成的正方形网格,计算至少要拿走多少根火柴才能破坏所有正方形. 思路:从边长为1的正方形开始遍历,将正方形的边 ...

  5. 【POJ】1084 Square Destroyer

    1. 题目描述由$n \times n, n \in [1, 5]$的正方形由$2 \times n \times (n+1)$根木棍组成,可能已经有些木棍被破坏,求至少还需破坏多少木根,可以使得不存 ...

  6. UVA - 1603 Square Destroyer (DLX可重复覆盖+IDA*)

    题目链接 给你一个n*n的由火柴组成的正方形网格,从中预先拿掉一些火柴,问至少还需要拿掉多少火柴才能破坏掉所有的正方形. 看到这道题,我第一反应就是——把每根火柴和它能破坏掉的正方形连边,不就是个裸的 ...

  7. 473 Matchsticks to Square 火柴拼正方形

    还记得童话<卖火柴的小女孩>吗?现在,你知道小女孩有多少根火柴,请找出一种能使用所有火柴拼成一个正方形的方法.不能折断火柴,可以把火柴连接起来,并且每根火柴都要用到.输入为小女孩拥有火柴的 ...

  8. [DLX反复覆盖] poj 1084 Square Destroyer

    题意: n*n的矩形阵(n<=5),由2*n*(n+1)根火柴构成,那么当中会有非常多诸如边长为1,为2...为n的正方形,如今能够拿走一些火柴,那么就会有一些正方形被破坏掉. 求在已经拿走一些 ...

  9. 50.Maximal Square(最大正方形)

    Level   Medium 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square conta ...

随机推荐

  1. jvm内存模型(运行时数据区)

    运行时数据区(runtime data area) jvm定义了几个运行时数据区,这些运行时数据区存储的数据,供开发者的应用或者jvm本身使用.按线程共享与否可以分为线程间共享和线程间独立. 线程间独 ...

  2. bzoj千题计划169:bzoj2463: [中山市选2009]谁能赢呢?

    http://www.lydsy.com/JudgeOnline/problem.php?id=2463 n为偶数时,一定可以被若干个1*2 矩形覆盖 先手每次从矩形的一端走向另一端,后手每次走向一个 ...

  3. poj 1182/codevs 1074 食物链

    http://poj.org/problem?id=1182 http://codevs.cn/problem/1074/ 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B ...

  4. Git Cannot rebase: You have unstaged changes.

    Cannot rebase: You have unstaged changes. 那说明你有修改过的文件git stashgit pull --rebase (每次push之前最好这样做一次)git ...

  5. soj2013.Pay Back

    2013. Pay Back Constraints Time Limit: 1 secs, Memory Limit: 256 MB Description "Never a borrow ...

  6. jQuery技巧笔记

    1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...

  7. [gym100956]Problem J. Sort It! BIT+组合数

    source : Pertozavodsk Winter Training Camp 2016 Day 1: SPb SU and SPb AU Contest, Friday, January 29 ...

  8. sort函数(cmp)、map用法---------------Tju_Oj_2312Help Me with the Game

    这道题里主要学习了sort函数.sort的cmp函数写法.C++的map用法(其实和数组一样) Your task is to read a picture of a chessboard posit ...

  9. C语言编写守护进程

    概念 守护进程(daemon)是一种运行在后台的一种特殊的进程,它独立于控制终端并且周期性的执行某种任务或等待处理某些发生的事件.由于在Linux中,每个系统与用户进行交流的界面成为终端,每一个从此终 ...

  10. 【C++】数组-二分法查找

    1.原理 对于给定值的查找,如果大于该数组的中间元素,下一步在元素值大的区域继续与其中间元素比较:否则下一步在元素值小的区域内继续查找,直到找到目标元素.如果到最后还没有找到,则输出"数组中 ...