hdu1518 (Square) deep search + pruning

Source: Internet
Author: User

Click to open Hangzhou electric 1518

Problem Descriptiongiven a set of sticks of various lengths, is it possible to join them end-to-end to form a square?

Inputthe first line of input contains N, the number of test cases. Each test case is begins with an integer 4 <= M <=, the number of sticks. M integers follow; Each gives the length of a stick-an integer between 1 and 10,000.

Outputfor each case, the output a line containing "yes" if was is the 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

Test instructions: Judging whether the given number of numbers can form a positive quadrilateral

Idea: From the test instructions can be seen, to become a positive quadrilateral, then must be given the sum of the number and can not be divided into 4, and the smallest number of the given number can not be greater than the side length of the quadrilateral. And then through the deep search through each number, until the length of the combination into a side length, and finally through the statistical composition of the number of edge length, determine whether the condition is satisfied

Import Java.util.scanner;public class P1518 {public static int m,edglen;public static int[] visit;public static int[] A;pu Blic static Boolean flag;public static void Main (string[] args) {Scanner sc=new Scanner (system.in); int n=sc.nextint (); whi Le (n-->0) {m=sc.nextint (); A=new int[m];visit=new int[m];int sum=0;flag=false;for (int i=0;i<m;i++) {a[i]= Sc.nextint (); sum+=a[i];} if (sum%4!=0) {//pruning, sum cannot be divisible by 4, you must not make up System.out.println ("no"); continue;} Edglen=sum/4;sort (); if (Edglen<a[0]) {//the smallest is greater than the length of the edge, minus System.out.println ("no"); continue;} DFS (0,a[0],0); if (flag) {System.out.println ("yes");} Else{system.out.println ("No");}}} private static void DFS (int edglennumb, int edg, int i) {int k=i;visit[i]=1;if (edg==edglen) {edglennumb++;k=0;edg=0;} if (edglennumb==3) {Flag=true;return;} for (int j=k;j<m;j++) {if (visit[j]==0 && edg+a[j]<=edglen) {DFS (edglennumb,a[j]+edg,j); if (flag) {return ;}}} visit[i]=0;} private static void sort () {for (int. i=0;i<m-1;i++) {for (int j=0;j<m-1-i;j++) {if (a[i]>a[j]) {int T=a[i];a[i]=a[j];a[j]=t;}}}} 



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1518 (Square) deep search + pruning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.