zoukankan      html  css  js  c++  java
  • Nyoj 43 24 Point game 【DFS】

    24 Point game

    时间限制:3000 ms  |  内存限制:65535 KB
    难度:5
    描写叙述

    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

    分析:能够进行的运算为加。 减, 被减, 乘, 除。 被除六种运算。我们仅仅须要每次都取出没实用的两个数(能够是运算之后的值),进行运算,枚举就好了。

    代码:

     
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    const double E = 1e-6;
    using namespace std;
    
    double s[50], res;  
    bool vis[50];
    
    int dfs(int m, int top){  //每次取出两个数然后将结果放入数组中,就等于每次减掉一个没有使用的数
    	if(m == 1){
    		if(fabs(res - s[top-1]) < E){   
    			//printf("%lf...res %lf..sll
    ", s[top-1], res);
    			return 1;
    			
    		}
    		return 0;
    	}
    	for(int i = 0; i < top-1; ++ i){
    		if(!vis[i]){
    			vis[i] = 1;
    			for(int j = i+1; j < top; ++ j){
    				if(!vis[j]){
    					vis[j] = 1;
    					
    					s[top] = s[i]+s[j];
    					if(dfs(m-1, top+1)) return 1;
    					
    					s[top] = s[i]-s[j];
    					if(dfs(m-1, top+1)) return 1;
    					
    					s[top] = s[j]-s[i];
    					if(dfs(m-1, top+1)) return 1;
    					
    					s[top] = s[i]*s[j];
    					if(dfs(m-1, top+1)) return 1;
    					
    					if(s[i] != 0){
    						s[top] = s[j]/s[i];
    						if(dfs(m-1, top+1)) return 1;
    					}
    					
    					if(s[j] != 0){
    						s[top] = s[i]/s[j];
    						if(dfs(m-1, top+1)) return 1;
    					}
    					
    					vis[j] = 0;
    				}
    			}
    			vis[i] = 0;
    		}
    	}
    	return 0;
    }
    
    int main(){
    	int t;
    	scanf("%d", &t);
    	while(t --){
    		int n;
    		scanf("%d", &n);
    		scanf("%lf", &res);
    		for(int i = 0; i < n; ++ i) scanf("%lf", &s[i]);
    		memset(vis, 0, sizeof(vis));
    		if(dfs(n, n)) printf("Yes
    ");
    		else printf("No
    ");
    	}
    	return 0;
    }         


  • 相关阅读:
    手机模式input框输入框收起键盘失焦后强制让页面归位
    造一个智能语音音箱!!!太简单了【语音智能管家】
    教你搭建一个花卉识别系统(超级简单)
    【语音智能管家】之语音唤醒(附演示视频)
    conda 安装GPU——CUDA
    筹划了几年,我终于开始实现了---语音智能管家
    我爬取了爬虫岗位薪资,分析后发现爬虫真香
    最近爆火的帅小伙丁真在AI面前颜值多少分?
    生物信息学云论坛第十三场报告会
    生物信息学云论坛第十二场报告会
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5263025.html
Copyright © 2011-2022 走看看