[JS]24点游戏(两种解法)

最近遇到的比较有意思的题,是在24点游戏基础上有挑战级别的,先实现了个最笨的穷举法实现,24点游戏的话,常规解法就是通过降维去降低复杂度,通过读编程之美中的解法,以及结合网上的一些其他语言实现,实现了一版JS的。
题目:

A game I played when I was young: Draw 4 cards from playing cards, use + - * / and () to make the final results equal to 24.
You will coding in function equalTo24. Function accept 4 parameters a b c d(4 numbers), value range is 1-100.
The result is a string such as "2223" ,(4+2)(5-1); If it is not possible to calculate the 24, please return "It's not possible!"
All four cards are to be used, only use three or two cards are incorrect; Use a card twice or more is incorrect too.
You just need to return one correct solution, don't need to find out all the possibilities.
The diffrent between "challenge version" and "simple version":

image.png
function  equalTo24(a,b,c,d){
    var ys = ['+','-','*','/'];
    for(var i=0;i<4;i++){
        var arrAll = [a,b,c,d];
        var outArr = [];
        outArr[0] = arrAll[i];
        arrAll.splice(i,1);
        for(let k=0;k<4;k++){       
            for(let j=0;j<3;j++){
                var outArr1 = [...outArr];
                outArr1.push(ys[k]);
                var arrAll1 = [...arrAll];
                outArr1.push(arrAll1[j]);
                arrAll1.splice(j,1);                
                for(let k=0;k<4;k++){                   
                    for(let x=0;x<2;x++){
                        var outArr2 = [...outArr1];
                        outArr2.push(ys[k]);            
                        var arrAll2 = [...arrAll1];
                        outArr2.push(arrAll2[x]);
                        arrAll2.splice(x,1);                        
                        for(let k=0;k<4;k++){                   
                            var outArr3 = [...outArr2];
                            outArr3.push(ys[k]);
                            outArr3.push(arrAll2[0]);
                            var outEx = insertBrackets(outArr3)
                            if(outEx) return outEx
                        }
                    }
                }
            }
        }
    }
    return `It's not possible!`
}
Array.prototype.insert = function (index, item) {
      this.splice(index, 0, item);
      return this
}
function insertBrackets(arr){
    let expressionObj={
        ex1:[...arr].insert(3,')').insert(0,'('),
        ex2:[...arr].insert(5,')').insert(0,'('),
        ex3:[...arr].insert(5,')').insert(2,'('),
        ex4:[...arr].insert(7,')').insert(2,'('),
        ex5:[...arr].insert(7,')').insert(4,'('),
        ex6:[...arr].insert(7,')').insert(4,'(').insert(3,')').insert(0,'('),
        ex7:[...arr].insert(5,')').insert(3,')').insert(0,'(').insert(0,'('),
        ex8:[...arr].insert(7,')').insert(5,')').insert(2,'(').insert(2,'('),
        ex9:[...arr].insert(5,')').insert(5,')').insert(2,'(').insert(0,'('),
        ex10:[...arr].insert(7,')').insert(7,')').insert(4,'(').insert(2,'(')   
    }
    for(let value of Object.values(expressionObj)){     
        var outStr = value.join().replace(/,/g,'');
        if(eval(outStr)==24) return outStr
    }
    
}
var a =  equalTo24(2,4,2,2);
a 
"(2+4)*(2+2)"
解法2
function node(value, exp) {
      this.value = value;
      this.exp = exp;
    }

    function equalTo24(arr) {
      let answers = new Set();
      let S = new Map();
      let A = arr;

      function run() {
        for (var i = 0; i < (1 << 4); i++) {
          let set = new Set();
          S.set(i, set);
        }
        for (var i = 0; i < 4; i++) {
          let n = new node(A[i], A[i] + "");
          let set = new Set();
          set.add(n);
          S.set(1 << i, set);
        }
        for (var i = 1; i < (1 << 4); i++) {
          fork(i);
        }

        let mSet = S.get((1 << 4) - 1);
        for (let v of mSet.values()) {
          if (v.value == 24) {
            answers.add(v.exp);
          }
        }
        if (answers.size == 0) {
          console.log("It's no possible");
        }
        else {
          for (let i of answers) {
            console.log(i)
          }
        }
      }

      function fork(m) {
        var mSet = S.get(m);
        if (mSet.size > 0)
          return mSet;
        else {
          for (var x = 1; x < m; x++) {
            if ((x & m) == x) {
              let s1 = fork(x);
              let s2 = fork(m - x);
              for (let n1 of s1)
                for (let n2 of s2) {
                  var str = "(" + n1.exp + "+" + n2.exp + ")";
                  mSet.add(new node(n1.value + n2.value, str));
                  str = "(" + n1.exp + "-" + n2.exp + ")";
                  mSet.add(new node(n1.value - n2.value, str));
                  str = "(" + n2.exp + "-" + n1.exp + ")";
                  mSet.add(new node(n1.value - n2.value, str));
                  str = "(" + n1.exp + "*" + n2.exp + ")";
                  mSet.add(new node(n1.value * n2.value, str));
                  if (n1.value != 0) {
                    str = "(" + n2.exp + "/" + n1.exp + ")";
                    mSet.add(new node(n2.value / n1.value, str));
                  }
                  if (n2.value != 0) {
                    str = "(" + n1.exp + "/" + n2.exp + ")";
                    mSet.add(new node(n1.value / n2.value, str));
                  }
                }
            }
          }
          return mSet;
        }

      }
      run()
    }
var arr = [12,40,5,6];
equalTo24(arr)

你可能感兴趣的:([JS]24点游戏(两种解法))