ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat

Similar documents
MAS336 Computational Problem Solving. Problem 3: Eight Queens

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

CSE 573 Problem Set 1. Answers on 10/17/08

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

More Recursion: NQueens

Spring 06 Assignment 2: Constraint Satisfaction Problems


: Principles of Automated Reasoning and Decision Making Midterm

Eight Queens Puzzle Solution Using MATLAB EE2013 Project

mywbut.com Two agent games : alpha beta pruning

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

Second Annual University of Oregon Programming Contest, 1998

THE ASSOCIATION OF MATHEMATICS TEACHERS OF NEW JERSEY 2018 ANNUAL WINTER CONFERENCE FOSTERING GROWTH MINDSETS IN EVERY MATH CLASSROOM

Spring 06 Assignment 2: Constraint Satisfaction Problems

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

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

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

2 Textual Input Language. 1.1 Notation. Project #2 2

CPSC 217 Assignment 3

An Optimal Algorithm for a Strategy Game

Investigation of Algorithmic Solutions of Sudoku Puzzles

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

EXPLAINING THE SHAPE OF RSK

Grade 7/8 Math Circles. Visual Group Theory

Conway s Soldiers. Jasper Taylor

Chapter 5 Backtracking. The Backtracking Technique The n-queens Problem The Sum-of-Subsets Problem Graph Coloring The 0-1 Knapsack Problem

A GRAPH THEORETICAL APPROACH TO SOLVING SCRAMBLE SQUARES PUZZLES. 1. Introduction

An improved strategy for solving Sudoku by sparse optimization methods

Econ 172A - Slides from Lecture 18

Reflections on the N + k Queens Problem

Beyond Prolog: Constraint Logic Programming

Kenken For Teachers. Tom Davis January 8, Abstract

Math236 Discrete Maths with Applications

FREDRIK TUFVESSON ELECTRICAL AND INFORMATION TECHNOLOGY

Algorithmique appliquée Projet UNO

The Mathematics Behind Sudoku Laura Olliverrie Based off research by Bertram Felgenhauer, Ed Russel and Frazer Jarvis. Abstract

MAGIC SQUARES KATIE HAYMAKER

arxiv: v2 [cs.ai] 15 Jul 2016

Graphs of Tilings. Patrick Callahan, University of California Office of the President, Oakland, CA

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

arxiv: v1 [math.co] 24 Oct 2018

INTRODUCTION TO COMPUTER SCIENCE I PROJECT 6 Sudoku! Revision 2 [2010-May-04] 1

Abstract Games Issue 9 Spring 2002

Odd king tours on even chessboards

arxiv: v2 [math.gt] 21 Mar 2018

8. You Won t Want To Play Sudoku Again

Instruction manual Chess Tutor

Dyck paths, standard Young tableaux, and pattern avoiding permutations

1111: Linear Algebra I

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Sudoku Solver Version: 2.5 Due Date: April 5 th 2013

1 Introduction. 2 An Easy Start. KenKen. Charlotte Teachers Institute, 2015

WPF PUZZLE GP 2015 INSTRUCTION BOOKLET ROUND 6. Puzzle authors: Germany Rainer Biegler Gabi Penn-Karras Roland Voigt Ulrich Voigt.

Three of these grids share a property that the other three do not. Can you find such a property? + mod

Some results on Su Doku

Design of Parallel Algorithms. Communication Algorithms

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

Bead Sort: A Natural Sorting Algorithm

LECTURE 7: POLYNOMIAL CONGRUENCES TO PRIME POWER MODULI

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

RGB COLORS. Connecting with Computer Science cs.ubc.ca/~hoos/cpsc101

Arrays. Independent Part. Contents. Programming with Java Module 3. 1 Bowling Introduction Task Intermediate steps...

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #7. Project #1: Checkers, Due: Feb. 19th, 11:59p.m.

Tile Number and Space-Efficient Knot Mosaics

Tic-tac-toe. Lars-Henrik Eriksson. Functional Programming 1. Original presentation by Tjark Weber. Lars-Henrik Eriksson (UU) Tic-tac-toe 1 / 23

CS61B Lecture #22. Today: Backtracking searches, game trees (DSIJ, Section 6.5) Last modified: Mon Oct 17 20:55: CS61B: Lecture #22 1

