• nyoj 43 24 Point game(dfs暴力)


     

    描述
    There is a game which is called 24 Point game.
    
    In this game , you will be given some numbers. Your task is to find an expression which have all the given numbers and the value of the expression should be 24 .The expression mustn't have any other operator except plus,minus,multiply,divide and the brackets. 
    
    e.g. If the numbers you are given is "3 3 8 8", you can give "8/(3-8/3)" as an answer. All the numbers should be used and the bracktes can be nested. 
    
    Your task in this problem is only to judge whether the given numbers can be used to find a expression whose value is the given number。
    
     
    输入
    The input has multicases and each case contains one line
    The first line of the input is an non-negative integer C(C<=100),which indicates the number of the cases.
    Each line has some integers,the first integer M(0<=M<=5) is the total number of the given numbers to consist the expression,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 is non-negative and less than 100
    输出
    For each test-cases,output "Yes" if there is an expression which fit all the demands,otherwise output "No" instead.
    样例输入
    2
    4 24 3 3 8 8
    3 24 8 3 3
    样例输出
    Yes
    No

    深搜枚举各种可能。

     1 #pragma comment(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>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 1000000
    23 #define inf 1e12
    24 int n;
    25 double v,a[6];
    26 bool dfs(int t){
    27    if(t==n){
    28       if(fabs(v-a[n])<1e-7){
    29          return true;
    30       }
    31       return false;
    32    }
    33    for(int i=t;i<n;i++){
    34       for(int j=i+1;j<=n;j++){
    35          double temp1=a[i];
    36          double temp2=a[j];
    37          a[i]=a[t];
    38          a[j]=temp1+temp2; if(dfs(t+1)) return true;//
    39          a[j]=temp1-temp2; if(dfs(t+1)) return true;//减1
    40          a[j]=temp2-temp1; if(dfs(t+1)) return true;//减2
    41          a[j]=temp1*temp2; if(dfs(t+1)) return true;//
    42          if(temp1){//除1
    43             a[j]=temp2/temp1; if(dfs(t+1)) return true;
    44          }
    45          if(temp2){//除2
    46             a[j]=temp1/temp2; if(dfs(t+1)) return true;
    47          }
    48          a[i]=temp1;//还原
    49          a[j]=temp2;//还原
    50       }
    51    }
    52    return false;
    53 }
    54 int main()
    55 {
    56    int t;
    57    scanf("%d",&t);
    58    while(t--){
    59       scanf("%d%lf",&n,&v);
    60       for(int i=1;i<=n;i++){
    61          scanf("%lf",&a[i]);
    62       }
    63       if(dfs(1)){
    64          printf("Yes
    ");
    65       }else{
    66          printf("No
    ");
    67       }
    68    }
    69     return 0;
    70 }
    View Code
  • 相关阅读:
    React 父调子 子调父 observer.js 非父子传值
    echarts 折线图
    跳转新页面解决用户重新登录的方法
    微信小程序规范 引入框架 引入外部js css
    js 400
    Angular2 表单校验
    Angular2 登录页进门户页 用户接口正常 从服务平台退出到门户页 登录接口报302
    CSP-S 2020 SC 迷惑行为大赏
    【题解】[CSP-S 2020] 函数调用【拓扑图DP】
    【题解】[CSP-S 2020] 动物园
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4984521.html
Copyright © 2020-2023  润新知