Eight Queens Puzzle Solution Using MATLAB EE2013 Project

Similar documents
In the game of Chess a queen can move any number of spaces in any linear direction: horizontally, vertically, or along a diagonal.


ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat

More Recursion: NQueens

If a pawn is still on its original square, it can move two squares or one square ahead. Pawn Movement

YourTurnMyTurn.com: chess rules. Jan Willem Schoonhoven Copyright 2018 YourTurnMyTurn.com

Once you get a solution draw it below, showing which three pennies you moved and where you moved them to. My Solution:

Spring 06 Assignment 2: Constraint Satisfaction Problems

Reflections on the N + k Queens Problem

Topic 10 Recursive Backtracking

The 8-queens problem

8. You Won t Want To Play Sudoku Again

G53CLP Constraint Logic Programming

Using a Stack. Data Structures and Other Objects Using C++

COMP9414: Artificial Intelligence Problem Solving and Search

Chained Permutations. Dylan Heuer. North Dakota State University. July 26, 2018

Spring 06 Assignment 2: Constraint Satisfaction Problems

CS/COE 1501

After learning the Rules, What should beginners learn next?

Solution Algorithm to the Sam Loyd (n 2 1) Puzzle

Lecture 2: Problem Formulation

MAS336 Computational Problem Solving. Problem 3: Eight Queens

An improved strategy for solving Sudoku by sparse optimization methods

Solving the N-Queens Problem with Local Search

UKPA Presents. March 12 13, 2011 INSTRUCTION BOOKLET.

CPSC 217 Assignment 3 Due Date: Friday March 30, 2018 at 11:59pm

Chess Puzzle Mate in N-Moves Solver with Branch and Bound Algorithm

CS188: Section Handout 1, Uninformed Search SOLUTIONS

COMBINATORIAL GAMES: MODULAR N-QUEEN

ChesServe Test Plan. ChesServe CS 451 Allan Caffee Charles Conroy Kyle Golrick Christopher Gore David Kerkeslager

Homework 9: Software Design Considerations

######################################################################

Separation Numbers of Chessboard Graphs. Doug Chatham Morehead State University September 29, 2006

Overview. Algorithms: Simon Weber CSC173 Scheme Week 3-4 N-Queens Problem in Scheme

Chess and Python revisited

Finding and Characterizing Knights Tours on the Chessboard

Chapter 4 Heuristics & Local Search

Game, Set, and Match Carl W. Lee September 2016

Canadian Mathematics Competition An activity of The Centre for Education in Mathematics and Computing, University of Waterloo, Waterloo, Ontario

Notice: Individual students, nonprofit libraries, or schools are permitted to make fair use of the papers and its solutions.

Describes the operation of multiplying by ten as adding a nought

CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms. Nicki Dell Spring 2014

Raf. J. of Comp. & Math s., Vol. 9, No. 2, 2012

COCI 2008/2009 Contest #3, 13 th December 2008 TASK PET KEMIJA CROSS MATRICA BST NAJKRACI

Tutorial: Constraint-Based Local Search

Melon s Puzzle Packs

CSE548, AMS542: Analysis of Algorithms, Fall 2016 Date: Sep 25. Homework #1. ( Due: Oct 10 ) Figure 1: The laser game.

Using a Stack. The N-Queens Problem. The N-Queens Problem. The N-Queens Problem. The N-Queens Problem. The N-Queens Problem

Backtracking. Chapter Introduction

Primo Plaid Flannels: New Blues

DELUXE 3 IN 1 GAME SET

Lecture 6: Latin Squares and the n-queens Problem

CSE 231 Fall 2012 Programming Project 8

Local search algorithms

Problem Solving Methods

All activity guides can be found online. Helping Teachers Make A Difference

Search then involves moving from state-to-state in the problem space to find a goal (or to terminate without finding a goal).

SUDOKU1 Challenge 2013 TWINS MADNESS

CMSC 201 Fall 2018 Project 3 Sudoku

CS431 homework 2. 8 June Question 1 (page 54, problem 2.3). Is lg n = O(n)? Is lg n = Ω(n)? Is lg n = Θ(n)?

Examination paper for TDT4120 Algorithms and Data Structures

Assignment 6 Play A Game: Minesweeper or Battleship!!! Due: Sunday, December 3rd, :59pm

Text Extraction from Images

UN DOS TREZ Sudoku Competition. Puzzle Booklet for Preliminary Round. 19-Feb :45PM 75 minutes

Scrabble Board Automatic Detector for Third Party Applications

the alien has the option of asserting that all 100 aliens have been to the living room by now. If this assertion is false, all 100 aliens are thrown

Perfect Domination for Bishops, Kings and Rooks Graphs On Square Chessboard

Lecture 1, CS 2050, Intro Discrete Math for Computer Science

Logic Masters India Presents

Arithmetic, bones and counting

Junior Circle Games with coins and chessboards

Take Control of Sudoku

Frontier/Modern Wargames Rules

Twenty-fourth Annual UNC Math Contest Final Round Solutions Jan 2016 [(3!)!] 4

Computing Science (CMPUT) 496

Solving Problems by Searching

Nurikabe puzzle. Zhen Zuo

1 Introduction The n-queens problem is a classical combinatorial problem in the AI search area. We are particularly interested in the n-queens problem

