|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * NQueen Algorithm |
| 5 | + * |
| 6 | + * @author Arvin Atri <[email protected]> |
| 7 | + * @date 2023/12/06 |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | +/* |
| 11 | +|-------------------------------------------------------------------------- |
| 12 | +| NQueen Algorithm |
| 13 | +|-------------------------------------------------------------------------- |
| 14 | +| |
| 15 | +| Arrange N queens on an NxN chessboard without attacking each other diagonally, |
| 16 | +| horizontally, or vertically—a classic problem in combinatorial optimization and |
| 17 | +| recursion. |
| 18 | +| |
| 19 | +*/ |
| 20 | + |
| 21 | +class NQueen |
| 22 | +{ |
| 23 | + |
| 24 | + //Variable to store the dimensions of the chessboard |
| 25 | + protected int $n; |
| 26 | + |
| 27 | + //Chessboard represented as an array |
| 28 | + protected array $board; |
| 29 | + |
| 30 | + //Constructor to initialize the chessboard dimensions and fill it with zeros |
| 31 | + public function __construct(int $n) |
| 32 | + { |
| 33 | + $this->n = $n; |
| 34 | + $this->board = array_fill(0, $this->n, array_fill(0, $this->n, 0)); |
| 35 | + } |
| 36 | + |
| 37 | + public function solve_n_queen($col = 0): void |
| 38 | + { |
| 39 | + if ($col >= $this->n) { |
| 40 | + $this->print_result(); |
| 41 | + } |
| 42 | + |
| 43 | + //Iterating through each row in the current column |
| 44 | + for ($row = 0; $row < $this->n; $row++) { |
| 45 | + if ($this->move_is_promising($row, $col)) { |
| 46 | + //Placing the queen at the current position |
| 47 | + $this->board[$row][$col] = 1; |
| 48 | + //Recursively solving for the next column |
| 49 | + $this->solve_n_queen($col + 1); |
| 50 | + //Backtracking by removing the queen from the current position |
| 51 | + $this->board[$row][$col] = 0; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function move_is_promising($row, $col): bool |
| 57 | + { |
| 58 | + //Checking if there is no queen in the same row |
| 59 | + for ($index = 0; $index < $col; $index++) |
| 60 | + if ($this->board[$row][$index]) |
| 61 | + return false; |
| 62 | + |
| 63 | + //Checking if there is no queen on the upper-left diagonal |
| 64 | + for ($i = $row, $j = $col; $i >= 0 && $j >= 0; $i--, $j--) |
| 65 | + if ($this->board[$i][$j]) |
| 66 | + return false; |
| 67 | + |
| 68 | + //Checking if there is no queen on the lower-left diagonal |
| 69 | + for ($i = $row, $j = $col; $i < $this->n && $j >= 0; $i++, $j--) |
| 70 | + if ($this->board[$i][$j]) |
| 71 | + return false; |
| 72 | + |
| 73 | + //If no conflicts are found, the move is promising |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + public function print_result(): void |
| 78 | + { |
| 79 | + for ($i = 0; $i < $this->n; $i++) { |
| 80 | + for ($j = 0; $j < $this->n; $j++) |
| 81 | + echo $this->board[$i][$j]; |
| 82 | + echo "\n"; |
| 83 | + } |
| 84 | + echo "\n"; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +$dimensions = 4; |
| 89 | +(new NQueen($dimensions))->solve_n_queen(); |
0 commit comments