poj2362 Square

来源:互联网 发布:拍照姿势的软件 编辑:程序博客网 时间:2024/05/17 00:52

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

yesno

yes

这题题意是给你一些边,看能够构成正方形,这题的数据比较水,为后面的poj1011埋下伏笔。

#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;int vis[30],liang,a[30],n;//liang表示每条边的长bool cmp(int a,int b){return a<b;}int dfs(int x,int pos,int len)//x表示已经拼了几根,pos表示下次从哪根开始拼,len表示当前拼的这根已经拼了多少长度{int i,j;if(x==3)return 1;for(i=pos;i>=1;i--){if(!vis[i]){if(a[i]+len<liang){vis[i]=1;if(dfs(x,i-1,len+a[i]))return 1;vis[i]=0;}else if(a[i]+len==liang){vis[i]=1;if(dfs(x+1,n,0))return 1;vis[i]=0;}}}return 0;}int main(){int m,i,j,T,sum;scanf("%d",&T);while(T--){scanf("%d",&n);sum=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);sum+=a[i];}if(sum%4!=0){printf("no\n");continue;}liang=sum/4;sort(a+1,a+1+n,cmp);memset(vis,0,sizeof(vis));if(dfs(0,n,0))printf("yes\n");else printf("no\n");}return 0;}


0 0
原创粉丝点击