给出一个示例输入:

[
    {"id":1,"currentBlack":1,"currentWhite":0,"max":1},
    {"id":2,"currentBlack":0,"currentWhite":1,"max":1},
]


输出输入的所有可能状态,其中currentBlack和currentWhite的值可以在其初始值到最大值之间的任何范围内。

此示例的正确输出:

[
    [
        {"id":1,"currentBlack":1,"currentWhite":0,"max":1},
        {"id":2,"currentBlack":0,"currentWhite":1,"max":1},
    ],
    [
        {"id":1,"currentBlack":1,"currentWhite":1,"max":1},
        {"id":2,"currentBlack":0,"currentWhite":1,"max":1},
    ],
    [
        {"id":1,"currentBlack":1,"currentWhite":1,"max":1},
        {"id":2,"currentBlack":1,"currentWhite":1,"max":1},
    ],
    [
        {"id":1,"currentBlack":1,"currentWhite":0,"max":1},
        {"id":2,"currentBlack":1,"currentWhite":1,"max":1},
    ]
]


实际输入的最大值将介于1到8之间,并且输入数组中将有更多的对象。我的尝试如下(被重评论):

function allPossibleCounts(pieceCounts) {//pieceCounts is the input
        var collection = []; //used to collect all possible values
        recursiveCalls(pieceCounts); //runs recursive function
        return collection; //returns result

        function recursiveCalls(pieceCounts) {
            //if pieceCounts is already in collection then return, not yet implemented so duplicates are currently possible
            collection.push(pieceCounts);//inputs a potential value

            console.log(JSON.stringify(pieceCounts));//this is successfully logs the correct values
            console.log(JSON.stringify(collection));//collection isn't correct, all values at the top of the array are copies of each other

            for (let n in pieceCounts) {//pieceCounts should be the same at the start of each loop within each scope, aka pieceCounts should be the same at the end of this loop as it is at the start

                subBlackCall(pieceCounts);
                function subBlackCall(pieceCounts) {
                    if (pieceCounts[n].currentBlack < pieceCounts[n].max) {
                        pieceCounts[n].currentBlack++;//increment
                        recursiveCalls(pieceCounts);
                        subBlackCall(pieceCounts);//essentially you're either adding +1 or +2 or +3 ect all the way up to max and calling recursiveCalls() off of each of those incremented values
                        pieceCounts[n].currentBlack--;//decrement to return pieceCounts to how it was at the start of this function
                    }
                }

                subWhiteCall(pieceCounts);
                function subWhiteCall(pieceCounts) {
                    if (pieceCounts[n].currentWhite < pieceCounts[n].max) {
                        pieceCounts[n].currentWhite++;
                        recursiveCalls(pieceCounts);
                        subWhiteCall(pieceCounts);
                        pieceCounts[n].currentWhite--;
                    }
                }
            }
        }
    }


但是目前我的尝试输出为复制数组的这种混乱的混乱

[[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}],[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}],[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}],[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}],[{"id":1,"currentBlack":1,"currentWhite":1,"max":1},{"id":2,"currentBlack":1,"currentWhite":1,"max":1}]]


编辑:工作代码:https://pastebin.com/qqFTppsY

最佳答案

pieceCounts[n]始终引用一个对象。您应该重新创建pieceCount,以将其另存为集合。例如,您可以添加

pieceCounts = JSON.parse(JSON.stringify(pieceCounts)); // just clone


recursiveCalls函数的开头。

关于javascript - 嵌套递归,查找所有可能的计件数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46222191/

10-11 15:56