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. css动画+照片清晰度动画

    源代码: <!DOCTYPE html><html><head> <title>donghua</title> <meta chars ...

  2. (转)用JS判断ckeditor3.6版本编辑器内容为空的方法

    因为编者我还是一个初学入门者,无论是JS还是PHP,都是懂了点皮毛.今天在研究ckeditor编辑器的时候遇到了一些问题,虽然这只是我的个人问题,但觉得可以分享给其他人,或许也有像我这样的初学者也会遇 ...

  3. linux jdk,java ee ,tomcat 安装配置

    1.把mypagekage.iso 挂载到linux操作系统中. 在VM做好配置,使用 mount /mnt/cdrom 2.把安装文件拷贝到/home cp 文件名 /home (快捷键tab) 3 ...

  4. Nagios设置只监控不报警

    设置全部监控项都开启邮件报警: vim /usr/local/nagios/etc/nagios.cfg 设置 enable_notifications=1    1为开启,0为关闭     如个别监 ...

  5. linux操作笔记

    [fedora可以ping通但是isReachAble返回false的原因] fedora18+ 防火墙设置原因,导致tcp端口不通,返回no route to host错误. 关闭防火墙命令: sy ...

  6. json数据返回

    <script type="text/javascript"> function xmlpage(){ var xhr=new XMLHttpRequest(); xh ...

  7. 研究一下uucode编码

    uucode编码是把任意二进制数据转换为ascii字符的编码用于在一些只能传递文本的地方传送二进制数据uu模块提供了encode()和decode()用于将一个文件转换为uucode编码的字符文件,文 ...

  8. 自定义ORM框架(转转)

    ORM背景 在数据库界,主流的数据库都是关系型数据库,其采用的关系型数据结构模型,无论从数学上还是实践中都相当的成熟,得到非常广泛的应用.在关系型数据结构理 论中,所有的数据都组织成一个个相互独立的二 ...

  9. PHPCMS v9构建模块

    ■补课: 1.phpcms v9帮助文件,上面会写关于二次开发的一些方法. http://v9.help.phpcms.cn/ 2.找一个后台还没安装的模块,先把代码看一边.比如dianping模块 ...

  10. 不要依赖hibernate的二级缓存

    一.hibernate的二级缓存   如果开启了二级缓存,hibernate在执行任何一次查询的之后,都会把得到的结果集放到缓存中,缓存结构可以看作是一个hash table,key是数据库记录的id ...