Exemple #1
0
function findPaths($nr, $branches, &$paths, $prevPath)
{
    $path = $prevPath . '-' . $nr;
    $out0 = $branches[$nr]['out'][0];
    $out1 = $branches[$nr]['out'][1];
    $last = findLastElement($path);
    $found = false;
    if (isset($out0) && strpos($path, "{$last}-{$out0}") === false) {
        findPaths($out0, $branches, $paths, $path);
        $found = true;
    }
    if (isset($out1) && strpos($path, "{$last}-{$out1}") === false) {
        findPaths($out1, $branches, $paths, $path);
        $found = true;
    }
    if (!$found) {
        $paths[] = $path;
    }
}
Exemple #2
0
function findPaths($items, $map, &$paths, $perms = [])
{
    if (!empty($items)) {
        $count = count($items);
        for ($i = $count - 1; $i >= 0; --$i) {
            $newitems = $items;
            $newperms = $perms;
            list($foo) = array_splice($newitems, $i, 1);
            array_unshift($newperms, $foo);
            findPaths($newitems, $map, $paths, $newperms);
        }
    } else {
        $dist = 0;
        foreach ($perms as $key => $perm) {
            if (isset($perms[$key + 1])) {
                $next = $perms[$key + 1];
                $dist += $map[$perm][$next];
            }
        }
        $paths[] = $dist;
    }
}