HDU1518 Square
#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 == 4) { f = 1; return ; } if(d == k) DFS(0, m + 1, 0); for(i = s ; i < n ; i++) { if(!vis[i] && d + a[i] <= k) { vis[i] = 1; DFS(i + 1, m, d + a[i]); vis[i] = 0; if(f == 1) return ; } } } int main() { int t, sum, max, i; scanf("%d", &t); while(t--) { sum = f = max = 0; scanf("%d", &n); for(i = 0 ; i < n ; i++) { scanf("%d", &a[i]); sum += a[i]; max = max(max, a[i]); } if(sum % 4 != 0 || max > sum / 4 || n < 4)//正方体的边数为小数,长度最大的木棍大于正方体的边数。木棍总数小于4都不能组成正方体 printf("no\n"); else { k = sum / 4; memset(vis, 0, sizeof(vis)); DFS(0, 1, 0); if(f == 1) printf("yes\n"); else printf("no\n"); } } return 0; }