Question

Write a Python Program to solve the N-Queen Problem without using Recursion.

08 Aug 2026
Answer :
Word Count : 1224
# N-Queen Problem Using Python Without Recursion The N-Queen problem is a classical problem in computer science and artificial intelligence that demonstrates the concepts of state-space search, constraint satisfaction, and backtracking. The problem is to place N queens on an N × N chessboard in such a way that no two queens attack each other. In chess, a queen can attack another queen if both are placed in the same row, the same column, or the same diagonal. Therefore, a valid solution must satisfy all these constraints simultaneously. In a Programming and AI Lab, the N-Queen problem is useful for understanding how an intelligent system can search through a large number of possible states and eliminate invalid states. The usual solution uses recursion to implement backtracking. However, the problem can also be solved without recursion by using an explicit stack. The stack performs the same task that the system call stack normally performs during recursive backtracking. The basic idea is to place one queen in each row. Since every row contains exactly one queen, we only need to decide the column in which the queen of each row should be placed. Before placing a queen, we check whether its position is safe. A position is safe when no previously placed queen exists in the same column or on either diagonal. For a board of size N, the possible positions form a search space. At every row, the algorithm tries different columns. If a safe position is found, the queen is placed and the algorithm moves to the next row. If no safe position is available, the algorithm backtracks to the previous row, removes the previously placed queen, and tries another column. In the non-recursive implementation, this backtracking information is maintained explicitly using a stack. The following Python program solves the N-Queen problem without using recursion. It uses a list as a stack and stores the column selected for each row. ```python def is_safe(queens, row, col): for previous_row in range(row): previous_col
________ ____ _____ _________ ________ _______ ________ ________ ________.
________ ___ _____ _________ _______ __________ ___ ________ ________ ________ _____.
____ _________ ________ __________ _____ ___ ___.
_________ ________ ___ _____ ________ ____ ___ _______ ___ __________ _____ ____.
______ ________ _______ _____ _______.
____ ____ ______ _______ _______ ____.
______ __________ _________ ____ ____ ____ ______ _____ _______.
_______ ______ __________ ________ __________ _________.
________ ______ ______ __________ ______ ___.
___ _____ _____ ______ ________.
______ ____ _____ ____ ________ __________ ___ _____ _________.
____ _________ __________ _________ ______ ___.
_________ _________ _______ ______ ___ _________ ____ ___ _______ ________ ___ _________.
_______ ___ _______ ________ ________ _____ _______.
_________ __________ _____ ____ __________ ______ ________ ___ _____ _____.
_____ __________ ___ ________ _____ __________ ____ ________ __________ _______ ___ _____.
_____ ___ _____ _________ ___ _________.
______ ___ __________ _______ _________ _____ ___ ____ ________ ___ ____.
_________ ________ ___ ________ _________ _________ ________ __________ _________ _____ ____ ____.
___ ___ _______ _______ ___.
______ __________ _______ ________ _______ ___.
__________ ______ _________ _________ ________ __________ _________ __________ _______ _____ _______.
______ _______ ______ ______ _______.
___ _______ ____ ____ _____ ___.
___ _________ ___ ___ ___ ___ _____ _____ _________ __________ _____ ________.
________ _____ _________ ____ _______ ________ _______ _________ ______ ________ ______.
__________ _____ _______ _____ ___ _____ ______ ___ ___ _____ ______ _____.
____ __________ _______ ___ ___.
______ _______ ___ ________ ______ _____ ____ _______ _______ ________ __________.
____ ____ _________ ____ ________ ________ ___ ______ __________ _________ ______.
__________ _____ _______ ___ ___ ________ ____ ____.
______ _________ ___ _________ ____ _____ _______ ___ ________ _____ _________ _______.
_____ _______ _______ ______ ________ _______ ________.
____ _____ ______ ____ ______.
_____ _______ _______ ________ __________ _________ __________ ______ __________ _______ ________ ________.
___ _________ ____ ___ _______ ________ _____.
________ _____ _______ ____ ________ ___ ______ _______ _________ _____ ____.
_______ ________ _________ __________ _________ ________ _____ ___.
_______ _________ ________ ____ ____ _____ ____.
______ _______ ______ _____ ___ ___ _________ ________ _____ ____.
__________ ____ _______ ________ _________ _____ ______ ____ _________ ___ _________ _________.
_____ ______ ________ ____ _______ _____ _____ ______ _______ _________ ________.
______ _________ __________ ________ ______.
_______ _____ _______ ____ ______ _______.
__________ ___ _________ __________ _____.
___ ___ ____ __________ ________ __________ ____ ____ ____ ___ ____.
_________ ______ _________ _____ ___ ______.
__________ __________ _____ _____ _____ _______ _________ _________ ___ _____ ________.
_____ ____ __________ ____ ______ _________ ______ _________ _____ _________ ______.
_____ __________ ________ _______ _______ _________ ________ __________ ________.
______ _______ _________ __________ _________ ______.
_______ ______ ____ ________ ______ ___ _____ _________ _________ ____ ________.
_________ ___ _____ ___ ___ ______.
____ ___ _______ ____ _____ ____ ___ ______ _____.
_____ ______ _______ __________ _________ ___ ______ ________ _______ _______ ___ ____.
________ ______ ______ __________ ___ ______ ____ _______ __________ _____ ______.
____ ___ _________ __________ __________ _____ _________ _________.
_________ _______ _______ ___ ____ ________ _______ ________ ________ ____ ____ _______.
_______ ________ __________ _________ ___ ___ ______ _____ ____ ______.
______ ______ ____ ________ ___.
__________ ____ ___ ________ _____.
________ _________ ____ __________ ______ ________ ______ _________ ______ _______.
____ __________ ____ _________ _________.
_________ _____ _____ _________ _________ ______ _______ ____ __________ _______.
_________ _____ ____ ________ _______ _______ _____ ______.
__________ ___ __________ ___ __________ ______ _____ ____ ____ _________ _____ _______.
_________ ________ __________ _________ ____ _____ _________ ____ __________.
_________ ___ ______ _________ ______ _________ ________ _____ _____ _______ __________ ___.
____ _____ _________ _____ _________ _____ ________ _______.
__________ ___ ________ _______ ______ ___ ___ ____ __________ ____ ________ ___.
___ ____ __________ _____ ________ _______ _________ ________ __________ _____ __________.
___ ___ _____ ______ __________ ______ ___ __________ ___ _____ __________.
________ _______ ____ ___ _________ _____ ____ ________ _____.
_________ ___ ______ ____ ___.
_________ ________ _______ ___ _________ __________.
_____ ____ ___ _______ __________ _______ __________.
___ ______ __________ ______ _____ _____ ___ __________ _________.
__________ _______ _________ ______ _______.
________ _________ _____ _____ ______ _______ ___ _____ ___ ________ ____ ______.
__________ __________ _________ _________ ___ ____ ______ ________ ___ ______ _____ ______.
_____ __________ ________ ____ ___ __________ _______ ________.
__________ _______ ______ ________ _______ ________ _________ ______ ________ _______ ___.
___ ______ _______ ________ __________ __________ _____ ____ _____ ______ __________.
_____ __________ ___ ____ ___ ____ __________ ________ ________.
__________ __________ ______ _______ _______ ______ ______ ___ ____ _________ __________.
__________ ______ ___ ______ ___ ____ _____ ____ __________ ______ _____ ______.
____ ________ ________ _____ ______ _____.
__________ ___ ____ _______ ___ _____ ____ __________ ______.
____ __________ _____ ___ __________ ____ ___ __________ _______.
_______ _______ _________ ____ _____ _________ ___ _________ ___.
__________ _______ _________ ________ _____ _________ _________ ___ ________ _____.
___ ______ __________ ______ _________ _____ ______ ___ _______ _______ _________ __________.
_____ ______ ______ __________ __________ __________ ___ _____.
_______ ____ _________ _____ ___ __________ ___ ______ __________.
________ _______ _____ ________ __________ __________ _______ ___ _____ ________ _______ ____.
__________ __________ _________ ______ ___ ______ ____ ___ ___ ____ _________.
_______ ____ ______ _______ ____ _________ ___ ________ ___.
___ _________ _______ ____ ____ _________ __________ _____.
_______ ________ ___ ________ ______ ______ _______ ______ ________ _______ _____.
_________ ____ _____ ______ __________ ______ ________ __________ __________ __________ ______.
IGNOU ACADEMY
Your IGNOU journey, made easier.
  • Solved Assignments
  • Study Notes
  • Guess Papers
  • Question Papers
Skip in 10
Top
📞
Call Support Instant phone assistance IGNOU MCSL-083 2026 - Write a Python Program to solve the N-Queen Problem without using Recu | IGNOU Academy
→
🟢
WhatsApp Chat Fast live messaging
→
Email Us Business enquiries & support
→