## Input

The input consists of several test cases. The number of test cases (T) is given in the first line of the input file. Each test case consists of two lines: The first line contains a natural number n, not greater than 5, which implies you are given a (complete or incomplete) n×n grid as input, and the second line begins with a nonnegative integer k, the number of matchsticks that are missing from the complete n×n grid, followed by k numbers specifying the matchsticks. Note that if k is equal to zero, then the input grid is a complete n×n grid; otherwise, the input grid is an incomplete n×n grid such that the specified k matchsticks are missing from the complete n×n grid.

## Output

Print exactly one line for each test case. The line should contain the minimum number of matchsticks that have to be taken out to destroy all the squares in the input grid.

2
0
3
3 12 17 23

## Sample Ouput

3

3

``` #include <iostream>
#include <cstring>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>

using namespace std;
typedef long long LL;

const int maxn = ;
const int maxl = ;

LL sqr[maxn];
LL one_sqr[maxl][maxl];

int n,edge_cnt,sqr_cnt;
int maxd;

int get_hor(int r,int c){
return (*n+)*r+c+;
}

int get_ver(int r,int c){
return (*n+)*r+c+n+;
}

inline LL get_LL(int p){
return (LL) << (p-);
}

void init(){
scanf("%d",&n);
edge_cnt = *n*(n+);
sqr_cnt = ;
for(int i = ;i < n;i++){
for(int j = ;j < n;j++){
one_sqr[i][j] = get_LL(get_hor(i,j)) | get_LL(get_hor(i+,j)) | get_LL(get_ver(i,j)) | get_LL(get_ver(i,j+));
sqr[sqr_cnt++] = one_sqr[i][j];
}
}

for(int size = ;size <= n;size++){
for(int i = ;i+size <= n;i++){
for(int j = ;j+size <= n;j++){
sqr[sqr_cnt] = ;
for(int k = ;k < size;k++)
for(int l = ;l < size;l++)
sqr[sqr_cnt] ^= one_sqr[i+k][j+l];
sqr_cnt++;
}
}
}
}

bool dfs(int d,LL s){
LL t = s,sel = -;
int mind = ;
for(int i = ;i < sqr_cnt;i++){
if((t&sqr[i]) == sqr[i]){
t ^= sqr[i];
mind++;
if(sel == -) sel = sqr[i];
}
}
if(sel == -) return true;
if(d+mind > maxd) return false;

for(int j = ;j <= edge_cnt;j++){
if(sel & get_LL(j))
if(dfs(d+,s^(get_LL(j)))) return true;
}

return false;
}

int solve(){
LL s = ((LL)<<edge_cnt)-;
int t,x;
scanf("%d",&t);
while(t--){
scanf("%d",&x);
s ^= get_LL(x);
}

for(maxd = ;;maxd++){
if(dfs(,s)) break;
}
return maxd;
}

int main()
{
int iCase;
#ifdef GEH
freopen("helloworld.01.inp","r",stdin);
#endif
scanf("%d",&iCase);
while(iCase--){
init();
printf("%d\n",solve());
}
return ;
}```

## UVA1374-Power Calculus(迭代加深搜索)的更多相关文章

1. POJ 3134 Power Calculus (迭代剪枝搜索)

题目大意:略 题目里所有的运算都是幂运算,所以转化成指数的加减 由于搜索层数不会超过\$2*log\$层,所以用一个栈存储哪些数已经被组合出来了,不必暴力枚举哪些数已经被搜出来了 然后跑\$iddfs\$就行 ...

2. UVA-1374 Power Calculus （迭代加深搜索）

题目大意:问最少经过几次乘除法可以使x变成xn. 题目分析:迭代加深搜索. 代码如下: # include<iostream> # include<cstdio> # incl ...

3. 【算法•日更•第三十九期】迭代加深搜索：洛谷SP7579 YOKOF - Power Calculus 题解

废话不多说,直接上题: SP7579 YOKOF - Power Calculus 题意翻译 (略过没有营养的题干) 题目大意: 给出正整数n,若只能使用乘法或除法,输出使x经过运算(自己乘或除自己, ...

4. Power Calculus UVA - 1374 迭代加深搜索

迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...

5. POJ1129Channel Allocation[迭代加深搜索　四色定理]

Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

6. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

7. 迭代加深搜索 POJ 1129 Channel Allocation

POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

8. 迭代加深搜索 codevs 2541 幂运算

codevs 2541 幂运算  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...

9. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

## 随机推荐

1. 《DSP using MATLAB》示例Example5.8

代码: n = [0:1:99]; x = cos(0.48*pi*n) + cos(0.52*pi*n); n1 = [0:1:9]; y1 = x(1:1:10); % N = 10 figure ...

2. jquery遍历数组与筛选数组的方法

grepgrep()方法用于数组元素过滤筛选 grep(array,callback,invert)array:待过滤数组;callback:处理数组中的每个元素,并过滤元素,该函数中包含两个参数,第 ...

3. iOS 里面 NSTimer 防止 循环引用

使用NSTimer的类 #import "TBTimerTestObject.h" #import "TBWeakTimerTarget.h" @interfa ...

4. strip_tags() 函数剥去 HTML、XML 以及 PHP 的标签

定义和用法 strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签. 语法 strip_tags(string,allow) 参数 描述 string 必需.规定要检查的字符串. ...

5. C#世界中的委托

委托是C#最重要的特性之一,C#后面的所有特性基本都是建立在委托的基础上的. 1.C#委托是什么? 可以把C#的委托理解为函数的一个包装,它使得C#中的函数可以作为参数来被传递.如果你学过C++,可以 ...

6. Oracle游标循环更新数据案例

declare v_XTXMBH number; v_ZJZJZJRQ varchar2(40); cursor c_job is SELECT XT.XTXMBH AS XTXMBH, QJ.ZJZ ...

7. Spring3.0官网文档学习笔记（七）--3.4.2

3.4.2 依赖与配置的细节     3.4.2.1  Straight values (primitives, Strings, and so on)     JavaBeans PropertyE ...