0% found this document useful (0 votes)
66 views11 pages

Design Problem Ai

The document describes a design problem related to artificial intelligence and solving the n queens problem. It outlines a Prolog program that uses backtracking to generate all possible solutions for placing n queens on an n×n chessboard so that no two queens attack each other. The program defines predicates to generate permutations, check for unique rows and diagonals, and draw the board configuration.

Uploaded by

shoyab17
Copyright
© Attribution Non-Commercial (BY-NC)
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)
66 views11 pages

Design Problem Ai

The document describes a design problem related to artificial intelligence and solving the n queens problem. It outlines a Prolog program that uses backtracking to generate all possible solutions for placing n queens on an n×n chessboard so that no two queens attack each other. The program defines predicates to generate permutations, check for unique rows and diagonals, and draw the board configuration.

Uploaded by

shoyab17
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 11

DESIGN PROBLEM

ARTIFICIAL INTELLIGENCE

SUBMITTED BY:-
NAME:-SHOYAB SIDDIQUEE
ROLL NO:-A16
SECTION:-A1806

SUBMITTED TO:-
MR.VIJAYENDHRA SIR
% solving n queens!

solve(Size, Solution) :-

sequence(Size, Grid),

permutation(Grid, Solution),

S is Size -1,

distinct_diagonals(Solution, S),

draw(Solution),

write('\n').

sequence(1, [0]).
sequence(Length, [Item|T]) :-

Item is Length -1,

sequence(Item, T),

!.

% Define solutions

goal(Grid) :-

distinct_rows(Grid),

distinct_diagonals(Grid).

%% check if all queens are on different rows

distinct_rows([]).

distinct_rows([H|T]) :-

\+ nth0(_, T, H),

distinct_rows(T).

%% check if queens are on different diagonals

distinct_diagonals(_, 0).

distinct_diagonals(Grid, Offset) :-

distinct_diagonals_aux(Grid, Offset),
NewOffset is Offset -1,

distinct_diagonals(Grid, NewOffset),

!.

distinct_diagonals_aux([], _).

distinct_diagonals_aux([H|T], Offset) :-

Test1 is H + Offset,

Test2 is H - Offset,

not( nth1(Offset, T, Test1) ),

not( nth1(Offset, T, Test2) ),

distinct_diagonals_aux(T, Offset).

% Draw the grid%

draw(Grid) :-

write('\n'),

length(Grid, Size),

draw_aux(Grid, Size),

!.

draw_aux([], Size) :-

draw_separator(Size).

draw_aux([H|T], Size) :-

draw_separator(Size),

draw_line(Size, H),

draw_aux(T, Size).

draw_separator(0) :-
write('+\n').

draw_separator(Size) :-

Size > 0,

write('+---'),

NewSize is Size -1,

draw_separator(NewSize).

draw_line(0, _) :-

write('|\n').

draw_line(Size, 0) :-

write('| x '),

NewSize is Size -1,

draw_line(NewSize, -1).

draw_line(Size, Position) :-

Size > 0,

write('| '),

NewSize is Size -1,

NewPosition is Position -1,

draw_line(NewSize, NewPosition).

You might also like