Eleventh Annual Ohio Wesleyan University Programming Contest April 1, 2017 Rules: 1. There are six questions to be completed in four hours. 2.

ON 4-DIMENSIONAL CUBE AND SUDOKU

CS188: Section Handout 1, Uninformed Search SOLUTIONS

Lecture 6: Latin Squares and the n-queens Problem

ON THE ENUMERATION OF MAGIC CUBES*

Lecture 20: Combinatorial Search (1997) Steven Skiena. skiena

GEOGRAPHY PLAYED ON AN N-CYCLE TIMES A 4-CYCLE

Computing Science (CMPUT) 496

Backtracking. Chapter Introduction

Techniques for Generating Sudoku Instances

a b c d e f g h i j k l m n

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

#A13 INTEGERS 15 (2015) THE LOCATION OF THE FIRST ASCENT IN A 123-AVOIDING PERMUTATION

KenKen Strategies. Solution: To answer this, build the 6 6 table of values of the form ab 2 with a {1, 2, 3, 4, 5, 6}

The 2013 British Informatics Olympiad

Which Rectangular Chessboards Have a Bishop s Tour?

Two Parity Puzzles Related to Generalized Space-Filling Peano Curve Constructions and Some Beautiful Silk Scarves

CALCULATING SQUARE ROOTS BY HAND By James D. Nickel

Informatics 2D: Tutorial 1 (Solutions)

Final Project: Verify a Sudoku Solution Due Fri Apr 29 (2400 hrs)? Wed May 4 (1200 hrs)? 1

Wythoff s Game. Kimberly Hirschfeld-Cotton Oshkosh, Nebraska

Permutation Tableaux and the Dashed Permutation Pattern 32 1

WPF PUZZLE GP 2018 ROUND 7 INSTRUCTION BOOKLET. Host Country: Netherlands. Bram de Laat. Special Notes: None.

Question Score Max Cover Total 149

RUBIK S CUBE SOLUTION

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

Symmetric Permutations Avoiding Two Patterns

Universiteit Leiden Opleiding Informatica

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

Introduction to Spring 2009 Artificial Intelligence Final Exam

C SC 483 Chess and AI: Computation and Cognition. Lecture 3 September 10th

WPF PUZZLE GP 2016 ROUND 8 INSTRUCTION BOOKLET. Host Country: Russia. Andrey Bogdanov. Special Notes: No special notes for this round.

Grade 7/8 Math Circles. Visual Group Theory

Automatically Generating Puzzle Problems with Varying Complexity

Transcription:

ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat Overview The goal of this assignment is to find solutions for the 8-queen puzzle/problem. The goal is to place on a 8x8 chess board 8 queens, in a way that none of the queens is able to Threaten any other queen using the standard chess moves for a queen (row-wise, column-wise, and diagonally). 8-queen Solution(s) and Recursion There are 92 possible solutions for this puzzle. However not all of them are unique. Some of the solutions are identical after rotating or mirroring the board. If we count these solutions as one, there are 12 unique solutions for the problem. Suppose initially we have the board all cleared up. Each queen is taking one column namely A, B,, H. We adopt the algebraic chess notation to denote the board position shown on figure 1. Figure 1 We place exactly one queen at each column. A convenient way to reference the queens is Q1, Q2,, Q8, which simply denote the queen positioned at the 1st, 2nd,, 8 th column. The algorithm for finding a solution can be described as following:

1. We can place Q1 queen on any row in the first column. Let s choose position A1. 2. The second queen goes on the second column. Starting from the bottom up we notice there will be a threat on B1 and B2. Therefore, the first available position is B3. Now we have to restrict the remaining 6 queens from being placed on rows 1 and 3, and diagonals A1-H8 and A2-G8. The board after placing the second queen is shown n figure 2. The red positions are the restricted ones. Figure 2 3. The positioning of the remaining queens can take place following similar considerations. However after placing Q7 at G6 we will end up with the following situation graphically shown in figure 3. There will be a threat to the Q8 if it is placed at any of the positions on the column 8 (figure 3). Figure 3 4. Luckily we have the opportunity to revise the position of already placed queens in order to find an acceptable position for Q8. We start

