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

Similar documents

Eight Queens Puzzle Solution Using MATLAB EE2013 Project

Homework Assignment #1

Structured Programming Using Procedural Languages INSS Spring 2018

More Recursion: NQueens

CS 540-2: Introduction to Artificial Intelligence Homework Assignment #2. Assigned: Monday, February 6 Due: Saturday, February 18

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

The 8-queens problem

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

MAS336 Computational Problem Solving. Problem 3: Eight Queens

CS256 Applied Theory of Computation

After learning the Rules, What should beginners learn next?

ENGR170 Assignment Problem Solving with Recursion Dr Michael M. Marefat

CS/COE 1501

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

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

CMPT 310 Assignment 1

Movement of the pieces

The Classification of Quadratic Rook Polynomials of a Generalized Three Dimensional Board

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

Homework Assignment #2

CPSC 217 Assignment 3

Which Rectangular Chessboards Have a Bishop s Tour?

CS61B, Fall 2014 Project #2: Jumping Cubes(version 3) P. N. Hilfinger

A1 Problem Statement Unit Pricing

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

Cardinality. Hebrew alphabet). We write S = ℵ 0 and say that S has cardinality aleph null.

Spring 06 Assignment 2: Constraint Satisfaction Problems

Counting Things Solutions

Spring 06 Assignment 2: Constraint Satisfaction Problems

A CLASSIFICATION OF QUADRATIC ROOK POLYNOMIALS

MA/CSSE 473 Day 13. Student Questions. Permutation Generation. HW 6 due Monday, HW 7 next Thursday, Tuesday s exam. Permutation generation

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

G53CLP Constraint Logic Programming

Counting Things. Tom Davis March 17, 2006

1 Modified Othello. Assignment 2. Total marks: 100. Out: February 10 Due: March 5 at 14:30

Lab 1. CS 5233 Fall 2007 assigned August 22, 2007 Tom Bylander, Instructor due midnight, Sept. 26, 2007

Battlehack: Voyage Official Game Specs

arxiv: v2 [cs.cc] 20 Nov 2018

Microchess 2.0 gives you a unique and exciting way to use your Apple II to enjoy the intellectually stimulating game of chess. The complete program lo

You ve seen them played in coffee shops, on planes, and

Backtracking. Chapter Introduction

Lecture 2: Sum rule, partition method, difference method, bijection method, product rules

In order for metogivebackyour midterms, please form. a line and sort yourselves in alphabetical order, from A

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

UKPA Presents. March 12 13, 2011 INSTRUCTION BOOKLET.

MA/CSSE 473 Day 14. Permutations wrap-up. Subset generation. (Horner s method) Permutations wrap up Generating subsets of a set

WPF PUZZLE GP 2018 ROUND 2 INSTRUCTION BOOKLET. Host Country: Switzerland. Markus Roth, Roger Kohler, Esther Naef

Computer Science COMP-250 Homework #4 v4.0 Due Friday April 1 st, 2016

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

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

Pointers. The Rectangle Game. Robb T. Koether. Hampden-Sydney College. Mon, Jan 21, 2013

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

CS188: Section Handout 1, Uninformed Search SOLUTIONS

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

Prolog - 3. Prolog Nomenclature

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

OCTAGON 5 IN 1 GAME SET

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

Sample Spaces, Events, Probability

DELUXE 3 IN 1 GAME SET

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

arxiv: v1 [math.co] 24 Oct 2018

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

Grade 7/8 Math Circles. Visual Group Theory

Equivalence classes of length-changing replacements of size-3 patterns

Chess Handbook: Course One

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

WPF PUZZLE GP 2018 ROUND 2. COMPETITION BOOKLET Host Country: Switzerland. ScHWeIZ. ScHWeiz. schweiz. SchWEIZ. SchwEiz. SchWEiZ. schweiz.

Second Annual University of Oregon Programming Contest, 1998

A complete set of dominoes containing the numbers 0, 1, 2, 3, 4, 5 and 6, part of which is shown, has a total of 28 dominoes.

Unit 12: Artificial Intelligence CS 101, Fall 2018

Senior Math Circles February 10, 2010 Game Theory II

Lecture 6: Latin Squares and the n-queens Problem

LESSON 2: THE INCLUSION-EXCLUSION PRINCIPLE

a b c d e f g h 1 a b c d e f g h C A B B A C C X X C C X X C C A B B A C Diagram 1-2 Square names

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

Lab 1. Due: Friday, September 16th at 9:00 AM

arxiv: v1 [cs.cc] 21 Jun 2017

CMPUT 657: Heuristic Search

