Sit-In Lab 1 Ob-CHESS-ion

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Identity and Inverse Matrices
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.
PROLOG 8 QUEENS PROBLEM.
Computers playing games. One-player games Puzzle: Place 8 queens on a chess board so that no two queens attack each other (i.e. on the same row, same.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
The N-Queens Problem lab01.
Chapter 7 Multidimensional Arrays. Defining a two dimensional array elementType[][] arrayName; // Java pro elementType arrayName[][]; // C++ alternate.
CS 61B Data Structures and Programming Methodology July 31, 2008 David Sun.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
Search Problems Russell and Norvig: Chapter 3, Sections 3.1 – 3.3 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2003/home.htm by Prof. Jean-Claude.
CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: CSE 221/ICT221 Analysis and Design of Algorithms Lecture 06: Analysis of Algorithm using List,
CS1020E Sitin 1 Discussion -- Counting Palindromes.
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.
Recursion Chapter 5.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API.
Announcements.
Randomized Algorithms Pasi Fränti Treasure island Treasure worth awaits 5000 DAA expedition 5000 ? ? Map for sale: 3000.
Recursion: Backtracking
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 ( 李德成 )
Two Dimensional Arrays
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.
Sudoku Jordi Cortadella Department of Computer Science.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
The 8-queens problem CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before you delete this.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Backtracking & Brute Force Optimization Intro2CS – weeks
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
Checkers A Matlab Project by Spenser Davison, Edward Dyakiw, Justin Pezzi, and Scott Wu.
Chapter 1 Section 1.6 Algebraic Properties of Matrix Operations.
Testing i. explain the importance of system testing and installation planning;
Sit-In Lab 1 Twenty-Forty-Eight
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
12-4: Matrix Methods for Square Systems
Problem Solving: Brute Force Approaches
CS1020 – Data Structures And Algorithms 1 AY Semester 2
1.5 Matricies.
CS100J Lecture 19 Previous Lecture This Lecture
Intro to Computer Science II
CSCI 104 Backtracking Search
CS 270 Math Foundations of CS
Data Structures and Algorithms
Algorithm Design and Analysis (ADA)
Problem Solving: Brute Force Approaches
Chapter 12 Recursion (methods calling themselves)
Python I/O.
Factoring Quadratics December 1st, 2016.
Applying Pascal’s Triangle
Jordi Cortadella Department of Computer Science
Number Theory.
Number Theory.
Determinants.
Objective: Today we will investigate the ‘magic’ in magic squares.
Magic Squares   10   X.
adapted from Recursive Backtracking by Mike Scott, UT Austin
Ch. 6 Recursion as a Problem Solving Technique
Lecture 13: Two-Dimensional Arrays
The N-queens problem Team: 404 brain not found
CS Software Studio Assignment 1
CSE 143 Lecture 18 More Recursive Backtracking
© T Madas.
CSE 143 Lecture 18 More Recursive Backtracking
DRILL.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

Sit-In Lab 1 Ob-CHESS-ion

N-Queens Puzzle Checker Famous N-Queens puzzle You want to check if the input solution is correct No 2 queens can be on the same row, column or diagonal Write a piece of code to verify the solution Input: N – Number of rows ( Number of queens on chessboard ) Followed by N rows of N numbers 0 – Empty space 1 – Queen Output whether the puzzle is a valid solution

Example – Valid? Output :

Example – Incorrect solution Output : INVALID

Example – Valid Solution Output : VALID

No other queen in same row, column and diagonal

Algorithm Loop through each square of the array: Start from the top left, iterate row by row For each queen found Starting from the square on its right, check if there exists another queen in the same row Starting from the square right below it, check if there exists another queen in the same column Starting from the square on its bottom right, check if there exists another queen in the same right diagonal Starting from the square on its bottom left, check if there exists another queen in the same left diagonal Return false if there exists another queen

Visualisation – Start from top left

Visualisation – Queen is found, check rows and diagonals

Visualisation – Queen is found, check rows and diagonals Valid

Visualisation – Iterate row by row - No queen in this space

Visualisation – Another Queen is found

Visualisation – Queen is found, check rows and diagonals Valid

Visualisation – Iterate row by row

Visualisation – Another Queen is found

Visualisation – Queen is found, check rows and diagonals Invalid

Visualisation – Break from loop and return false Invalid

Main method: Helper methods: Read in the inputs Call helper methods and print the output Helper methods: Check rows, columns and diagonals ( 4 directions ), returning false if another queen is found Check if given coordinates are out of array bounds ( >= N )

How to check diagonals?

0 1 2 3 4 5 6 7 1 2 [1][3] 3 4 5 6 7

0 1 2 3 4 5 6 7 1 2 [2][4] 3 4 5 6 7

0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 [3][5]

0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 [3][5] Row++ Col++

0 1 2 3 4 5 6 7 1 2 [1][3] 3 4 5 6 7

0 1 2 3 4 5 6 7 1 2 [2][2] 3 4 5 6 7

0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 [3][1]

0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 [3][1] Row++ Col--

Questions?