Object Recognition System using Template Matching Based on Signature and Principal Component Analysis

This chapter gives you everything you

arxiv: v1 [math.co] 24 Nov 2018

The game of Paco Ŝako

If a word starts with a vowel, add yay on to the end of the word, e.g. engineering becomes engineeringyay

28,800 Extremely Magic 5 5 Squares Arthur Holshouser. Harold Reiter.

Unit. The double attack. Types of double attack. With which pieces? Notes and observations

Solutions for CAT 2017 DILR - Afternoon (Slot-2)

13 TH 24 HOURS PUZZLE CHAMPIONSHIP

Comparing Methods for Solving Kuromasu Puzzles

Python for education: the exact cover problem

A1 Problem Statement Unit Pricing

Adversary Search. Ref: Chapter 5

N-Queens Problem. Latin Squares Duncan Prince, Tamara Gomez February

An Exploration of the Minimum Clue Sudoku Problem

Overview PROBLEM SOLVING AGENTS. Problem Solving Agents

Coin Cappers. Tic Tac Toe

Fun and Games on a Chess Board

Algorithm Performance For Chessboard Separation Problems

MODULE: DESIGNING AND DEVELOPING OBJECT-ORIENTED COMPUTER PROGRAMS ASSIGNMENT TITLE: WORDSEARCH MARCH 2014

OCTAGON 5 IN 1 GAME SET

Project Connect Four (Version 1.1)

Transcription:

Eight Queens Puzzle Solution Using MATLAB EE2013 Project Matric No: U066584J January 20, 2010 1 Introduction Figure 1: One of the Solution for Eight Queens Puzzle The eight queens puzzle is the problem of putting eight chess queens on an 8 8 chessboard such that none of them is able to capture any other using the standard chess queen s moves. The queens must be placed in such a way that no two queens would be able to attack each other. Thus, a solution requires that no two queens share the same row, column, or diagonal. Nowadays, the eight queens puzzle can be solved easily and almost instantly with the help of a computer. Various programs have been created to solve this puzzle, some with even more queens at a larger board. In this project, I will solve the eight queens puzzle using MATLAB programming language, and further improve the program to tackle puzzles with more or lesser queens. 1

2 Algorithm To solve this puzzle in a systematic way, I have divided this problem into a few stages: Initialisation, Update, Checking, and Printing Stages. Initialisation Stage Matrices to be used will be initialised at this stage. Size of the matrices depend on the user input, corresponding to number of queens in the queens puzzle. Update and Checking Stages The chess board is updated with the placement of queen at the available empty space at each iteration. Then checking will be done to validate the previous placement. If the placement is valid, the process will be continued with the Update and Checking Stages until all eight queens are filled. If the placement is void, it will roll back to the previous move and search for the next available empty space to place the queen. Printing Stage When all eight queens are placed correctly on the board, the final matrix will be printed out. The idea was translated into the following flow chart to design the algorithm to solve this problem. Figure 2: Flow Chart of the Program 2

3 Programming In this section, I will comment about the actual coding of the program. First, I have created two subfunctions to be used in the main program function. 3.1 Subfunction queensetupboard.m Calling this subfunction creates a size by size matrix to represent the actual chess board. A matrix C of the same size will also be created to perform updates and checking of the available space. Lastly, a three dimensional matrix is created to store the last value of the matrix C at each column loop. It is used for rolling back of previous matrix C if they are needed. 3.2 Subfunction queenupdate.m This subfunction input the operation matrix together with the coordinate of the new queen. It will return an updated operation matrix with all the non available spaces marked as 1 while the rest 0. It also return a non-updated operation matrix with the coordinate of queen placed marked as 2 for roll back purposes. It will replace the updated matrix in the main function if the current queen placement is found to be invalid later. Technically, the non available spaces to be marked would be the row, column, and the diagonal of the given coordinate. Marking of row and column of matrix can be done easily using the function matrix A(row,:)=1 and matrix A(:,column)=1. Diagonal of (row, column) would be at all the coordinates of (row ± 1, column ± 1) as shown in the codes. 3.3 Main Function queenpuzzle.m An input number from users will be used to determine the number of queens and also the size of the chess board. Initially, the program will generate a few matrices as explained in the first subfunction above. In the computational part, a queen will be placed at the available space of the current column (column starts counting from 1) of the matrix. If there is no available space left, the matrix will be rolled back to the previous matrix and it will search for new available space at the previous column. Or else if the queen successfully placed, the matrix will be updated to marked out all the non available spaces (row, column and diagonal of queen). The loop will be ended when the column number equal to the size of the matrix. By then, a solution should be obtained. For the case that there is no solution for the puzzle, an empty matrix will be returned. Finally, the matrix representing the chess board will be updated according to the solution, with 1 represent queen, and 0 represent empty space on the board. 3

4 Source Codes 4.1 queensetupboard.m 4.2 queenupdate.m 4

4.3 queenpuzzle.m 5

5 Results The result of the puzzle can be obtained by entering queenpuzzle(size) in MATLAB Command Window where size is the number of queen desired. Results for eight, nine, and ten queens puzzle are shown below. >> queenpuzzle(8) ans = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 >> queenpuzzle(9) ans = 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 >> queenpuzzle(10) ans = 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 6