【发布时间】:2017-09-14 14:49:57
【问题描述】:
给定一个示例输入:
[
{"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以作为不同的对象保存到集合中。例如,您可以在recursiveCalls函数的开头添加pieceCounts = JSON.parse(JSON.stringify(pieceCounts)); // just clone。 -
我认为这是一个指针问题,我不明白为什么使用 .slice() 从来没有用过,只是在一秒钟前才发现这是因为虽然它正在复制数组中的对象的指针数组都还是一样的。你应该把这个作为答案,它是正确的
-
为什么不能有id: 1, currentBlack: 0, currentWhite: 0的组合?
-
因为初始输入设置了下限,这样您就可以更轻松地递归遍历它,而不必在对象中有 min: 值。整个程序正在计算可以设置尺寸为 x,y 的棋盘的合法方式的数量。这是拼图中的最后一块,它为棋盘上的棋子数量生成了所有可能的组合。零不一定是我的特定程序的最低限度的原因是,您必须始终拥有 1 个黑色和 1 个白色国王才能使棋盘合法。还有其他限制,但这里适用
标签: javascript recursion permutation