[英]How to make a permutation without the repetitions?
我的目標是測試我創建的游戲的解決方案。 就像數獨游戲一樣,你在玩一個網格。 網格讓你找到一個秘密組合。 我創建網格的方式無法確保只有一種可能的解決方案,如果有多個解決方案,則網格是錯誤的。 所以我必須用所有可能的組合來測試網格。 創建所有組合的最快方法是生成組合的每個可能數量的所有可能存在:例如 [1,1,1 | 1,2,0 | 3,0,0] 如果組合的長度是 3 和三個可能的值。 有了這些信息,我需要使用排列。
這是我寫的腳本:
function recursion($grd_chfr, $pt_chfr, $tab, &$tab_all) {
if($grd_chfr == 0) {
$tab_all[] = $tab;
return;
}
for($n = $pt_chfr; $n <= $grd_chfr; $n++) {
$b = $tab;
$b[] = $n;
recursion($grd_chfr - $n, $n, $b, $tab_all);
}
}
function permutations(array $elements)
{
if (count($elements) <= 1) {
yield $elements;
} else {
foreach (permutations(array_slice($elements, 1)) as $permutation) {
foreach (range(0, count($elements) - 1) as $i) {
yield array_merge(
array_slice($permutation, 0, $i),
[$elements[0]],
array_slice($permutation, $i)
);
}
}
}
}
$somme = 7;
$depart = 1;
$entrees = 7;
$tab_all = [];
$time_start = microtime(true);
// I use a recursive function to get every manner to spread values, to fill the combination whose length is $somme
recursion($somme, $depart, [], $tab_all);
$new_tab_all = [];
$count_gen = 0;
// if the number of possible values to use ($entrees) is higher or smaller than the length of the combination I adjust by adding zeros or by deleting distributions
foreach($tab_all as $tab){
if(count($tab) < $entrees){
$count = count($tab);
for($x = $depart; $x <= $entrees - $count; $x++){
$tab[] = 0;
}
// I create all possible permutation for each distribution, and that's where there is too much duplicatas
foreach(permutations($tab) as $combi){
if(!in_array($combi, $new_tab_all)) {
$new_tab_all[] = $combi;
}
}
}
elseif(count($tab) == $entrees){
$new_tab_all[] = $tab;
}
}
$time_stop = microtime(true);
echo round(($time_stop - $time_start), 2)."s d'écoulées, ".count($new_tab_all)." solutions (".$count_gen." générées).</br></br>";
我需要一個腳本來獲取組合的每個可能的唯一排列。 例如,有數組 [1,1,2,3],我想要一個返回 [[1,1,3,2], [1,3,1,2], [3,1,1, 2], [2,1,3,1], [2,3,1,1], [1,3,2,1], [1,1,2,3], ...] 從來沒有兩次相同的組合。
為了進行排列,我使用了一個腳本,但它會生成大量重復,例如 [1,1,1,1,2,3]。 我在那里找到了: 排列 - 所有可能的數字集
function permutations(array $elements)
{
if (count($elements) <= 1) {
yield $elements;
} else {
foreach (permutations(array_slice($elements, 1)) as $permutation) {
foreach (range(0, count($elements) - 1) as $i) {
yield array_merge(
array_slice($permutation, 0, $i),
[$elements[0]],
array_slice($permutation, $i)
);
}
}
}
}
我還找到了一個我認為是我需要的腳本,但它是用 python 編寫的,而我需要它在 php 中。 我在那里找到了: 具有唯一值的排列
class unique_element:
def __init__(self,value,occurrences):
self.value = value
self.occurrences = occurrences
def perm_unique(elements):
eset=set(elements)
listunique = [unique_element(i,elements.count(i)) for i in eset]
u=len(elements)
return perm_unique_helper(listunique,[0]*u,u-1)
def perm_unique_helper(listunique,result_list,d):
if d < 0:
yield tuple(result_list)
else:
for i in listunique:
if i.occurrences > 0:
result_list[d]=i.value
i.occurrences-=1
for g in perm_unique_helper(listunique,result_list,d-1):
yield g
i.occurrences+=1
a = list(perm_unique([1,1,2]))
print(a)
我不知道如何翻譯php中的第二個代碼,也不知道如何修改第一個代碼以排除重復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.