N-Queens
LeetCode 51 · కఠినం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
Column-by-column backtracking with row + diagonal hashes.
నేను N-Queens (LeetCode 51) ను ఇలా పరిష్కరించాను — Constraint Satisfaction ప్యాటర్న్లోని ఒక కఠినం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.
కింది వివరణ ఇంగ్లీష్లో ఉంది.
Intuition
Place one queen per column. For each row in the current column, place a queen if no earlier queen attacks that square, recurse to the next column, then remove it. Attacks can be checked by scanning, or in O(1) with arrays for rows and both diagonals.
Approach
- 1
Version 1: check left, upper-left and lower-left squares by scanning.
- 2
Version 2: row_hash[row], diag[row+col], anti-diag[n−1+col−row].
- 3
Place, mark, recurse, unmark.
- 4
col == n → record the board.
నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు
Walk three directions.
O(1) attack check.
Scan for attacksPythonO(n!·n) time
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
res=[]
board = [["."]*n for _ in range(n)]
def can_place_queen(row,col,board):
temp_row = row
temp_col = col
#move left
while col>=0:
if board[row][col]=="Q":
return False
col-=1
#left diagonal
row = temp_row
col = temp_col
while row>=0 and col>=0:
if board[row][col]=="Q":
return False
row-=1
col-=1
#bottom diagonal
row = temp_row
col = temp_col
while row<n and col>=0:
if board[row][col]=="Q":
return False
row+=1
col-=1
return True
def backtrack(col,board):
if col==n:
res.append(["".join(row) for row in board])
return
for row in range(n):
if can_place_queen(row,col,board):
board[row][col]="Q"
backtrack(col+1,board)
board[row][col]="."
backtrack(0,board)
return resHash arraysPythonO(n!) time
class Solution:
def solveNQueens(self, n: int) -> List[List[str]]:
res=[]
board = [["."]*n for _ in range(n)]
row_hash = [0]*n
bottom_left_diagonal = [0]*(2*n-1)
top_left_diagonal = [0]*(2*n-1)
def backtrack(col,board):
if col==n:
res.append(["".join(row) for row in board])
return
for row in range(n):
if row_hash[row]==0 and bottom_left_diagonal[row+col]==0 and top_left_diagonal[n-1+col-row]==0:
board[row][col]="Q"
row_hash[row]=1
bottom_left_diagonal[row+col]=1
top_left_diagonal[n-1+col-row]=1
backtrack(col+1,board)
board[row][col]="."
row_hash[row]=0
bottom_left_diagonal[row+col]=0
top_left_diagonal[n-1+col-row]=0
backtrack(0,board)
return resN-Queens — తరచూ అడిగే ప్రశ్నలు
N-Queens వెనుక ఉన్న intuition ఏమిటి?
Place one queen per column. For each row in the current column, place a queen if no earlier queen attacks that square, recurse to the next column, then remove it. Attacks can be checked by scanning, or in O(1) with arrays for rows and both diagonals.
N-Queens (LeetCode 51) ను ఎలా పరిష్కరించాలి?
1. Version 1: check left, upper-left and lower-left squares by scanning. 2. Version 2: row_hash[row], diag[row+col], anti-diag[n−1+col−row]. 3. Place, mark, recurse, unmark. 4. col == n → record the board.
N-Queens యొక్క time, space complexity ఎంత?
Time: O(n!). Space: O(n²) board. (Accepted approach: Hash arrays.)
N-Queens లో సాధారణ పొరపాట్లు ఏమిటి?
Only check to the left — columns to the right are still empty.
N-Queens ను పరిష్కరించడానికి ఒకటి కంటే ఎక్కువ మార్గాలు ఉన్నాయా?
Scan for attacks (O(n!·n) time): Walk three directions. Hash arrays (O(n!) time): O(1) attack check.