NYOJ-43 Point Game-dfs

Source: Internet
Author: User

Point game time limit: theMs | Memory Limit:65535KB Difficulty:5
Describe

There is a game which are called point game.

In this game, you'll be given some numbers. Your task is to find a expression which the given numbers and the value of the expression should be 24. The expression mustn ' t has any other operator except Plus,minus,multiply,divide and the brackets.

e.g. If the numbers given is "3 3 8 8", you can Give "8/(3-8/3)" as an answer. All the numbers should is used and the bracktes can be nested.

Your task in this problem are only to judge whether the given numbers can being used to find a expression whose value is the G Iven number.

Input
The input has multicases and each case contains one line
The first line of the input was an non-negative integer C (c<=100), which indicates the number of the cases.
Each line have some integers,the first integer M (0<=m<=5) is the total number of the given numbers to consist the EXP ression,the second integers N (0<=n<=100) is the number which the value of the expression should be.
Then,the followed M integer is the given numbers. All the given numbers are non-negative and less than 100
Output
For each
test-cases,output ' Yes ' if there is a expression which fit all the demands,otherwise output "No" instead.
Sample input
24 24 3 3 8 83 24 8 3 3
Sample output
YesNo
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <queue> #include <stack>//#define MIN 1e-10using namespace std; Const double Min=1e-10;//const double, habitually written as const int leads to a night double a[10]; int n;double num;bool DFS (int x) {if (x==n) {if (Fabs (Num-a[n]) <min) return True;return false; Double now,next;for (int i=x;i<n;i++) {for (int j=i+1;j<=n;j++) {now=a[i];next=a[j];a[i]=a[x];a[j]=now+next;if ( DFS (x+1)) return True;a[j]=now-next;if (Dfs (x+1)) return True;a[j]=next-now;if (Dfs (x+1)) return true;a[j]=now*next;if (Dfs (x+1)) return true;if (Next)//denominator not 0 {a[j]=now/next;if (DFS (x+1)) return true;} if (now)//The denominator is not 0{a[j]=next/now;if (DFS (x+1)) return true; A[i]=now;a[j]=next;}} return false;} int main () {int t;while (~scanf ("%d", &t)) {while (t--) {memset (a,0,sizeof (a)); scanf ("%d%lf", &n,&num); for ( int i=1;i<=n;i++) {scanf ("%lf", &a[i]);} if (DFS (1)) cout<< "Yes" <<endl; else cout<< "No" <<endl;}} return 0;}


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