Nyoj Point Game (Dfs violence)

Source: Internet
Author: User

Describe
There isA game which isCalled -Point game. Inch ThisGame, you'll be given some numbers. Your Task isTo find an expression which has all the given numbers and the value of the expression should be -. The expression mustn'T has any other operator except Plus,minus,multiply,divide and the brackets.e.g. If the numbers you is given is "3 3 8 8", you can give"8/(3-8/3)"  asAn answer. All the numbers should is used and the bracktes can be nested. Your Taskinch  ThisProblem isOnly to judge whether the given numbers can is used to find a expression whose value isThe given number. 

Input
The input has multicases and each Casecontains one linethe first line of the input isAn non-negative integer C (c<= -), which indicates the number of the cases. Each line has some integers,the first integer M (0<=m<=5) isThe total number of the given numbers to consist the expression,the second integers N (0<=n<= -) isThe number which the value of the expression should be. Then,the followed M integer isThe given numbers. All the given numbers isNon-negative and less than -

Output
" Yes " if  is " No " instead.

Sample input
2 4  - 3 3 8 8 3  - 8 3 3

Sample output
YesNo

Deep search enumerates various possibilities.

1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 1000000 at #defineINF 1e12 - intN; - Doublev,a[6]; - BOOLDfsintt) { -    if(t==N) { -       if(Fabs (V-a[n]) <1e-7){ in          return true; -       } to       return false; +    } -     for(inti=t;i<n;i++){ the        for(intj=i+1; j<=n;j++){ *          Doubletemp1=A[i]; $          DoubleTemp2=A[j];Panax Notoginsenga[i]=A[t]; -A[J]=TEMP1+TEMP2;if(Dfs (t+1))return true;//Plus theA[J]=TEMP1-TEMP2;if(Dfs (t+1))return true;//minus 1 +A[J]=TEMP2-TEMP1;if(Dfs (t+1))return true;//minus 2 AA[J]=TEMP1*TEMP2;if(Dfs (t+1))return true;//Multiply the          if(TEMP1) {//except 1 +A[J]=TEMP2/TEMP1;if(Dfs (t+1))return true; -          } $          if(TEMP2) {//except 2 $A[J]=TEMP1/TEMP2;if(Dfs (t+1))return true; -          } -A[I]=TEMP1;//Restore theA[J]=TEMP2;//Restore -       }Wuyi    } the    return false; - } Wu intMain () - { About    intT; $scanf"%d",&t); -     while(t--){ -scanf"%D%LF",&n,&v); -        for(intI=1; i<=n;i++){ Ascanf"%LF",&a[i]); +       } the       if(Dfs (1)){ -printf"yes\n"); $}Else{ theprintf"no\n"); the       } the    } the     return 0; -}
View Code

Nyoj Point Game (Dfs violence)

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.