Final Practice Problems: Dynamic Programming and Max Flow Problems (I) Dynamic Programming Practice Problems

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

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)?

Homework 7: Subsets Due: 10:00 PM, Oct 24, 2017

Reflections on the N + k Queens Problem

Cryptic Crosswords for Bright Sparks

Latin squares and related combinatorial designs. Leonard Soicher Queen Mary, University of London July 2013

Part I At the top level, you will work with partial solutions (referred to as states) and state sets (referred to as State-Sets), where a partial solu

Problem 1: Booklet Printing

Math Circle Beginners Group May 22, 2016 Combinatorics

Tutorial: Constraint-Based Local Search

Quintessence A Packet of Puzzles by John Bulten

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

Week 3-4: Permutations and Combinations

MATHEMATICS ON THE CHESSBOARD

Tac Due: Sep. 26, 2012

Recursive Triangle Puzzle

CMPT 310 Assignment 1

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

8. You Won t Want To Play Sudoku Again

Transcription:

CMPS 12A Introduction to Programming Winter 2013 Programming Assignment 5 In this assignment you will write a java program finds all solutions to the n-queens problem, for 1 n 13. Begin by reading the Wikipedia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/eight_queens_puzzle In the game of Chess a queen can move any number of spaces in any linear direction: horizontally, vertically, or along a diagonal. The Eight Queens puzzle is to find a way to place 8 queens on an otherwise empty 8 8 chessboard in such a way that no two queens confront each other. One solution to this problem is pictured below. The n-queens problem is the natural generalization of placing n queens on an n n chessboard so that no two queens confront one another. There are many ways of solving this problem. Our approach will be to start with a solution to the n-rooks problem (i.e. place n Rooks on an n n chessboard so that no two rooks attack each other) then check if that arrangement is also a solution to n-queens. The rook move in chess is similar to the queen's move except that it cannot move diagonally. 1

