HDU 3325 arithmetically challenged (DFS full row + brute force enumeration cracking + set Set Application)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3325

Arithmetically challenged

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 331 accepted submission (s): 139

Problem descriptionchallenge 24 is a popular mathematics game used in your grade schools. In each game, contestants are given a card with four positive integers I1, I2, I3, I4 On it, and the first one who can use all of these numbers and any combination of the four basic arithmetic operations to get 24 wins. Each of the numbers I1, I2, I3, I4 Must be used exactly once. division can be used only if the divisor evenly divides the dividend (I. E ., you can perform 6/2 but not 6/4 ). for example, if the card contains the numbers 7, 2, 5 and 1, possible solutions are (7-2) * 5-1 or (7 + 1) * (5-2 ). hmmm... this sounds like a source of a good programming problem.

Write a program that determines the longest consecutive sequence of integers that can be obtained by different ways of arithmetically combining the four integers. for example, with 7, 2, 5 and 1 the longest consecutive sequence is-18 to 26 (yes, we're allowing final results to be negative ). the "+" and "-" operators must be used as binary operators, not as unary signs.

Inputeach test case will consist of a single line containing the four, not necessarily distinct, positive integers, none of which will exceed 100. A line containing four 0's will terminate input. outputfor each test case, output the case number and the longest consecutive sequence of obtainable values, in the format shown in the sample output. if there is more than one longest consecutive sequence, use the one with the largest first value. sample input7 2 5 18 15 38 30 0 0 0 sample outputcase 1:-18 to 26 Case 2: 150 to 153 Question: There are only four numbers for this question, and four may be in full order! = 24. You can use DFS to solve the problem in full arrangement. After the solution is complete, brute-force cracking is triggered. There are 6 + cases for each brute-force cracking of the results in full arrangement, the six situations are a/B, B/A, A + B, a * B, a-B, and B-A, but these six situations cannot cover all situations, in another case, (A @ B) @ (c @ D) the operator "+-*/" here uses @ to indicate that the computing time complexity here is: 4! * 6*6*6 = 5184 is acceptable. In addition, if we use the data stored in the Set set, the Set set will automatically eliminate the number of duplicates and sort the data in order, it's easy to use. here we can use map instead. Code Implementation:
1 # Include <iostream> 2 # Include <stdio. h> 3 # Include <math. h> 4 # Include <algorithm> 5 # Include < String . H> 6 # Include < String > 7 # Include <ctime> 8 # Include <queue> 9 # Include <list>10 # Include <map> 11 # Include < Set > 12   # Define INF 999999999 13   # Define Maxn 100000 14   Using   Namespace  STD;  15   Int B [ 10 ], Mark [10 ], [ 10 ], Arr [maxn + 1  ], Pos;  16   Set < Int > SS;  17   18   //  Perform computation based on six computation Conditions  19   Int Fenjie ( Int A, Int B, Int  P)  20   {  21       If (P = 0  )  22           Return A + B;  23       Else   If (P = 1  )  24          Return A- B;  25       Else   If (P = 5  )  26           Return A * B;  27       Else   If (P = 3  )  28   { 29           If (! B | A % B! = 0  )  30               Return INF; //  When the Division rule first determines whether the Division is 0, this is necessary, otherwise re  31           Return A/ B;  32   }  33       Else   If (P =4  )  34   {  35           If (! A | B %! = 0  )  36               Return INF; //  Same as above  37           Return B/ A;  38   } 39       Else   If (P = 2  )  40           Return B- A;  41   }  42   43   Void  Baoli ()  44   {  45      Int  I, J, K, temp2, sum1, sum2, sum3;  46       For (I = 0 ; I < 6 ; I ++ )  47   {  48           If (Sum1 = fenjie (A [B [ 0 ], A [B [ 1 ], I) = inf) //  1. handle illegal requests 49               Continue  ;  50           For (J = 0 ; J < 6 ; J ++ )  51   {  52               If (Sum2 = fenjie (sum1, a [B [ 2 ], J) = inf) //  2  53                  Continue  ;  54               For (K = 0 ; K < 6 ; K ++ )  55   {  56                   If (Sum3 = fenjie (sum2, a [B [ 3 ], K ))! = Inf) //  3  57  SS. insert (sum3 );  58                   //  1 2 3 is an enumeration of the first six possible brute force attacks.  59                   60                   //  Processing of the last (A @ B) @ (c @ D)  61                   If (Temp2 = fenjie (A [B [ 2 ], A [B [ 3 ], K) = INF)  62                       Continue ;  63                   If (Sum3 = fenjie (sum1, temp2, j) = INF)  64                       Continue  ;  65   SS. insert (sum3 );  66   }  67   }  68   }  69  }  70   71   //  DFS arranges all Arrays  72   Void DFS ( Int  S)  73   {  74       Int  I;  75       If (S> = 4 )  76   {  77   Baoli ();  78   }  79       Else  80   {  81           For (I = 0 ; I < 4 ; I ++ )  82  {  83               If (! Mark [I])  84   {  85 Mark [I] = 1  ;  86 B [s] = I;  87 DFS (S + 1  );  88 Mark [I] =0  ;  89   }  90   }  91   }  92   }  93   Int  Main ()  94   {  95       Int SB =1  ;  96       While (~ Scanf ( "  % D  " , & [ 0 ], & [ 1 ], & [ 2 ], & [ 3  ])  97   {  98           If ([0 ] + [ 1 ] + [ 2 ] + [ 3 ] = 0  )  99               Break  ;  100           Int  I;  101 Memset (mark, 0 , Sizeof  (Mark )); 102 Pos = 0  ;  103   SS. Clear ();  104 DFS ( 0  );  105           While (! SS. Empty ())  106   {  107 Arr [POS ++] = * SS. Begin (); 108   SS. Erase (ss. Begin ());  109   }  110           Int Maks = 0 , CNT = 0 , ED = 0  ;  111           For (I = 1 ; I <Pos; I ++ )  112  {  113               If (ARR [I] = arr [I- 1 ] + 1  )  114 CNT ++ ;  115               Else  116 CNT = 0  ;  117               If (Maks <=CNT)  118   {  119 Maks = CNT;  120 Ed = Arr [I];  121   }  122   }  123 Printf ( "  Case % d: % d to % d \ n  " , Sb ++, Ed-Maks, Ed );  124   }  125       Return   0  ;  126 }

 

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.