1 Data Structures CSCI 132, Spring 2014 Lecture 18 Recursion and Look-Ahead.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

Adversarial Search We have experience in search where we assume that we are the only intelligent being and we have explicit control over the “world”. Lets.
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.
1 Data Structures CSCI 132, Spring 2014 Lecture 15 Recursion.
Tic Tac Toe Game Design Using OOP
PLANNING THE TIC TAC TOE GAME BY NEEL DAVE. TIC TAC TOE INSTRUCTIONS Tic Tac Toe Instructions The basic concept of Tic Tac Toe 1.This is a game for two.
The N-Queens Problem lab01.
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?
Chapter5 RECURSION. Outline 1. Introduction to Recursion 2. Principles of Recursion 3. Backtracking: Postponing the Work 4. Tree-Structured Programs:
Lecture 4 1) RECURSION 2)BACKTRACKING 3)LOOK AHEAD.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Recursion Chapter 5.
Announcements.
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
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
Artificial Intelligence and Robotics By Keith Bright & John DeBovis.
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
CSC 205 Programming II Lecture 18 The Eight Queens Problem.
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.
Sudoku Jordi Cortadella Department of Computer Science.
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
TIC TAC TOE. import java.util.Scanner; import java.util.Random; public class PlayTTT{ public static void main(String[]args){ Scanner reader = new Scanner(System.in);
1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists.
Solving Inequalities. C + 3 < 12 Guess a reasonable solution and write your guess.
1 Data Structures CSCI 132, Spring 2014 Lecture 17 Backtracking.
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Introduction to State Space Search
© 2006 Pearson Addison-Wesley. All rights reserved 6-1 Chapter 6 Recursion as a Problem- Solving Technique.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE
Chomp. How is the game played Human player goes first choose a square, all to the right and down are “eaten” computer takes a turn whoever is forced to.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
1 Working with Controls at Run Time. 2 Objectives You will be able to Add controls to a Windows form at run time. Modify controls at run time.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CSE 143 read: 12.5 Lecture 18: recursive backtracking.
Data Structures & Algorithms Lecturers : Boontee Kruatrachue Room no. 913 Kritawan Siriboon Room no. 913 Lecturers : Boontee Kruatrachue Room no. 913 Kritawan.
Data Structures & Algorithms Lecturer : Kritawan Siriboon, Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++,… Mark Allen Weiss, Addison.
Game playing Types of games Deterministic vs. chance
Backtracking, Search, Heuristics
CS100J Lecture 19 Previous Lecture This Lecture
Intro to Computer Science II
CSCI 104 Backtracking Search
Data Structures and Algorithms
Adversarial Search.
Sit-In Lab 1 Ob-CHESS-ion
Multi-dimensional Array
CSE 143 Lecture 19 More Recursive Backtracking
Functions in C.
1) RECURSION 2) BACKTRACKING 3) LOOK AHEAD
Algorithms: Design and Analysis
Tic Tac Toe application
CSE 143 Lecture 18 More Recursive Backtracking
Two dimensional arrays.
CSE 143 Lecture 18 More Recursive Backtracking
Backtracking, Search, Heuristics
Data Structures and Algorithms
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
CS51A David Kauchak Spring 2019
Two dimensional arrays.
CS51A David Kauchak Spring 2019
Backtracking, Search, Heuristics
Data Structures & Programming
Data Structures & Programming
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 18 Recursion and Look-Ahead

2 Recall The Eight Queens Problem Can we place 8 queens on a chessboard so that none can take another? (I.e. none share a row, column or diagonal)? Q

3 Recall 8 Queens solution void solve_from(Queens &configuration) { if (configuration.is_solved( )) { configuration.print( ); } else { for (int col = 0; col < configuration.board_size; col++) { if (configuration.unguarded(col)) { configuration.insert(col); solve_from(configuration); // Recursively continue to add queens. configuration.remove(col); }

4 Print only one solution bool solve_from(Queens &configuration) //return true if solved { if (configuration.is_solved( )) { configuration.print( ); return true; } else { bool solved = false; for (int col = 0; !solved && col < configuration.board_size; col++) { if (configuration.unguarded(col)) { configuration.insert(col); solved = solve_from(configuration); configuration.remove(col); } } return solved; } }

5 Recursion tree for 4 queens

6 Game Tree for "Game of Six"

7 The MiniMax method

8 Finding the Best Move

9 Looking ahead recursively LookAhead() for each possible move make move answer = lookAhead() undo move keep track of best answer return best answer

10 Example: Tic Tac Toe class Move { //Class to represent a move for tic tac toe public: Move( ); Move( int r, int c); int row; int col; }; Move::Move( ) { row = 3; col = 3; } Move::Move(int r, int c) { row = r; col = c; }

11 The Playing Board class Board { public: Board( ); bool done( ) const; //true if game done void print( ) const; void instructions( ) const; bool better(int value, int old_value) const; //true if value better than old void play(Move try_it); //play given move int worst_case( ) const; //worst possible case value int evaluate( ) const; //how good is configuration? int legal_moves(Stack &moves) const; //List of legal moves private: int squares[3][3]; //0 => unoccupied, 1=> player 1, 2=>player2 int moves_done; int the_winner( ) const; };

12 Some Board Methods void Board :: play(Move try_it) { squares[try_it.row][try_it.col] = moves_done%2 + 1; moves_done++; } bool Board :: done( ) const { return ( moves_done == 9) || (the_winner( ) > 0); } int Board :: evaluate( ) const { int winner = the_winner( ); if (winner == 1) { return 10 - moves_done; } else if (winner == 2) { return moves_done - 10; } else { return 0; } }

Looking Ahead int look_ahead(const Board &game, int depth, Move &recommended) { if (game.done( ) || depth == 0) { return game.evaluate( ); } else { Stack moves; game.legal_moves(moves); int value, best_value = game.worst_case( ); while (!moves.empty( )) { Move try_it, reply; moves.top(try_it);//get next move Board new_game = game;//make copy of board new_game.play(try_it);//try next move value = look_ahead(new_game, depth - 1, reply); //recursively look ahead if (game.better(value, best_value)) { best_value = value;//value is new best value recommended = try_it;//recommend this move } moves.pop( );//pop it off the stack } return best_value;//return best value found } }