这个题目的搜索思路在于对正方形从边长为1的标准开始找起,判断每个正方形在没有破坏情况下包含哪些边,以及计算出没有破坏的情况下的周长和当前情况下的周长,便于后续的搜索与判断。具体实现见如下代码:
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
using namespace std;
int ind;
int T,n,k;
int sticks[100];
int square_size[100];
int cur_size[100];
int contain[100][100];
int Depth;
int getId1(int x,int y){
return (2 * n + 1)*x + y;
}
int getId2(int x,int y){
return (2 * n + 1)*x + y + n;
}
void Init(){
ind = 0;
memset(square_size,0,sizeof(square_size));
memset(cur_size, 0, sizeof(cur_size));
for (int i = 0; i < 100; i++) sticks[i] = 1;
memset(contain,0,sizeof(contain));
for (int i = 0; i < k; i++){
int t;
cin >> t;
sticks[t-1] = 0;
}
for (int i = 1; i <= n; i++){
for (int x = 0; x <= n - i; x++){
for (int y = 0; y <= n - i; y++){
square_size[ind] = 4 * i;
cur_size[ind] = 0;
for (int j = 0; j < i; j++){
int a, b, c, d;
a = getId1(x, y + j);
b = getId1(x + i, y + j);
c = getId2(x + j, y);
d = getId2(x + j, y + i);
contain[ind][a] = 1;
contain[ind][b] = 1;
contain[ind][c] = 1;
contain[ind][d] = 1;
cur_size[ind] += sticks[a] + sticks[b] + sticks[c] + sticks[d];
}
ind++;
}
}
}
}
int find_exist(){
for (int i = 0; i < ind; i++){
if (square_size[i] == cur_size[i]) return i;
}
return -1;
}
bool Solve(int cur_d){
int id = find_exist();
if (id == -1) return true;
if (cur_d >= Depth) return false;
for (int i = 0; i < 2 * n*(n + 1); i++){
if (contain[id][i]){
for (int j = 0; j < ind; j++){
if (contain[j][i]) cur_size[j]--;
}
if (Solve(cur_d + 1)) return true;
for (int j = 0; j < ind; j++){
if (contain[j][i]) cur_size[j]++;
}
}
}
return false;
}
int main(){
int T;
cin >> T;
while (T--){
cin >> n >> k;
Init();
for (Depth = 1;; Depth++){
if (Solve(0)) break;
}
cout << Depth << endl;
}
return 0;
}