Beispiel #1
0
/**
 * Solve the 8 Queens puzzle
 * @return array A list of solutions
*/
function solve8queens2()
{
    $board = 8;
    // create the different possible rows
    for ($x = 0; $x < $board; ++$x) {
        $row[$x] = 1 << $x;
    }
    // create all the possible orders of rows
    $solcount = 0;
    $solutions = array();
    while ($row != false) {
        if (checkBoard($row, $board)) {
            if (!in_array($row, $solutions)) {
                $solutions[] = $row;
                $solutions = findRotation($row, $board, $solutions);
                ++$solcount;
            }
        }
        $row = nextPermutation($row);
    }
    return $solutions;
}
        }
        ++$a;
    }
    return true;
}
if (isset($_POST['process']) && isset($_POST['boardX'])) {
    //Within here is the code that needs to be run if process is clicked.
    //First I need to create the different possible rows
    for ($x = 0; $x < $boardX; ++$x) {
        $row[$x] = 1 << $x;
    }
    //Now I need to create all the possible orders of rows, will be equal to [boardY]!
    $solcount = 0;
    $solutions = array();
    while ($row != false) {
        if (checkBoard($row, $boardX)) {
            if (!in_array($row, $solutions)) {
                $solutions[] = $row;
                renderBoard($row, $boardX);
                $solutions = findRotation($row, $boardX, $solutions);
                ++$solcount;
            }
        }
        $row = pc_next_permutation($row);
    }
    echo "<br><br>&nbsp&nbsp&nbsp&nbspRows/Columns: " . $boardX . "<br>&nbsp&nbsp&nbsp&nbspUnique Solutions: " . $solcount . "<br>&nbsp&nbsp&nbsp&nbspTotal Solutions: " . count($solutions) . "  - Note: This includes symmetrical solutions<br>";
    //print_r($solutions);
}
//This code collects the starting parameters
echo <<<_END
<form name="input" action="queens.php" method="post">