Example #1
0
function solve()
{
    $grid = makeEmptyGrid();
    $list = [0, 1, 2, 3, 4, 5, 6, 7];
    $tries = 0;
    $solutions = 0;
    foreach (permutations($list) as $permutation) {
        $queens = [];
        for ($i = 0; $i < 8; $i++) {
            $queens[] = [$i, $permutation[$i]];
        }
        if (check($queens, $grid)) {
            printSolution($queens);
            $solutions++;
        } else {
            $tries++;
        }
    }
    //print_r($solutions);
    echo 'permutations tried: ' . $tries . '. solutions found: ' . $solutions;
}
Example #2
0
function solution(array $A, $N)
{
    $depthMax = 0;
    $aqIterations = 0;
    $arIterations = 0;
    for ($i = 0; $i < $N - 2; $i++) {
        $Ap = $A[$i];
        $Aq = $Ap;
        $k = $i;
        while ($k < $N - 1 && $A[$k + 1] < $Aq) {
            $Aq = $A[$k + 1];
            $k++;
            $aqIterations++;
        }
        $Ar = $Aq;
        while ($k < $N - 1 && $A[$k + 1] > $Ar) {
            $Ar = $A[$k + 1];
            $k++;
            $arIterations++;
        }
        $depthNow = min($Ap - $Aq, $Ar - $Aq);
        if ($depthNow > $depthMax) {
            $depthMax = $depthNow;
        }
    }
    //    echo "aqIterations: $aqIterations; arIterations: $arIterations\n";
    return $depthMax;
}
$a = [0, 1, 3, -2, 0, 1, 0, -3, 2, 3];
printSolution('solution', [$a, count($a)]);