Download presentation

Presentation is loading. Please wait.

1
PROLOG 8 QUEENS PROBLEM

2
**8 QUEENS Place 8 queens on an 8 X 8 chessboard so that:**

Queens do not attack each other

3
**8 QUEENS Solution will be programmed as a unary predicate**

solution(Position) Will be true if Position represents a valid chessboard position for the 8 queens and no 2 queens can attack each other

4
**8 QUEENS Representation of the chess board position**

Each queen will be represented by X and Y (row and column) We will define a new operator, : X:Y Potential solution: [X1:Y1, X2:Y2, X3:Y3, …, X8:Y8]

5
**8 QUEENS Since every queen will have to be in a different row **

We can hard code all the Xis to be 1 to 8 A potential solution can be templated as : [1:Y1, 2:Y2, 3:Y3, .., 8:Y8]

6
**8 QUEENS Recursive formulation of the problem**

We will “think” about this problem in terms of placing N queens not just 8 recursive relation between N queens and (N – 1) queens Use this to solve when number of queens is 8

7
**8 QUEENS Recursive formulation of the problem**

List of N queens Base case: list of queens is empty General case: list of queens looks like [X:Y | Others] and Others itself is a solution X and Y must be integers between 1 and 8 The X:Y queen must not attack any queen in Others

8
**8 QUEENS solution([]). % no queen good solution([X:Y | Others]) :-**

solution(Others), member(Y,[1,2,3,4,5,6,7,8]), noattack(X:Y,Others). % need to define % X will be hard coded 1 to 8

9
**8 QUEENS Need to define predicate noattack noattack takes 2 arguments:**

A queen A list of queens Base case: list of queens is empty The queen cannot attack the queens in the empty list (there is not any)

10
**8 QUEENS Base case for noattack noattack(_,[]).**

A queen (1st argument) cannot be attacked by 0 queen

11
8 QUEENS General case If the list of queens is not empty, then it can be written [X1:Y1 | Others] where X1:Y1 represents the first queen (head) and Others the other queens (tail) noattack(X:Y,[X1:Y1 | Others]) :- ????

12
**8 QUEENS noattack(X:Y,[X1:Y1 | Others]) :- ???? That will be true if:**

Queen X:Y cannot attack queen X1:Y1, and Queen X:Y cannot attack any of the queens in the list of queens Others

13
**8 QUEENS One queen vs another (X:Y vs X1:Y1)**

X and X1 must be different but that is automatic (the Xis are hard coded) Y and Y1 must be different (column) Y1 – Y must be different from X1 – X (main diagonal) Y1 – Y must be different from X – X1 (other diagonal)

14
**8 QUEENS One queen vs another (X:Y vs X1:Y1)**

Y1 and Y must be different X1 and X are hard coded and automatically different Y1 – Y different from X1 – X Y1 – Y different from X – X1 We will use the =\= operator , arithmetic not equal

15
**8 QUEENS General case for noattack noattack(X:Y,[X1:Y1 | Others]) :-**

Y =\= Y1, Y1 – Y =\= X1 – X, Y1 – Y =\= X – X1, noattack(X:Y, Others). % X and X1 are hard coded different

16
**8 QUEENS solution([]). % no queen solution([X:Y | Others]) :-**

solution(Others), member(Y,[1,2,3,4,5,6,7,8]), noattack(X:Y,Others).

17
**8 QUEENS noattack(_,[]). noattack(X:Y,[X1:Y1 | Others]) :- Y =\= Y1,**

Y1 – Y =\= X1 – X, Y1 – Y =\= X – X1, noattack(X:Y, Others).

18
**8 QUEENS Hard code the Xis into 1 to 8**

template([1:Y1, 2:Y2, 3:Y3, …, 8:Y8]). To run the program: template(S), solution(S).

Similar presentations

OK

1 Applications of Recursion (Walls & Mirrors - Chapter 5)

1 Applications of Recursion (Walls & Mirrors - Chapter 5)

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Shared value creating ppt on ipad Ppt on economic reforms in india 1991 crisis Ppt on dry cell and wet cell batteries Ppt on human chromosomes diagram Ppt on water cycle Ppt on introduction to information security Download ppt on tsunami warning system Ppt on as 14 amalgamation synonym Ppt on annual review meeting Muscular system anatomy and physiology ppt on cells