HDU 1581 square (DFS (deep first search ))

Source: Internet
Author: User
Square

Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 6032 accepted submission (s): 1937

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 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.

Outputfor each case, output a line containing "yes" if 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
Question: Can a square be formed? Train of Thought: DFS algorithm method 1
Import Java. io. *; import Java. util. *; public class main {public static Boolean mark []; public static integer a []; public static Boolean boo; public static int edge, sum; public static int n, m; public static void main (string [] ARGs) {consumer SC = new consumer (New bufferedinputstream (system. in); printwriter PW = new printwriter (New bufferedoutputstream (system. out), true); n = SC. nextint (); For (INT I = 0; I <n; I ++) {M = SC. n Extint (); boo = false; Mark = new Boolean [m]; A = new integer [m]; sum = 0; edge = 0; For (Int J = 0; j <m; j ++) {A [J] = SC. nextint (); sum + = A [J];} arrays. sort (A, new comparator <integer> () {// sort public int compare (integer O1, integer O2) {return O1> O2? -; // Sort by descending order}); edge = sum> 2; // divide the right one by 2if (sum % 4 = 0 & edge> = A [0]) {DFS (0, 0); If (BOO) PW. println ("yes"); elsepw. println ("no");} else {PW. println ("no") ;}}// the first variable has several edges, and the second variable is the length of the edge of each loop, the third variable is the public static void DFS (INT length, int size, int index) element. {// if it is true, return if (BOO) return; // If (length = 4) {boo = true;} If (size = edge) {DFS (Length + 1, 0, length + 1);} else {for (INT I = index; I <m; I ++) {If (! Mark [I] & size + A [I] <= edge) {mark [I] = true; DFS (length, size + A [I], I + 1 ); mark [I] = false; // backtracking }}}}}
Method 2
Import Java. io. *; import Java. util. *; public class main {public static int n, m, sum, edge; public static Boolean mark []; public static int A []; public static void main (string [] ARGs) {consumer SC = new consumer (New bufferedinputstream (system. in); printwriter PW = new printwriter (New bufferedoutputstream (system. out), true); n = SC. nextint (); For (INT I = 0; I <n; I ++) {M = SC. nextint (); Mark = new Boolean [m]; A = new int [m]; sum = 0; f Or (Int J = 0; j <m; j ++) {A [J] = SC. nextint (); sum + = A [J];} edge = sum> 2; // shifts one digit to the right by 2arrays. sort (a); // sort if (sum % 4 = 0 & edge> = A [. length-1]) {// The side length must be greater than or equal to the maximum value of the elements in the array if (DFS (0, 0) PW. println ("yes"); elsepw. println ("no");} elsepw. println ("no") ;}// the first variable has several edges, and the second variable is the length of the edge of each loop, the third variable is the public static Boolean DFS (INT length, int size, int index) element. {// exit the loop if (length = 4) if it is equal to four edges) return true; If (size = edge) {If (DFS (Length + 1, 0, Length + 1)) Return true; elsereturn false;} else {for (INT I = index; I <m; I ++) {If (! Mark [I] & size + A [I] <= edge) {mark [I] = true; If (DFS (length, size + A [I], I + 1) return true; Mark [I] = false ;}}return false ;}}

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.