poj_2362
来源:互联网 发布:大数据概念的首次提出 编辑:程序博客网 时间:2024/05/21 12:08
Square
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 19036 Accepted: 6600
Description
Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square?
Input
The first line of input contains N, the number of test cases. Each test case begins with an integer 4 <= M <= 20, the number of sticks. M integers follow; each gives the length of a stick - an integer between 1 and 10,000.
Output
For each case, output a line containing "yes" if is is possible to form a square; otherwise output "no".
Sample Input
34 1 1 1 15 10 20 30 40 508 1 7 2 6 4 4 3 5
Sample Output
yesnoyes
#include <stdio.h>#include <string.h>int arr[21];int m, bian;int he;int visit[21];int f;void dfs(int st, int len, int sum){ if (sum >= 3) { f = 1; return ; } if (f) return ; if (len > bian) return ; if (len == bian) { dfs(0, 0, sum + 1); } int i; for (i = st; i < m; i++) { if (!visit[i]) { visit[i] = 1; dfs(i + 1, len + arr[i], sum); visit[i] = 0; } }}int main(){ int n; scanf("%d", &n); while (n--) { memset(visit, 0, sizeof(visit)); he = f = 0; int i; scanf("%d", &m); int sum = 0; for (i = 0; i < m; i++) { scanf("%d", &arr[i]); sum += arr[i]; } if (sum % 4 != 0) { printf("no\n"); continue; } bian = sum / 4; dfs(0, 0, 0); if (f) { printf("yes\n"); } else printf("no\n"); } return 0;}
0 0