This function returns a boolean value True if it is safe to place a queen there considering the current state of the board. Parameters: board (2D matrix): The chessboard row, column: Coordinates of the cell on the board Returns: Boolean Value >>> is_safe([[0, 0, 0
(board: list[list[int]], row: int, column: int)
| 14 | |
| 15 | |
| 16 | def is_safe(board: list[list[int]], row: int, column: int) -> bool: |
| 17 | """ |
| 18 | This function returns a boolean value True if it is safe to place a queen there |
| 19 | considering the current state of the board. |
| 20 | |
| 21 | Parameters: |
| 22 | board (2D matrix): The chessboard |
| 23 | row, column: Coordinates of the cell on the board |
| 24 | |
| 25 | Returns: |
| 26 | Boolean Value |
| 27 | |
| 28 | >>> is_safe([[0, 0, 0], [0, 0, 0], [0, 0, 0]], 1, 1) |
| 29 | True |
| 30 | >>> is_safe([[0, 1, 0], [0, 0, 0], [0, 0, 0]], 1, 1) |
| 31 | False |
| 32 | >>> is_safe([[1, 0, 0], [0, 0, 0], [0, 0, 0]], 1, 1) |
| 33 | False |
| 34 | >>> is_safe([[0, 0, 1], [0, 0, 0], [0, 0, 0]], 1, 1) |
| 35 | False |
| 36 | >>> is_safe([[1, 0, 0], [0, 0, 0], [0, 0, 0]], 1, 2) |
| 37 | True |
| 38 | >>> is_safe([[1, 0, 0], [0, 0, 0], [0, 0, 0]], 2, 1) |
| 39 | True |
| 40 | >>> is_safe([[0, 0, 0], [1, 0, 0], [0, 0, 0]], 0, 2) |
| 41 | True |
| 42 | >>> is_safe([[0, 0, 0], [1, 0, 0], [0, 0, 0]], 2, 2) |
| 43 | True |
| 44 | """ |
| 45 | |
| 46 | n = len(board) # Size of the board |
| 47 | |
| 48 | # Check if there is any queen in the same upper column, |
| 49 | # left upper diagonal and right upper diagonal |
| 50 | return ( |
| 51 | all(board[i][j] != 1 for i, j in zip(range(row), [column] * row)) |
| 52 | and all( |
| 53 | board[i][j] != 1 |
| 54 | for i, j in zip(range(row - 1, -1, -1), range(column - 1, -1, -1)) |
| 55 | ) |
| 56 | and all( |
| 57 | board[i][j] != 1 |
| 58 | for i, j in zip(range(row - 1, -1, -1), range(column + 1, n)) |
| 59 | ) |
| 60 | ) |
| 61 | |
| 62 | |
| 63 | def solve(board: list[list[int]], row: int) -> bool: |