Solutions to the n-rooks problem are easy to find since one need only position the rooks so that no two are on the same row and no two are on the same column. Since there are n rows and n columns to choose from, solutions abound. A natural way to encode solutions to n-rooks is as permutations of the integers {1, 2, 3,, n}. A permutation of a set is an ordered arrangement of the elements in that set. If we number the rows and columns of the n n chessboard using the integers 1 through n, each square is then labeled by a unique pair of coordinates (i, j) indicating the square in row i and column j. The permutation a, a, a,, a ) ( 1 2 3 n corresponds to the placement of a piece on the square with coordinates ( a j, j) for 1 j n. For instance, the permutations ( 2, 7, 8, 5,1, 4, 6, 3) and ( 2, 4, 6, 8, 3,1, 7, 5) correspond to two the 8-Rooks solutions pictured below. 8 R 7 R 6 R 5 R 4 R 3 R 2 R 1 R 8 R 7 R 6 R 5 R 4 R 3 R 2 R 1 R Observe that the solution on the right is also a solution to 8-Queens, while the one on the left is not since certain pairs of pieces lie on the same diagonal. In fact the solution on the right is the 8-Queens solution pictured on the previous page. In general, any solution to the n-queens problem is also a solution to n- Rooks. The converse is false however. Not every solution to n-rooks is a solution to n-queens. Your program will generate all solutions to n-rooks by systematically producing all permutations of the set {1, 2, 3,, n}. It will check each n-rooks solution for diagonal attacks to see if the given permutation also solves n-queens. Whenever an n-queens solution is found, your program will print out 2

the permutation, then move on to the next permutation. Thus ( 2, 4, 6, 8, 3,1, 7, 5) would be printed while ( 2, 7, 8, 5,1, 4, 6, 3) would not. Two major problems must therefore be solved to complete the project: (1) how can you produce all permutations of the set {1, 2, 3,, n}, and (2) given one such permutation how can you determine if two pieces lie on the same diagonal. Permutations There are n! permutations of a finite set containing n elements. To see this observe that there are n ways to choose the first element in the arrangement, n 1 ways to choose the second element, n 2 ways to th th choose the third,, 2 ways to choose the ( n 1) element, and finally 1 way to choose the n and last element in the ordered arrangement. The number of ways of making all these choices in succession is therefore n( n 1)( n 2) 3 2 1 n!. For instance there are 3! 6 permutations of the set {1, 2, 3}: 123, 132, 213, 231, 312, 321. The permutations of a finite set {1, 2, 3,, n} have a natural ordering called the lexicographic order, also known as alphabetical order. The 4! 24 permutations of {1, 2, 3, 4} are listed in order as follows. 1234 2134 3124 4123 1243 2143 3142 4132 1324 2314 3214 4213 1342 2341 3241 4231 1423 2413 3412 4312 1432 2431 3421 4321 As an exercise list the 5! 120 permutations of {1, 2, 3, 4, 5} in lexicographic order. After finishing this exercise, you will see the need for an algorithm that will systematically produce all permutations of a finite set. We will represent a permutation a, a, a,, a ) by an array A [ ] of length n 1, where a j ( 1 2 3 n A[ j] for 1 j n, and the element A [0] is simply not used. Your program will include a function with the following heading. static void nextpermutation(int[] A){...} This method will alter its argument A by advancing ( A[1], ) to the next permutation in the lexicographic ordering. If ( A[1], ) is already at the end of the sequence, the function will reset A to the initial permutation ( 1, 2, 3,, n) in the lexicographic order. The pseudocode below gives an outline for the body of nextpermutation(). scan the array from right-to-left if the current element is less than its right-hand neighbor call the current element the pivot stop scanning if the left end was reached without finding a pivot reverse the array (permutation was lexicographically last, so start over) return scan the array from right-to-left again if the current element is larger than the pivot call the current element the successor stop scanning swap the pivot and the successor reverse the portion of the array to the right of where the pivot was found return 3

Run the above procedure by hand on the initial permutation (1, 2, 3, 4) and see that it does indeed produce all 24 permutations of the set {1, 2, 3, 4} in lexicographic order. Finding Diagonal Attacks Your program will also include another function with the following heading. static boolean issolution(int[] A){...} This method will return true if the permutation represented by ( A[1], ) places no two pieces on the same diagonal, and will return false otherwise. To check if two queens at ( A [ i], i ) and ( A [ j], j ) lie on the same diagonal, it is sufficient to check whether their horizontal distance apart is the same as their vertical distance apart, as illustrated in the diagram below. horizontal distance 6 2 4 8 7 Q 6 5 4 3 Q 2 1 vertical distance 7 3 4 Function issolution() should compare each pair of queens at most once. If a pair is found on the same diagonal, do no further comparisons and return false. If all n ( n 1) / 2 comparisons are performed without finding a diagonal attack, return true. Program Operation Your program for this project will be called Queens.java. You will include a Makefile with your that creates an executable Jar file called Queens, allowing one to run the program by typing Queens at the command line. Your program will read an integer n from the command line indicating the size of the Queens problem to solve. The program will operate in two modes: normal and verbose, indicated by the command line option "-v". In normal mode, the program prints only the number of solutions to n-queens. In verbose mode, all permutations representing solutions to n-queens will be printed in lexicographic order, followed by the number of such solutions. Thus to find the number of solutions to 8-Queens you will type: % Queens 8 To print all 92 unique solutions to 8-Queens type: % Queens v 8 4

If the user types anything on the command line other than the option v and a number n, the program will print a usage message to stderr and quit. A sample session is included below. % Queens Usage: Queens [-v] number Option: -v verbose output, print all solutions % Queens x Usage: Queens [-v] number Option: -v verbose output, print all solutions % Queens 5 5-Queens has 10 solutions % Queens -v 5 (1, 3, 5, 2, 4) (1, 4, 2, 5, 3) (2, 4, 1, 3, 5) (2, 5, 3, 1, 4) (3, 1, 4, 2, 5) (3, 5, 2, 4, 1) (4, 1, 3, 5, 2) (4, 2, 5, 3, 1) (5, 2, 4, 1, 3) (5, 3, 1, 4, 2) 5-Queens has 10 solutions % Queens -v 6 (2, 4, 6, 1, 3, 5) (3, 6, 2, 5, 1, 4) (4, 1, 5, 2, 6, 3) (5, 3, 1, 6, 4, 2) 6-Queens has 4 solutions % Queens -v 4 (2, 4, 1, 3) (3, 1, 4, 2) 4-Queens has 2 solutions % Queens -v 3 3-Queens has 0 solutions % It is recommended that you write helper functions to perform basic subtasks such as: print the usage message and quit, calculate the factorial of n, print out a formatted array as above, swap two elements in an array, and reverse the elements in a subarray. Some of these methods have already been posted on the website as examples. Note that there are much more efficient procedures for solving this problem that use programming methods outside the scope of this course. Even if you know how to use such methods, you are required to solve the problem as indicated in this project description. Your program should work very quickly on problem sizes up to 12 or 13. Beyond that you should expect that the program will slow down considerably. What to turn in Write a Makefile for this project that creates an executable Jar file called Queens. Include both clean and submit targets. Submit the files Makefile and Queens.java to the assignment name pa5. As always start early and ask questions of myself, the TAs and on Piazza. 5