鱼C论坛

 找回密码
 立即注册
查看: 1359|回复: 1

[技术交流] C++刷leetcode(679. 24 点游戏)【深度优先搜索】

[复制链接]
发表于 2020-4-13 22:52:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-5-8 17:51 编辑

题目描述:
  1. 你有 4 张写有 1 到 9 数字的牌。你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24。

  2. 示例 1:

  3. 输入: [4, 1, 8, 7]
  4. 输出: True
  5. 解释: (8-4) * (7-1) = 24
  6. 示例 2:

  7. 输入: [1, 2, 1, 2]
  8. 输出: False
  9. 注意:

  10. 除法运算符 / 表示实数除法,而不是整数除法。例如 4 / (1 - 2/3) = 12 。
  11. 每个运算符对两个数进行运算。特别是我们不能用 - 作为一元运算符。例如,[1, 1, 1, 1] 作为输入时,表达式 -1 - 1 - 1 - 1 是不允许的。
  12. 你不能将数字连接在一起。例如,输入为 [1, 2, 1, 2] 时,不能写成 12 + 12 。

  13. 来源:力扣(LeetCode)
  14. 链接:https://leetcode-cn.com/problems/24-game
  15. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码


  1. #include<iostream>
  2. #include<vector>
  3. #include<cmath>

  4. using namespace std;

  5. void dfs(vector<double>&input, bool& res){
  6.         if (input.size() == 1) {
  7.             if (abs(input[0] - 24) < 0.0001) res = true;
  8.             return;
  9.         }
  10.         for(int i = 0; i < input.size(); i++){
  11.                 for(int j = 0; j < i; j++){
  12.                         double temp1 = input[i], temp2 = input[j];
  13.                        
  14.                         vector<double> temp {temp1 + temp2, temp1 - temp2, temp2 - temp1, temp1 * temp2};
  15.                         if(temp2 > 0.0001) temp.push_back(temp1 / temp2);
  16.                         if(temp1 > 0.0001) temp.push_back(temp2/temp1);
  17.                         input.erase(input.begin() + i);
  18.                         input.erase(input.begin() + j);
  19.                         for(int i = 0; i < temp.size(); i++){
  20.                                 input.push_back(temp[i]);
  21.                                 dfs(input, res);
  22.                                 input.pop_back();
  23.                         }
  24.                         input.insert(input.begin() + j, temp2);
  25.                         input.insert(input.begin() + i, temp1);
  26.                        
  27.                        
  28.                 }
  29.         }
  30. }


  31. bool solution(vector<int>&num){
  32.         bool res = false;
  33.         vector<double> input (num.begin(), num.end());
  34.         dfs(input, res);
  35.         return res;
  36. }


  37. int main(void){
  38.         vector<int> input;
  39.         int number;
  40.         while(cin >> number){
  41.                 input.push_back(number);
  42.         }
  43.         cout << solution(input) << endl;
  44.         return 0;
  45. }
复制代码



注意事项:
1.
  1. input.insert(input.begin() + j, temp2);
  2.                         input.insert(input.begin() + i, temp1);
复制代码

上面的顺序不能反着,要正好和前面两句镜像对称
2. p-q q-p 包含了j+1到input.size()的情况所以j取0到i即可。
3.bool后面一定要带&,因为res的值在运行dfs的时候可能会改变。
4.代码参考链接:https://leetcode-cn.com/problems ... su-fa-by-guohaodin/

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 17:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表