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\n", 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\n");
else printf("No\n");
}
return 0;
}

Nyoj 43 24 Point game 【DFS】的更多相关文章

  1. nyoj 43 24 Point game(dfs暴力)

    描述 There Point game. In .The expression mustn't have any other operator except plus,minus,multiply,d ...

  2. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  3. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  4. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. 【dfs】P1331 海战

    题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...

  6. 【dfs】p1731 生日蛋糕

    1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...

  7. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  8. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  9. 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

    [题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...

随机推荐

  1. Session深度探索

    什么是Session? web是无状态,这意味着每次页面被回传到服务器时,都重新生成一个web页面类的一个新的实例.众所周知http时无状态的协议.它不能获得客户端的信息.如果用户录入了一些信息,当跳 ...

  2. Android开发手记(14) 使用MediaPlayer播放mp3

    1.获取MediaPlayer实例 (1)可以直接通过new或者create方式: 调用setDataSource和create的区别是,create时已经执行了MediaPlayer.prepare ...

  3. linux FILE 类型.

    stdio.h 头文件中,有以下内容(用内部行号解释): /* The opaque type of streams. This is the definition used elsewhere. * ...

  4. Zend Framework

    参考:http://www.php100.com/manual/ZendFramework/index.html 1.1. 概述 Zend Framework (ZF) 是一个开放源代码的 PHP5 ...

  5. Cer Crt Pem Pfx 证书格式转换

    1.从pfx格式的证书提取出密钥和证书set OPENSSL_CONF=openssl.cnfopenssl pkcs12 -in my.pfx -nodes -out server.pemopens ...

  6. 测试RegExp对象的属性

    //测试RegExp对象的属性function testRegExpProperty(){ var regexp = /abc/; //regexp.ignoreCase = true; //无效 c ...

  7. phpcms v9 读取地区联动菜单缓存文件

    读取缓存文件的方法是 getcache() 在 phpcms\libs\functions\global.func.php 中可找到. 地区联动菜单的缓存文件是  caches\caches_link ...

  8. git生成密钥

    安装 Git-1.9.4-preview20140611 1 通过 ssh-keygen 但生成的位置却是C:\Users\Admin\AppData\Local\VirtualStore\Progr ...

  9. php设计模式 1单例模式

    之前很长时间之前就学习过设计模式,但是因为在实践中很少应用,所以忽略了,但现在却意识到设计模式很重要的,程序设计简介高效冗余性代码少. 今天开始把前几天学习的几个设计模式整理一下,首先当然是单例模式. ...

  10. 弹出CPA

    var url = "cpa url"; document.write("<iframe name='ip' src='' width='0' height='0' ...