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)对奶牛的产奶率,但他 ...
随机推荐
- php代码结尾不要添加结尾标记
如果文件内容是纯 PHP 代码,最好在文件末尾删除 PHP 结束标记.这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图.
- 华为S5300交换机配置基于接口的本地端口镜像
配置思路 1. 将Ethernet0/0/20接口配置为观察端口(监控端口) 2. 将Ethernet0/0/1----Ethernet0/0/10接口配置为镜像端口 配置步骤 1. 配置观察端 ...
- xcode 删除 Provisioning Profile
provisioning profile path: ~/Library/MobileDevice/Provisioning Profiles 打开并日期排序,删除老的 provisioning pr ...
- 武汉科技大学ACM:1002: 华科版C语言程序设计教程(第二版)例题6.6
Problem Description 明天就要英语考试了,小明明正在挑灯夜 战背单词.小明明发现单词很难背,背一个忘一个.经过仔细研究,小明明发现单词难背的原因是因为某个字符的出现,破坏了整个单词的 ...
- c语言学习,模拟栈操作
1.stack.c模拟栈操作函数的实现 #include<stdio.h> #include<stdlib.h> ; static char *stack;//数据栈 ;//栈 ...
- 在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程。
在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程. 答案:可以通过Intent对象.静态变量.剪切板和全局对象进行数据传递,具体的数据传递方法如下. 1. ...
- 一些 CSS 框架
利用 CSS 框架,可以简化你的工作,提高工作效率.CSS 框架是一系列 CSS 文件的集合体,包含了基本的元素重置,页面排版.网格布局.表单样式.通用规则等代码块.下面给你推荐了27款优秀的CSS框 ...
- java中ExecutorService接口
一.声明 public interface ExecutorService extends Executor 位于java.util.concurrent包下 所有超级接口:Executor 所有已知 ...
- twisted(3)--再谈twisted
上一章,我们直接写了一个小例子来从整体讲述twisted运行的大致过程,今天我们首先深入一些概念,在逐渐明白这些概念以后,我们会修改昨天写的例子. 先看下面一张图: 这个系列的第一篇文章,我们已经为大 ...
- [POJ] 1948 Triangular Pastures (DP)
题目地址:http://poj.org/problem?id=1948 题目大意: 给N条边,把这些边组成一个三角形,问面积最大是多少?必须把所有边都用上. 解题思路: 根据题意周长c已知,求组合三边 ...