0% found this document useful (0 votes)
380 views6 pages

Practical No: 02: Aim: A) Write A Program To Simulate 4-Queen / N-Queen Problem. Diagram

The document describes programs to solve two classic artificial intelligence problems: 1) The N-Queen problem, which places N queens on an N x N chessboard so that no two queens attack each other. The code uses a recursive backtracking algorithm. 2) The Tower of Hanoi problem, which moves a stack of disks from one rod to another according to the rules of the puzzle. The code uses a recursive algorithm to solve the problem. Both programs take user input and output the steps of the solutions.

Uploaded by

56 Dhiraj Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
380 views6 pages

Practical No: 02: Aim: A) Write A Program To Simulate 4-Queen / N-Queen Problem. Diagram

The document describes programs to solve two classic artificial intelligence problems: 1) The N-Queen problem, which places N queens on an N x N chessboard so that no two queens attack each other. The code uses a recursive backtracking algorithm. 2) The Tower of Hanoi problem, which moves a stack of disks from one rod to another according to the rules of the puzzle. The code uses a recursive algorithm to solve the problem. Both programs take user input and output the steps of the solutions.

Uploaded by

56 Dhiraj Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Name : Navneet

Roll No :

Practical No : 02

AIM :
A) Write a program to simulate 4-Queen / N-Queen problem.

DIAGRAM :

CODE :
class QueenChessBoard:
def __init__(self, size):
# board has dimensions size x size
self.size = size
# columns[r] is a number c if a queen is placed at row r and column c.
# columns[r] is out of range if no queen is place in row r.
# Thus after all queens are placed, they will be at positions
# (columns[0], 0), (columns[1], 1), ... (columns[size - 1], size - 1)
self.columns = []
def place_in_next_row(self, column):
self.columns.append(column)
def remove_in_current_row(self):
return self.columns.pop()
def is_this_column_safe_in_next_row(self, column):
# index of next row

Artificial Intelligence
Name : Navneet
Roll No :

row = len(self.columns)
# check column
for queen_column in self.columns:
if column == queen_column:
return False
# check diagonal
for queen_row, queen_column in enumerate(self.columns):
if queen_column - queen_row == column - row:
return False
# check other diagonal
for queen_row, queen_column in enumerate(self.columns):
if ((self.size - queen_column) - queen_row
== (self.size - column) - row):
return False
return True
def display(self):
for row in range(self.size):
for column in range(self.size):
if column == self.columns[row]:
print('Q', end=' ')
else:
print('.', end=' ')
print()
def solve_queen(size):
"""Display a chessboard for each possible configuration of placing n
queens on an n x n chessboard and print the number of such
configurations."""
board = QueenChessBoard(size)
number_of_solutions = 0
row = 0
column = 0
# iterate over rows of board
while True:
# place queen in next row
while column < size:
if board.is_this_column_safe_in_next_row(column):
board.place_in_next_row(column)
row += 1
column = 0
break
else:
column += 1
# if could not find column to place in or if board is full

Artificial Intelligence
Name : Navneet
Roll No :

if (column == size or row == size):


# if board is full, we have a solution
if row == size:
board.display()
print()
number_of_solutions += 1
# small optimization:
# In a board that already has queens placed in all rows except
# the last, we know there can only be at most one position in
# the last row where a queen can be placed. In this case, there
# is a valid position in the last row. Thus we can backtrack two
# times to reach the second last row.
board.remove_in_current_row()
row -= 1
# now backtrack
try:
prev_column = board.remove_in_current_row()
except IndexError:
# all queens removed
# thus no more possible configurations
break
# try previous row again
row -= 1
# start checking at column = (1 + value of column in previous row)
column = 1 + prev_column
print('Number of solutions:', number_of_solutions)
n = int(input('Enter n: '))
solve_queen(n)

OUTPUT :

Artificial Intelligence
Name : Navneet
Roll No :

Artificial Intelligence
Name : Navneet
Roll No :

B) Write a program to solve tower of Hanoi problem.

DIAGRAM :

CODE :
Def moveDisk(fp,tp):
Print(“moving disk from”,fp,”to”,tp)
Def moveTower(height,fromPole, toPole, withPole):
If height >= 1:
moveTower(height-1,fromPole,withPole,toPole)
moveDisk(fromPole,toPole)
moveTower(height-1,withPole,toPole,fromPole)
moveTower(3,”A”,”B”,”C”)

OUTPUT :

Artificial Intelligence
Name : Navneet
Roll No :

Artificial Intelligence

You might also like