Intro to Computer Science II

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

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.
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
Tic Tac Toe Architecture CSE 5290 – Artificial Intelligence 06/13/2011 Christopher Hepler.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CompSci Recursion & Minimax Playing Against the Computer Recursion & the Minimax Algorithm Key to Acing Computer Science If you understand everything,
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
November 10, 2009Introduction to Cognitive Science Lecture 17: Game-Playing Algorithms 1 Decision Trees Many classes of problems can be formalized as search.
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
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.
Announcements.
COSC2007 Data Structures II
Recursion: Backtracking
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
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 ( 李德成 )
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
Two Dimensional Arrays
Announcements This Wednesday, Class and Labs are cancelled! The last lab is due this Wednesday … how many people are planning on doing it? Finally posted.
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
Chess By Kezia Farley.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
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"?
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Introduction to State Space Search
CSE 143 Lecture 13 Recursive Backtracking slides created by Ethan Apter
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Various Problem Solving Approaches. Problem solving by analogy Very often problems can be solved by looking at similar problems. For example, consider.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Game playing Types of games Deterministic vs. chance
Backtracking, Search, Heuristics
Using a Stack Chapter 6 introduces the stack data type.
Chapter 5 Recursion as a Problem-Solving Technique
CSCI 104 Backtracking Search
Data Structures and Algorithms
Adversarial Search.
Fundamentals of Programming II Backtracking with Stacks
Sit-In Lab 1 Ob-CHESS-ion
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"?
Hubert Chan (Chapters 1.6, 1.7, 4.1)
CSE 143 Lecture 19 More Recursive Backtracking
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
Using a Stack Chapter 6 introduces the stack data type.
Recursion Copyright (c) Pearson All rights reserved.
Using a Stack Chapter 6 introduces the stack data type.
NIM - a two person game n objects are in one pile
adapted from Recursive Backtracking by Mike Scott, UT Austin
Ch. 6 Recursion as a Problem Solving Technique
Using a Stack Chapter 6 introduces the stack data type.
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
Using a Stack Chapter 6 introduces the stack data type.
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.
Exercise: Dice roll sum
Tic Tac Toe application
CSE 143 Lecture 18 More Recursive Backtracking
CSE 143 Lecture 18 More Recursive Backtracking
Exercise: Dice roll sum
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"?
Backtracking, Search, Heuristics
Tic-Tac-Toe Game Engine
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CS51A David Kauchak Spring 2019
Backtracking, Search, Heuristics
2019 SAIMC Puzzle Challenge General Regulations
Unit II Game Playing.
Presentation transcript:

Intro to Computer Science II Recursions (2) 1

Recursive binary search

Check if a number is a prime

Playing Tic-Tac-Toe Consider the game of tic-tac-toe If you play tic-tac-toe against a computer, how does the computer make its decisions?

Game Tree Provides the sequence of all possible moves that can be made in the game for both opponents. The computer can evaluate the game tree and determine its best move. For tic-tac-toe, the best move is one that allows it to win before its opponent in the fewest possible moves. The “computer” can evaluate every possible move much faster than a human.

Game Tree Example Suppose you (O) have been playing against the computer (X) and now it's the computer's turn.

Game Tree Example The computer would need to evaluate all of its possible moves to determine the best.

Game Tree Example The following figure shows the rest of the tree for a movement in the upper-right square.

The 8-Queens Problem The task is to place 8 queens onto a chessboard such that no queen can attack another queen. Uses a standard 8 x 8 chess board. There are 92 solutions to this problem.

Queen Moves The queen can move and attack any piece of the opponent by moving in any direction along a straight line.

Sample Solutions

4-Queens Problem To develop an algorithm, we consider the smaller 4-queens problem. Since no two queens can occupy the same column, we can proceed one column at a time. Place a queen in position (0, 0).

4-Queens Problem This move eliminates a number of squares for the placement of additional queens.

4-Queens Problem We move to the second column and place a queen at position (2, 1)

4-Queens Problem The 3rd queen should be placed in the 3rd column. But there are no open cells in the third column. So there is no solution based on the placement of the first 2 queens.

4-Queens Problem We have to backtrack: go back to the previous column pickup the last queen placed try to find another valid cell in that column.

4-Queens Problem Place a queen at position (3,1) and move forward.

4-Queens Problem In the 3rd column, we can now place a queen at position (1,2). But now we have no open slots in the 4th column.

4-Queens Problem We again must backtrack and pick up the queen from the 3rd column. But there are no other empty cells in the 3rd column.

4-Queens Problem We must backtrack yet again and pick up the queen from the 2rd column. But there are no other empty cells in the 2nd column either.

4-Queens Problem So we backtrack one more time and pick up the queen from the 1st column. We then try again to place a queen in the 1st column.

4-Queens Problem In the 1st column, we can place a queen at position (1, 0).

4-Queens Problem We again continue with the process and attempt to find open positions in each of the remaining columns. We can use a similar approach to solve the original 8-queens problem.

N-Queens Board ADT The n-queens board is used for positioning queens on a square board for use in solving the n-queens problem. consists of n x n squares. each square is identified by index [0...n) NQueensBoard( n ) size() numQueens() unguarded( row, col ) placeQueen( row, col ) removeQueen( row, col ) reset() draw()

8-Queens Solution def solveNQueens( board, col ): if board.numQueens() == board.size() : return True else : for row in range( board.size() ): if board.unguarded( row, col ): board.placeQueen( row, col ) if board.solveNQueens( board, col+1 ) : board.removeQueen( row, col ) return False