Why is this n-queens recursive solution not working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Why is this n-queens recursive solution not working?

here is the code: from copy import deepcopy as dc class Solution: def solveNQueens(self, n: int) -> List[List[str]]: result = list() board =[["."]*n]*n def issafe(row, col, board): #check the left elements in row for i in range(col+1): if board[row][i]=="Q": return False row-=1 col-=1 row2 = row col2 = col #check down diagonal elements while row>=0 and col>=0: if board[row][col]=="Q": return False row-=1 col-=1 while col2>=0 and row2<n: if board[row2][col2]=="Q": return False row2+=1 col2-=1 return True def helper(col, board): if col==n: result.append(["".join(row) for row in board]) return for row in range(n): if issafe(row, col, dc(board)): board[row][col] = "Q" helper(col+1, dc(board)) board[row][col] = "." helper(0, dc(board)) return result

2nd Jun 2022, 6:08 PM
Meghraj - avatar
1 Answer
- 1
Put the code in Code Playground and include a link to it in the question description. This allows us to see the problem and test solutions. Also, explain what didn't work. Good communication is key to get good help.
2nd Jun 2022, 10:44 PM
Emerson Prado
Emerson Prado - avatar