You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution:
def solveNQueens(self, n: int):
def is_safe(board, row, col):
# Check if there is a queen in the same column
for i in range(row):
if board[i][col] == 'Q':
return False
# Check upper-left diagonal
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 'Q':
return False
# Check upper-right diagonal
for i, j in zip(range(row, -1, -1), range(col, n)):
if board[i][j] == 'Q':
return False
return True
def solve(board, row):
if row == n:
result.append(["".join(row) for row in board])
return
for col in range(n):
if is_safe(board, row, col):
board[row][col] = 'Q'
solve(board, row + 1)
board[row][col] = '.'
result = []
board = [['.' for _ in range(n)] for _ in range(n)]
solve(board, 0)
return result
class Solution:
def solveNQueens(self, n: int):
def is_safe(board, row, col):
# Check if there is a queen in the same column
for i in range(row):
if board[i][col] == 'Q':
return False
Get user input for N
n = int(input("Enter the value of N: "))
solution = Solution()
solutions = solution.solveNQueens(n)
Display solutions
for sol in solutions:
for row in sol:
print(row)
print()
This is missing
The text was updated successfully, but these errors were encountered: