LintCode 739: 24 Game (DFS经典题)

739. 24 Game

You have 4 cards each containing a number from 1 to 9. You need to compute whether they could operated through */+-() to get the value of 24.

Example

Example 1:

Input:[1, 4, 8, 7]
Output:true
Explanation:8 * (7 - 4) * 1 = 24

Example 2:

Input:[1, 1, 1, 2]
Output:false
Explanation:no way to get 24

Example 3:

Input:[3, 3, 8, 8]
Output:true
Explanation:8 / ( 3 - 8 / 3) = 24

Notice

  • The division operator / represents real division, not integer division. so 4 / (1 - 2/3) = 12.
  • Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
  • You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

Input test data (one parameter per line)How to understand a testcase?

 

解法1:
 

class Solution {
public:
    /**
     * @param nums: 4 cards
     * @return: whether they could get the value of 24
     */
    bool compute24(vector<double> &nums) {
        return dfs(nums, 4);
    }
    
private:
    bool dfs(vector<double> &nums, int n) {
        if (n == 1) {
            if (fabs(nums[0] - 24) < 1e-6) return true;
            else return false;
        }
        
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                double a = nums[i];
                double b = nums[j];
                nums[j] = nums[n - 1];
                
                // a + b
                nums[i] = a + b;
                if (dfs(nums, n - 1)) {
                    return true;
                }
                
                // a - b
                nums[i] = a - b;
                if (dfs(nums, n - 1)) {
                    return true;
                }
                
                // b - a
                nums[i] = b - a;
                if (dfs(nums, n - 1)) {
                    return true;
                }
                
                // a * b
                nums[i] = a * b;
                if (dfs(nums, n - 1)) {
                    return true;
                }
                
                // a / b
                if (fabs(b) > 1e-6) {
                    nums[i] = a / b;
                    if (dfs(nums, n - 1)) {
                        return true;
                    }
                }
                
                // b / a
                if (fabs(a) > 1e-6) {
                    nums[i] = b / a;
                    if (dfs(nums, n - 1)) {
                        return true;
                    }
                }
                nums[i] = a;
                nums[j] = b;
            }
        }
        
        return false;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值