搜索一·24点---dfs

时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述

周末,小Hi和小Ho都在家待着。

在收拾完房间时,小Ho偶然发现了一副扑克,于是两人考虑用这副扑克来打发时间。

小Ho:玩点什么好呢?

小Hi:两个人啊,不如来玩24点怎么样,不靠运气就靠实力的游戏。

小Ho:好啊,好啊。

<经过若干局游戏之后>

小Ho:小Hi,你说如果要写个程序来玩24点会不会很复杂啊?

小Hi:让我想想。

<过了几分钟>

小Hi:我知道了!其实很简单嘛。

提示:24点

输入

第1行:1个正整数, t,表示数据组数,2≤t≤100。

第2..t+1行:4个正整数, a,b,c,d,1≤a,b,c,d≤10。

输出

第1..t行:每行一个字符串,第i行表示第i组能否计算出24点。若能够输出”Yes”,否则输出”No”。

样例输入
2
5 5 5 1
9 9 9 9
样例输出
Yes
No

#include "iostream"
#include "algorithm"
#include "vector"
#include "fstream"
#include "string.h"
using namespace std;

int T;
double nowNum[4];
int ops[3];
double number[4];
int used[4];
//              +  -  *  /  反- 反/
int opType[] = {0, 1, 2, 3, 4, 5};
bool Oper(int depth);
double calType1();
double calType2();

bool Num(int depth)
{
    if(depth >= 4)
    {
        return Oper(0);
    }
    for(int i=0; i<4; i++)
    {
        if(!used[i])
        {
            nowNum[depth] =  number[i];
            used[i] = 1;
            if(Num(depth+1))
                return true;
            used[i] = 0;
        }
    }
    return false;
}

bool Oper(int depth)
{
    if(depth >= 3)
    {
        if (abs(calType1()-24) < 1e-10)
            return true;
        if (abs(calType2()-24) < 1e-10)
            return true;
        return false;
    }
    for(int i=0; i<6; i++)
    {
        ops[depth] = opType[i];
        if(Oper(depth+1))
            return true;
    }
    return false;
}

//(c @ d) @ e @ f
double calType1()
{
    double result = nowNum[0];
    for(int i=1; i<4; i++)
    {
        if(ops[i-1] == 0)
            result += nowNum[i];
        else if(ops[i-1] == 1)
            result -= nowNum[i];
        else if(ops[i-1] == 2)
            result *= nowNum[i];
        else if(ops[i-1] == 3)
            result /= nowNum[i];
        else if(ops[i-1] == 4)
            result = nowNum[i] - result;
        else
            if(result != 0)
                result = nowNum[i] / result;
            else
                return 0;
    }
    return result;
}

//(c @ d) @ (e @ f)
double calType2()
{
    double a[2];
    int k = 0;
    double result;
    for(int i=0; i<3; i+=2)
    {
        if(ops[i] == 0)
            a[k++] = nowNum[i] + nowNum[i+1];
        else if(ops[i] == 1)
            a[k++] = nowNum[i] - nowNum[i+1];
        else if(ops[i] == 2)
            a[k++] = nowNum[i] * nowNum[i+1];
        else if(ops[i] == 3)
            a[k++] = nowNum[i] / nowNum[i+1];
        else if(ops[i] == 4)
            a[k++] = nowNum[i+1] - nowNum[i];
        else 
            a[k++] = nowNum[i+1] / nowNum[i];
    }
    if(ops[1] == 0)
        result =  a[0] + a[1];
    else if(ops[1] == 1)    
        result = a[0] - a[1];
    else if(ops[1] == 2)
        result = a[0] * a[1];
    else if(ops[1] == 3)
        if(a[1] != 0)
            result = a[0] / a[1];
        else
            return 0;
    else if(ops[1] == 4)
        result = a[1] - a[0];
    else 
        if(a[0] != 0)
            result = a[1] / a[0];
        else
            return 0;
    return result;
}

int main()
{
    cin >> T;
    while(T--)
    {
        for(int i=0; i<4; i++)
            cin >> number[i];
        memset(used, 0, sizeof(used));
        if(Num(0))
            cout << "Yes";
        else
            cout << "No";
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值