0
0
DSA Cprogramming~10 mins

N Queens Problem in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - N Queens Problem
Start with empty board
Place queen in row 0, col 0
Check if safe
Place queen
Move to next row
Repeat placement
If no safe col in row
Backtrack to previous row
If all rows placed
Solution found
End or find more solutions
The flow shows placing queens row by row, checking safety, backtracking if needed, until all queens are placed safely.
Execution Sample
DSA C
bool solveNQueens(int row) {
  if (row == N) return true;
  for (int col = 0; col < N; col++) {
    if (isSafe(row, col)) {
      placeQueen(row, col);
      if (solveNQueens(row + 1)) return true;
      removeQueen(row, col);
    }
  }
  return false;
}
This code tries to place queens row by row, backtracking when no safe position is found.
Execution Table
StepOperationRowColumn TriedActionBoard State (Q=queen, .=empty)
1Try place queen00Safe, place queenQ . . . . . . . . . . . . . . .
2Try place queen10Not safe, try nextQ . . . . . . . . . . . . . . .
3Try place queen11Not safe, try nextQ . . . . . . . . . . . . . . .
4Try place queen12Safe, place queenQ . . . . . Q . . . . . . . . .
5Try place queen20Not safe, try nextQ . . . . . Q . . . . . . . . .
6Try place queen21Not safe, try nextQ . . . . . Q . . . . . . . . .
7Try place queen23Safe, place queenQ . . . . . Q . . . . Q . . . .
8Try place queen30Not safe, try nextQ . . . . . Q . . . . Q . . . .
9Try place queen31Safe, place queenQ . . . . . Q . . . . Q . Q . .
10All queens placedN/AN/ASolution foundQ . . . . . Q . . . . Q . Q . .
11Backtrack31Remove queenQ . . . . . Q . . . . Q . . . .
12Try next col32Not safe, try nextQ . . . . . Q . . . . Q . . . .
13Try next col33Not safe, backtrackQ . . . . . Q . . . . Q . . . .
14Backtrack23Remove queenQ . . . . . Q . . . . . . . . .
15Try next col24No column 4, backtrackQ . . . . . Q . . . . . . . . .
16Backtrack12Remove queenQ . . . . . . . . . . . . . . .
17Try next col13Safe, place queenQ . . . . . . Q . . . . . . . .
18Try place queen20Not safe, try nextQ . . . . . . Q . . . . . . . .
19Try place queen21Safe, place queenQ . . . . . . Q . Q . . . . . .
20Try place queen30Not safe, try nextQ . . . . . . Q . Q . . . . . .
21Try place queen31Not safe, try nextQ . . . . . . Q . Q . . . . . .
22Try place queen32Not safe, try nextQ . . . . . . Q . Q . . . . . .
23Try place queen33Not safe, backtrackQ . . . . . . Q . Q . . . . . .
24Backtrack21Remove queenQ . . . . . . Q . . . . . . . .
25Try next col22Not safe, try nextQ . . . . . . Q . . . . . . . .
26Try next col23Not safe, backtrackQ . . . . . . Q . . . . . . . .
27Backtrack13Remove queenQ . . . . . . . . . . . . . . .
28Try next col14No column 4, backtrackQ . . . . . . . . . . . . . . .
29Backtrack00Remove queen. . . . . . . . . . . . . . . .
30Try next col01Safe, place queen. Q . . . . . . . . . . . . . .
31Continue similarlyN/AN/AContinue placing queensPartial board states...
32EventuallyN/AN/AFind all solutions or stopFinal board states
33EndN/AN/ANo more columns to tryFinal or no solution
💡 All rows processed with safe queen placements or all options exhausted with backtracking
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7After Step 10After Step 29After Step 30Final
row0123N (4)01N or backtracked
col0033101varies
board[empty]Q . . . . . . . . . . . . . . .Q . . . . . Q . . . . . . . . .Q . . . . . Q . . . . Q . . . .Q . . . . . Q . . . . Q . Q . .. . . . . . . . . . . . . . . .. Q . . . . . . . . . . . . . .varies with placement
Key Moments - 3 Insights
Why do we backtrack when no safe column is found in a row?
Because placing a queen in the current row is impossible without conflicts, so we remove the queen from the previous row and try a different column there (see steps 13-15 and 25-27 in execution_table).
How do we know if a position is safe for placing a queen?
We check columns, and diagonals for existing queens before placing (implied in 'Safe' or 'Not safe' in execution_table rows like 2, 4, 5).
Why does the algorithm stop when row == N?
Because all N queens have been placed safely on the board, meaning a solution is found (see step 10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 10, what is the board state?
A. Q . .\n. . . Q\n. Q . .\n. . . .
BQ . . .\n. . . .\n. . . .\n. . . .
CQ . . .\n. . Q .\n. . . Q\n. Q . .
D. . . .\n. . . .\n. . . .\n. . . .
💡 Hint
Refer to the 'Board State' column at step 10 in execution_table.
At which step does the algorithm first backtrack by removing a queen?
AStep 11
BStep 10
CStep 5
DStep 1
💡 Hint
Look for 'Remove queen' action in execution_table.
If the board size N is increased, how would the variable 'row' change in variable_tracker?
AIt would decrease
BIt would go up to N instead of 4
CIt would stay the same
DIt would become negative
💡 Hint
The 'row' variable tracks current row; it goes from 0 to N (see variable_tracker).
Concept Snapshot
N Queens Problem:
- Place N queens on NxN board
- One queen per row, no two queens attack
- Use backtracking: place queen, check safety
- If stuck, remove last queen and try next column
- Stop when all queens placed safely
Full Transcript
The N Queens Problem places N queens on an NxN chessboard so that no two queens threaten each other. The algorithm tries to place a queen in each row, checking if the position is safe by ensuring no other queen is in the same column or diagonal. If a safe position is found, it places the queen and moves to the next row. If no safe position exists in a row, it backtracks by removing the queen from the previous row and tries the next column there. This continues until all queens are placed or all options are exhausted. The execution table shows step-by-step queen placements, safety checks, backtracking, and board states. The variable tracker monitors the current row, column tried, and board configuration after each step. Key moments clarify why backtracking is needed, how safety is checked, and when the algorithm stops. The visual quiz tests understanding of board states, backtracking steps, and variable changes. The snapshot summarizes the problem and backtracking approach in simple terms.