zoukankan      html  css  js  c++  java
  • poj2362 Square

    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

    3
    4 1 1 1 1
    5 10 20 30 40 50
    8 1 7 2 6 4 4 3 5
    

    Sample Output

    yes
    no
    

    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
    ");continue;
    		}
    		liang=sum/4;
    		sort(a+1,a+1+n,cmp);
    		memset(vis,0,sizeof(vis));
    		if(dfs(0,n,0))printf("yes
    ");
    		else printf("no
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    正则表达式
    JavaIO详解
    Java集合类详解
    Java虚拟机原理图解
    关于Java中按值传递和按引用传递的问题详解
    图的建立(邻接矩阵)+深度优先遍历+广度优先遍历+Prim算法构造最小生成树(Java语言描述)
    The 70th problem,UVa10396 Vampire Numbers
    二叉树的实现(Java语言描述)
    编程之路
    AngularJS 开发中常犯的10个错误
  • 原文地址:https://www.cnblogs.com/herumw/p/9464697.html
Copyright © 2011-2022 走看看