8 Queens. Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 8 – Search and conclusions.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Polynomial Time Algorithms for the N-Queen Problem Rok sosic and Jun Gu.
PROLOG 8 QUEENS PROBLEM.
Modern Programming Languages
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
Exercises – don’t use built in functions for these as we want the practice Write a recursive function to add up all the numbers in a list "flatten" a list.
L.O. Today you will learn how to play chess. How to Play Chess.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
The N-Queens Problem lab01.
Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
Smallest Number Of Queens Another presentation by Domenic Acitelli.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03.
Long Multiplication What is long multiplication?
Module 6 Lesson 16.
Data Structures Using C++ 2E Chapter 6 Recursion.
Vlad Furash & Steven Wine.  Problem surfaced in 1848 by chess player Max Bezzel as 8 queens (regulation board size)  Premise is to place N queens on.
FATIH UNIVERSITY Department of Computer Engineering Programming Style and Technique Notes for Ch.8 of Bratko For CENG421&553 Fall03.
Data Structures Using C++ 2E Chapter 6 Recursion.
Constraint Satisfaction Problems (CSPs) CPSC 322 – CSP 1 Poole & Mackworth textbook: Sections § Lecturer: Alan Mackworth September 28, 2012.
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Bellwork April 29 th – May 3 rd. Bellwork 4/29/13 Put eight x's into this 8-by-8 grid so that no two x's are in line with each other vertically, horizontally,
Solving N-Queens in Clojure
Two Dimensional Arrays
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
Collection Classes Eric Roberts CS 106B January 14, 2013 (Part 1: Vectors, Grids, Stacks, and Queues)
Chess By Kezia Farley.
A game based off of the esteemed classic By: Tadziu Kosiara.
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
Introduction to State Space Search
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
N- Queens Solution with Genetic Algorithm By Mohammad A. Ismael.
Chapter 13 Backtracking Introduction The 3-coloring problem
How do I multiply a triple digit number by a double digit? 145 x 67 = ? 267 x 19 = ? I’m sure that you’ll agree that there are many methods that we can.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
Splash Screen. Concept Example 1 Second-Order Determinant Definition of determinant Multiply. = 4Simplify. Answer: 4 Evaluate.
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
An Analysis of the n- Queens problem Saleem Karamali.
Eight Queens Problem The problem is to place 8 queens on a chess board so that none of them can attack the other. A chess board can be considered a plain.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CSG3F3/ Desain dan Analisis Algoritma
MULTIPLICATION 5 Multiplicand X 3 Multiplier 15 Product LET’S LEARN
Intro to Computer Science II
Figure 5.1 a) Five queens that cannot attack each other, but that can attack all of column 6; b) backtracking to column 5 to try another square for the.
Sit-In Lab 1 Ob-CHESS-ion
Objective: Today we will investigate the ‘magic’ in magic squares.
(c) 2004 MCSC Technology Training
Magic Squares   10   X.
Exercise: Dice roll sum Write a method diceSum similar to diceRoll, but it also accepts a desired sum and prints only arrangements that add up to.
CS Software Studio Assignment 1
Additional Example 2: Graphing Ordered Pairs Graph and label each point on a coordinate grid. A. L (3, 5) Start at (0, 0)
© T Madas.
When we multiply by 10 we are making the number ten times bigger.
The N-Queens Problem Search The N-Queens Problem Most slides from Milos Hauskrecht.
Exercise: Dice roll sum
Magical Hexagons (Teachers notes):
Matrices and Determinants
3-in-a-row Die 1 Die 2 (4 , 3).
3-in-a-row Die 1 Die 2 (4 , 3).
Presentation transcript:

8 Queens

Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions to this problem in Ivan Bratko’s book “Prolog Programming for Artificial Intelligence”. A chess-board is an 8x8 grid Y X

The problem is now finding such a list with the queens positioned on the board in such a way that they don’t attack one another. We will define a predicate: solution(Pos). We may represent the board as a list of eight elements. [X1:Y1, X2:Y2, X3:Y3, X4:Y4, X5:Y5, X6:Y6, X7:Y7, X8:Y8] Each X:Y pair represents the position of one queen on the board Representing the board

solution(Pos) returns a solution in a list “Pos”. Y X Y1 = 4, Y2 = 2, Y3 = 7, Y4 = 3, Y5 = 6, Y6 = 8, Y7 = 5, Y8 = 1Here is an example solution: [1:4, 2:2, 3:7, 4:3, 5:6, 6:8, 7:5, 8:1]

Q3 X=3 Y=4 Q4 X=8 Y=4 X Q1 X=1 Y=6Q2 X=5 Y=6 Q6 X=5 Y=2 Q5 X=1 Y=2 Y \= Y1, % different rows (Y1 - Y) \= (X1 - X), (Y1 - Y) \= (X - X1), % different diagonals

To avoid vertical attacks, queens have to be on different columns. We may fix the X coordinates to achieve this. [1:Y1, 2:Y2, 3:Y3, 4:Y4, 5:Y5, 6:Y6, 7:Y7, 8:Y8] Each of the Y’s will be a Y coordinate (a number between 1 to 8). Solution 1 We will put the queens on the board one by one making sure they do not attack one another until we have them all on the board. We may break the problem down to two cases of having an empty list or a list with a head and a tail as we usually do with problems involving lists.

Case 1 There are no queens on the board then the no attack condition holds and we have: solution([ ]). Case 2 first queen other queens There are some queens on the board in which case the list will be [X:Y|Sofar] In such a case, there will be a solution if 1. No attacks between the queens in Sofar 2. X and Y are integers between A queen at square X:Y must not attack any of the queens in the list Sofar solution([ ]). % Nothing to attack! solution([X:Y | Sofar]) :- % Add a new queen solution(Sofar), % Sofar is OK member(Y, [1, 2, 3, 4, 5, 6, 7, 8]), % Generate Y noattack(X:Y, Sofar). % Test

noattack(X:Y, [X1:Y1 | Rest]):- Y =\= Y1, % different rows (Y1 - Y) =\= (X1 - X), (Y1 - Y) =\= (X - X1), % different diagonals noattack(X:Y, Rest). % OK with the others. Now we must define the predicate noattack(Queen, List_of_Queens). This can again be broken into two cases. Case 1 If the List_of_Queens is empty then there are no queens to be attacked: noattack( _, [ ]). Case 2 If the List_of_Queens is not empty then it can be represented as [Queen1 | Rest_of_Queens] and we must check two conditions: 1. The queen at position Queen must not attack the one at position Queen1 2. The queen at position Queen must not attack the ones in the Rest_of_Queens

solution([ ]). % Nothing to attack! solution([X:Y | Sofar]) :- % Add a new queen solution(Sofar), % Sofar is OK member(Y, [1, 2, 3, 4, 5, 6, 7, 8]), % Generate Y (X is known) noattack(X:Y, Sofar). % Test % A solution template template([1:Y1, 2:Y2, 3:Y3, 4:Y4, 5:Y5, 6:Y6, 7:Y7, 8:Y8]) Putting it all together noattack(_, [ ]). noattack(X:Y, [X1:Y1 | Rest]):- Y =\= Y1, % different rows (Y1 - Y) =\= (X1 - X), (Y1 - Y) =\= (X - X1), % different diagonals noattack(X:Y, Rest). % OK with the others.

The query: ?-template(Pos),solution(Pos). generates Y1 = 4, Y2 = 2, Y3 = 7, Y4 = 3, Y5 = 6, Y6 = 8, Y7 = 5, Y8 = 1 as the first solution. There are 92 solutions.