backtracking from the last placed queen, Q7. The remaining positions G7 and G8 will not be safe for Q7, therefore we must proceed by relocating Q6 from its current position occupying F4. Once we find a new safe position for a queen, we can continue to the next column, and look for a different solution starting from the next column on just as we did when we started to solve the problem. If you proceed in this manner, the first occurrence of a solution will be one corresponding to the board shown in figure 4. Figure 4 As you see, in this case, it was necessary to backtrack all the way to the second column and relocate Q2 from its initially assigned position (you may try this). 5. Once a solution is reached, the result can be printed on the screen. Then we can search for additional solutions. As an idea how to do this is the following. Since Q8 is the last positioned queen we go ahead and consider other available positions for it, which are H4-H8 in figure 4. In case of threats we backtrack in the usual way as described before. In developing a program to solve the 8-queens problem, the board can be encoded in many ways. A convenient way is to represent the positions of the queens on the board with a one dimensional array. For example the solution in figure 4 can be represented as: 1 7 4 6 8 2 5 3

The Programming Assignment and Requirements The assignment is to write a recursive program that implements backtracking to solve the 8 queen puzzle. The accepted solutions are board arrangements, where there are no threats between the queens. Requirement 1: User input and Output Your code has to allow user input. The input allows user to specify the positions for the first n queens, where 0<n<8, in the format described in the previous section. If the user specifies the positions of interest for the first n>0 queens, your program should start looking for solutions starting from the column n+1. If there is already a threat between the queens with the input supplied by the user, the program reports that a solution is impossible that includes the specified input. Otherwise, the program look for possible solutions starting by placing the queens in the n+1 and subsequent columns and report all solutions (if any) which match the user s specification. For example, to indicate that we want solutions, in which the first three columns are taken by the queens placed on the second, fourth, and sixth rows, the user will do the following: >Input the initial state: 2 4 6 0 0 0 0 0 In this case, your program shall find all solutions (if any) for the remaining five columns. Hence the positions specified with zero 0 in the user input denote the empty columns. For output, your program should print all possible solutions according to the format shown below: 2 4 6 8 3 1 7 5 2 4 7 1........ 2 4 8............

Requirement 2: Functions Your program must implement the two functions described below. 1. Function nextcolumn This function should accept an array input, corresponding to a board that has valid positions for the first n queens on the first n columns, and it returns an integer for the first valid position (if any) for the queen on the next column, n+1 (starting from row 1 and ending with row 8). By valid we mean that no threats exist between the queens. If none can be found this function returns 0. As an example, consider the board in figure 5: 8 7 6 5 4 3 2 1 A B C D E F G H Figure 5 This board can be specified as b=[1 3 0 0 0 0 0 0]. Since the first zero in the array b appears at index 2 of the array, i.e. the 3rd column of the board (array indices start from 0 in C). If the function nextcolumn(b) is called, it will return 5 since this is the first position starting from row 1 where a queen can be placed without any threats from the other queens. 2. Function nextposition Now assume we are not interested in 5 th row position for to be occupied by our 3 rd queen. If the function nextposition(b) is called with the value of b=[1 3 5 0 0 0 0 0] it should find (if any) and report the next possible (valid) row for the last queen in its column (in this example the 3 rd

col.). If no more valid position can be found for the last queen in its column, this function returns 0 (recall that the order is from rows 1 through 8 ascending). Therefore, in the current example, the call nextposition(b) returns the integer value 6. This means that the 6 th row is the next valid position for the 3rd queen. This function accepts an array input, corresponding to a board with valid positions for the first n queens on the first n columns, and it returns an integer. Extra Credit (25 points) Not all valid solutions to the 8-queen problem are unique. Some are reflections or rotations of other solutions. For example all the solutions shown in figure 6 are not unique. The second solution is a reflection and the third solution (from left) is a rotation of the first solution. In order to receive the extra credit given here, implement the function isunique and use it in your program to only report unique solutions for the given 8-queen problem. The function isunique accepts two arrays b1 and b2 corresponding to two board configurations and returns 1 if they are unique and 0 if they are not. Therefore: isunique ([2,4,6,8,3,1,7,5],[5,7,1,3,8,6,4,1]) = 0 Figure 6 Your program implementing the extra credit reports unique solutions of the given problem similar to the following format. US 2 4 6 8 3 1 7 5

US 1 3 5............