Poj 2362/DFS: determines whether a square can be formed

Source: Internet
Author: User

1011 simply change it.

# Include <iostream> # include <algorithm> Using   Namespace STD; # define Max 64 Int Sticks [Max]; Bool Used [Max]; Int Sticknum, Plen, N; Bool Compare ( Int A, Int B ){ Return A> B ;} // Match from beginindex. The next step is to match the stick of matchlen. hasmatch stick has been matched before.  Bool DFS (Int Beginindex, Int Matchlen, Int Hasmatch ){ If (Matchlen = 0) {hasmatch ++; If (Hasmatch = 4 ){ Return   True ;} For (Beginindex = 0; used [beginindex]; beginindex ++); used [beginindex] = True ; If (DFS (beginindex + 1, Plen-sticks [beginindex], hasmatch )) Return   True ; Used [beginindex] =False ; Return   False ;} Else { If (Beginindex> N-1) Return   False ; For ( Int I = beginindex; I <n; I ++ ){ If (Used [I]) Continue ; If (Sticks [I]> matchlen) Continue ;If (I> 0 & sticks [I] = sticks [I-1] &! Used [I-1]) Continue ; Used [I] = True ; If (DFS (I + 1, matchlen-sticks [I], hasmatch )) Return   True ; Used [I] = False ;}} //  Return   False ;} Int Main ( Int Argc, Char * Argv []) {// Freopen ("I: // in.txt", "r", stdin );  Int Sum = 0; Bool OK; Int I, J; Int C; scanf (" % D ", & C ); For (J = 0; j <C; j ++) {OK = False ; Scanf (" % D ", & N); sum = 0; For (I = 0; I <n; I ++) {scanf (" % D ", & Sticks [I]); sum + = sticks [I];} If (Sum % 4! = 0) {OK =False ;} Else {Sort (sticks, sticks + N, compare); Plen = sum/4; used [0] = True ; If (DFS (0, Plen-sticks [0], 0) {OK = True ;} // Used [0] = false; } Printf (" % S \ n ", OK? " Yes ":" No "); Memset (used, False , Sizeof (Used ));} Return 0 ;}

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.