HDU1518 Square

时间:2021-10-30 10:11:00
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define max(a, b)(a > b ? a : b)
#define N 30 int a[N], vis[N], n, k, f; void DFS(int s, int m, int d)//s为搜索起点 m为已组成的正方体的边数 d当前组成木棍的长度(正方体的一条边可由多个木棍组成)
{
int i;
if(m == )
{
f = ;
return ;
}
if(d == k)
DFS(, m + , );
for(i = s ; i < n ; i++)
{
if(!vis[i] && d + a[i] <= k)
{
vis[i] = ;
DFS(i + , m, d + a[i]);
vis[i] = ;
if(f == )
return ;
}
}
}
int main()
{
int t, sum, max, i;
scanf("%d", &t);
while(t--)
{
sum = f = max = ;
scanf("%d", &n);
for(i = ; i < n ; i++)
{
scanf("%d", &a[i]);
sum += a[i];
max = max(max, a[i]);
}
if(sum % != || max > sum / || n < )//正方体的边数为小数,长度最大的木棍大于正方体的边数。木棍总数小于4都不能组成正方体
printf("no\n");
else
{
k = sum / ;
memset(vis, , sizeof(vis));
DFS(, , );
if(f == )
printf("yes\n");
else
printf("no\n");
}
}
return ;
}