Ai Project File
Ai Project File
Ai Project File
(SCHOOL OF ENGINEERING)
Department of CST
(Artificial Intelligence – CSH205B-P)
ARTIFICAL INTELLEGENCE
(MINI PROJECT)
N-Queens Problem
Submitted to:
Mr. Narender
Submitted by:
Course: B-tech CSE CSTI4
G Keerthana (2K22CSUN01231)
Ch Akshaya (2K22CSUN01226)
Vishal (2K22CSUN01253)
Problem Description:
The N-Queens problem is a chess puzzle that involves placing N queens on an NxN
chessboard in such a way that no two queens threaten each other. The goal is to find
a configuration where no two queens share the same row, column, or diagonal. The
problem has received extensive research in computer science and mathematics and
is frequently used as a standard for evaluating algorithms and heuristics.
The significance of the N-Queens problem in AI and computer science lies in its
complexity and its applications in various fields.
Here are a few key points:
Approach:
The N-Queens problem can be solved using the backtracking algorithm in Turbo
Prolog. Here is a general description of the approach:
1. Start by placing the first queen in the first row and the first column of the
chessboard.
2. Move to the next row and try to place a queen in each column of that row,
one by one.
3. Check for clashes: For each column, check if placing a queen in that position
would lead to a clash with any previously placed queens. Clashes can occur if
two queens are in the same row, column, or diagonal.
4. Backtrack if clash occurs: If placing a queen in a particular column of the
current row leads to a clash, backtrack to the previous row and try placing the
queen in the next column of that row.
5. Repeat steps 3-4: Repeat the process of checking for clashes and backtracking
until a valid configuration is found or all possibilities have been exhausted.
6. Print or store solutions: Once a valid configuration is found, either print it or
store it as a solution. Continue the backtracking process to find all possible
solutions.
The backtracking algorithm explores all possible configurations by systematically
placing queens on the chessboard and checking their validity. If a queen cannot be
placed in a particular position without threatening another queen, the algorithm
backtracks and explores other possibilities.
Implementation:
Code:
% Place N queens on an NxN chessboard so that no two queens attack each
other.
% Facts
safe_queen(_, []).
safe_queen(Row, [Column|Queens]) :-
NextRow is Row + 1,
safe_queen(NextRow, Queens).
% any queen in Queens. Offset is the current row offset from Row being
checked.
NextOffset is Offset + 1,
queens(N, Queens) :-
numlist(1, N, Rows),
permutation(Rows, Queens),
safe_queen(1, Queens).
Results:
Conclusion:
Based on the search results, there are limited specific findings or insights related to
solving the N-Queens problem in Turbo Prolog. The N-Queens problem can be
solved in Turbo Prolog using various techniques, including backtracking and
constraint logic programming (CLP).
o CLP allows for the use of constraints to model and solve the problem.
2. The backtracking algorithm is a popular and intuitive approach for solving the
N-Queens problem.
3. Constraint logic programming (CLP) can be used in Turbo Prolog to solve the
N-Queens problem.
4. While specific Turbo Prolog code examples or reports are not readily available,
there are resources and examples available for solving the N-Queens problem
in other Prolog systems like SWI-Prolog.
---------------------------------------------------------------------------------------------