Nyoj 43 24 Point game 【DFS】
24 Point game
- 描写叙述
-
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】的更多相关文章
- nyoj 43 24 Point game(dfs暴力)
描述 There Point game. In .The expression mustn't have any other operator except plus,minus,multiply,d ...
- 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】
目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...
- Kattis - glitchbot 【DFS】
Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...
- HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))
度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【dfs】P1331 海战
题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...
- 【dfs】p1731 生日蛋糕
1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...
- 【dfs】LETTERS
1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...
- 洛谷P1605 迷宫【dfs】
题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...
- 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名
[题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...
随机推荐
- html:标签原本属性
<!doctype html>无标题文档 a标签,默认有text-decoration属性 span标签不需要清零 b标签不需要清零 em标签不需要清零 strong 相邻内嵌元素代码里面 ...
- hdu 5108
//题意是给一个数N,然后让你求M,使得N/M为素数,并且M的值最小 //思路呢,大概有两种,一个是遍历素数求解的,不过数据太大不现实 //另外一种就是质因数求解,for循环是遍历质因数,然后whil ...
- (转)解析php中die(),exit(),return的区别
本篇文章是对php中die(),exit(),return的区别进行了详细的分析介绍,需要的朋友参考下 die()停止程序运行,输出内容exit是停止程序运行,不输出内容return是返回值d ...
- url的非法字符有哪些
需要过滤的特殊字符及字符串有: net user xp_cmdshell /add exec master.dbo.xp_cmdshell net localgroup administrators ...
- linux jdk,java ee ,tomcat 安装配置
1.把mypagekage.iso 挂载到linux操作系统中. 在VM做好配置,使用 mount /mnt/cdrom 2.把安装文件拷贝到/home cp 文件名 /home (快捷键tab) 3 ...
- Delphi之TreeView
TreeView是Delphi中使用频率比较高的一个控件,虽然使用次数很多,但总结不够.借着这次做GDW原型的机会总结一下,写的过程中也会参考网上的博文. TTreeView.TTreeNodes和T ...
- php explode 用法详解
定义和用法explode() 函数把字符串分割为数组. 语法explode(separator,string,limit)参数 描述 separator 必需.规定在哪里分割字符串.string 必需 ...
- java eclipse 连接数据库全过程
1.需要下载一个jar包.地址 http://pan.baidu.com/s/1i50LRId 2.代码如下: import java.sql.*; public class Mytest { pub ...
- 1160 蛇形矩阵(codevs)
http://codevs.cn/problem/1160/ 题目描述 Description 小明玩一个数字游戏,取个n行n列数字矩阵(其中n为不超过100的奇数),数字的填补方法为:在矩阵中心从1 ...
- 转:VC中MessageBox的常见用法
一.关于MessageBox 消息框是个很常用的控件,属性比较多,本文列出了它的一些常用方法,及指出了它的一些应用场合. 1.MessageBox("这是一个最简单的 ...