24-point Game (DFS)

Source: Internet
Author: User
Tags integer division

24-point gameTime limit:3000/1000ms (java/others) Memory limit:65535/65535kb (java/others)Submit Status

">< Span id= "mathjax-span-2" class= "Mrow" >24 24 points is to give you a bunch of numbers and ask if you are subtraction bracket ">< Span id= "mathjax-span-4" class= "math" > 24 24 points.

Shen Yu think this is very fun, decided to test you, give you 44 number, can exchange position, can use subtraction and brackets, whether can constitute 24 points?

Note that the division here is not an integer division, such as a sample

Input

First line tT, indicating how many sets of test data,1≤T≤1≤t≤50

NextTT line, per line44 positive integersa1 A1,a2 A2, a3 A3,a4 A4, which indicates what each number is,1≤ai≤1≤ai≤13

Output

For each inquiry, if it can be 24 points, output yes , otherwise the output no

Sample Input and output
Sample Input Sample Output
23 3 8 81 1 1 1
Yesno
Hint

3333 88) 88

We can construct a 8 & #x00F7; ( 3 & #x2013; 8 & #x00F7; 3 ) = ">< Span id= "mathjax-span-74" class= "Mrow" >8 ÷ ( 3–8 ÷3 ) =24

Solution: 24 points, violent search under the good, at first I think the whole arrangement, and then in order to proceed, but the idea is obviously imperfect, and then saw the great God,

The great God is the solution to extend the current operation of the array storage, the VIS record is used, the idea is very ingenious;

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<map>#include<string>#include<vector>using namespacestd;Const intinf=0x3f3f3f3f;#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)#defineP_ printf ("")#defineMem (x, y) memset (x,y,sizeof (x))typedefLong LongLL;intFlot;Doublea[ the];intvis[ the];/*void dfs (int pos,double cur), void ary (int num) {if (flot) return;        if (num==4) {//for (int i=0;i<4;i++) printf ("%d", Ans[i]);p UTS ("");        DFS (1,ans[0]);    Return        } for (int i=0;i<4;i++) {if (vis[i]) continue;        Ans[num]=a[i];        Vis[i]=1;        ary (num+1);    vis[i]=0; }}*/voidDfsintUsdintTP) {    if(Flot)return; if(usd==4){        if(ABS (a[tp-1]- -) <1e-8) flot=1; return; }    /*DFS (Pos+1,cur+ans[pos]);    DFS (Pos+1,cur-ans[pos]);    DFS (Pos+1,cur*ans[pos]);    DFS (Pos+1,cur/ans[pos]);    DFS (Pos+1,-cur*ans[pos]);    DFS (Pos+1,-cur/ans[pos]);    DFS (pos+1,ans[pos]/cur);    DFS (pos+1,ans[pos]/cur); */     for(intI=0; i<tp;i++){        if(Vis[i])Continue; Vis[i]=1;  for(intj=i+1; j<tp;j++){            if(Vis[j])Continue; VIS[J]=1; A[TP]=A[I]+A[J];d FS (usd+1, tp+1); A[TP]=A[I]*A[J];d FS (usd+1, tp+1); A[TP]=A[I]-A[J];d FS (usd+1, tp+1); A[TP]=a[j]-a[i];d FS (usd+1, tp+1); if(a[i]!=0) {A[TP]=a[j]/a[i];d FS (usd+1, tp+1); }            if(a[j]!=0) {A[TP]=A[I]/A[J];d FS (usd+1, tp+1); } Vis[j]=0; } Vis[i]=0; }}intMain () {intT;    SI (T);  while(t--){         for(intI=0;i<4; i++) scanf ("%LF",&A[i]); Flot=0; Mem (Vis,0); DFS (1,4); if(Flot) puts ("Yes"); ElsePuts"No"); }    return 0;}

24-point Game (DFS)

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.