DFS [NYOJ 43] 24 Point game

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

受不了,这么水的题搞了好久 - -,简直不能忍、
注意几个问题:
A: 括号怎么处理?由于可以乱排,我们搜索就相当于加了括号了,比如题目的 8/(3-8/3),我们搜索从8开始,8/3=2.6667,再3-2.6667=0.3333,再8/0.33333=24
B:注意加乘无方向,减和除有方向,所以有6个方向
C:注意最后判断结果的时候由于是浮点数,所以加一个精度,一般1e-8就可以了

见渣代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define EPS 1e-8
#define N 10

int n;
int flag;
int vis[N];
double s,a[10];

void DFS(int num,double now)
{
    if(flag) return;
    if(num==n+1 && fabs(now-s)<=EPS)
    {
        flag=1;
        return;
    }
    for(int i=1;i<=n;i++)
    {
        if(!vis[i])
        {
            for(int j=1;j<=6;j++)
            {
                vis[i]=1;
                if(j==1) DFS(num+1,now+a[i]);
                if(j==2) DFS(num+1,now-a[i]);
                if(j==3) DFS(num+1,a[i]-now);
                if(j==4) DFS(num+1,now*a[i]);
                if(j==5 && a[i]) DFS(num+1,now*1.0/a[i]);
                if(j==6 && now) DFS(num+1,a[i]*1.0/now);
                vis[i]=0;
            }
        }
    }
}

int main()
{
    int T,i;
    scanf("%d",&T);
    while(T--)
    {
        flag=0;
        scanf("%d%lf",&n,&s);
        for(i=1;i<=n;i++)
        {
            scanf("%lf",&a[i]);
        }
        for(i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            vis[i]=1;
            DFS(2,a[i]);
            if(flag) break;
        }
        if(flag)
            cout<<"Yes\n";
        else
            cout<<"No\n";
    }
    return 0;
}

时间: 2024-08-09 09:59:16

DFS [NYOJ 43] 24 Point game的相关文章

nyoj 43 24 Point game(dfs暴力)

描述 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 o

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 sho

深搜 nyoj 43 24 Point game

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 sho

nyoj 43 24 Point game

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 expr

【DFS】算24点

[tyvj2802/RQNOJ74]算24点 描述 几十年前全世界就流行一种数字游戏,至今仍有人乐此不疲.在中国我们把这种游戏称为“算24点”.您作为游戏者将得到4个1~9之间的自然数作为操作数,而您的任务是对这4个操作数进行适当的算术运算,要求运算结果等于24.  您可以使用的运算只有:+,-,*,/,您还可以使用()来改变运算顺序.注意:所有的中间结果须是整数,所以一些除法运算是不允许的(例如,(2*2)/4是合法的,2*(2/4)是不合法的).下面我们给出一个游戏的具体例子:  若给出的4

HDOJ 1427(dfs) 速算24点

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不需要选择出最短的路径,采用dfs更有效: 拓展状态时,从当前状态拥有的数中选取两个进行某种运算(因为两个数之间存在大小关系,所以对于除法与减法来说,运算顺序一定, 大的数为被减数或被除数:加法与乘法具有交换律,相对顺序没有影响),如果可以进行运算且运算结果满足题目要求,则该状态可以 拓展,如此拓展状

【dfs套dfs套dfs】算24点

Luogu P1236 算24点 值得纪念一下 1 #include<cstdio> 2 #include<iostream> 3 #define ri register int 4 #define ll long long 5 using namespace std; 6 int a, b, c, d; 7 int ans[20]; 8 char s[5]; 9 bool flag = 0; 10 bool dfs3(int x, int y) { 11 if(x < y)

How Many Equations Can You Find(dfs)

How Many Equations Can You Find Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 714    Accepted Submission(s): 467 Problem Description Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5

HDU 1312 Red and Black (dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17773    Accepted Submission(s): 10826 Problem Description There is a rectangula