Lab Report 03
Lab Report 03
[For teachers use only: Don’t write anything inside this box]
Marks: Signature:
Comments: Date:
Contents
0.1 OBJECTIVES/AIM . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
0.2 PROCEDURE / ANALYSIS / DESIGN . . . . . . . . . . . . . . . . . 2
0.3 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
0.4 Test Result / Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
0.5 DISCUSSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
0.6 CONCLUSION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1
0.1 OBJECTIVES/AIM
• To solve the classic N-Queens problem using the backtracking algorithm.
• To understand how constraint satisfaction problems work through the example of
N-Queens.
• To find all possible valid solutions for placing N queens on an N × N chessboard.
• To represent the solutions visually using a box format resembling a real chess-
board.
• Maintain a list of tuples, where each tuple stores the column and row position of
each queen.
• Create a helper function is_safe() to check whether a queen can be safely
placed at a particular position without being attacked.
• Use a recursive function solve() to attempt placing queens row by row:
– For each row, try placing a queen in each column.
– If placing a queen at a position is safe, recursively attempt to place the next
queen.
– If placing a queen leads to a dead-end (no safe placement in the next row),
backtrack and try the next column.
• When a valid solution (placing N queens successfully) is found, add it to the list
of solutions.
• Implement a make_board() function to visualize the solutions as a list of strings
with ’Q’ indicating a queen and ’.’ indicating an empty space.
• Use a print_boxed() function to display each solution neatly in a box-style
format similar to an actual chessboard layout.
2
0.3 Implementation
def solve_n_queens(n):
solutions = []
backtrack(0, [])
return solutions
def print_boxed_boards(solutions):
for idx, board in enumerate(solutions, start=1):
print(f"Solution {idx}:")
for row in board:
print("+---" * len(row) + "+")
for ch in row:
print(f"| {’Q’ if ch == ’Q’ else ’ ’} ", end="")
print("|")
print("+---" * len(board[0]) + "+\n")
# --- Main Program ---
n = int(input("Enter number of queens: "))
all_solutions = solve_n_queens(n)
print(f"\nTotal solutions for {n} queens: {len(all_solutions)}\n")
print_boxed_boards(all_solutions)
3
0.4 Test Result / Output
0.5 DISCUSSION
The N-Queens problem is a classic example of a constraint satisfaction problem (CSP),
where multiple conditions must be satisfied simultaneously. Backtracking is an effective
technique to solve such problems because it explores possible states in a systematic way
and abandons those paths that cannot lead to a valid solution.
During the experiment:
• It was observed that early pruning (checking for safety while placing queens)
drastically reduces the number of possibilities that need to be explored.
• The complexity increases rapidly with the value of N. For instance, while N = 4
and N = 8 can be computed quickly, larger values of N (e.g., N = 14 or N = 16)
require significant computation time due to the exponential growth in possibili-
ties.
4
0.6 CONCLUSION
In this lab, the N-Queens problem was successfully solved using a backtracking ap-
proach in Python. By leveraging recursion and constraint checking, all valid configura-
tions for placing queens on the chessboard were found efficiently.
The experiment deepened the understanding of:
• How early pruning techniques improve the performance of recursive search algo-
rithms.