Documente Academic
Documente Profesional
Documente Cultură
10/17/2012
CS 260:
Foundations Of
Computer Science
We are surrounded by
insurmountable opportunity.
Pogo (Walt Kelley)
Todays Agenda
Chapter 7 Stacks
Stacks
Chapter 7
10/17/2012
Stacks
Solving a Maze
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Brute-Force
Backtracking
Chapter 7: Stacks 9
Chapter 7: Stacks 10
Designing a Solution
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Backtracking
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 8
Problem details
Algorithm description
Implementation details
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 11
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 12
10/17/2012
Problem Description
Maze Structure
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 13
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Movement
The Algorithm
Chapter 7: Stacks 14
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 15
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 17
Chapter 7: Stacks 16
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 18
10/17/2012
numRows()
numCols()
findPath()
reset()
draw()
Chapter 7: Stacks 19
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 20
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
5 5
4 1
3 4
*****
*.*.*
*...*
*.*..
*.***
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 21
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Maze Implementation
Maze Implementation
maze.py
class Maze :
# Define constants to represent contents of the maze cells.
MAZE_WALL = "*"
PATH_TOKEN = "x"
TRIED_TOKEN = "o"
# Creates a maze object with all cells marked as open.
def __init__( self, numRows, numCols ):
self._mazeCells = Array2D( numRows, numCols )
self._startCell = None
self._exitCell = None
# ...
# Private storage class for holding a cell position.
class _CellPosition( object ):
def __init__( self, row, col ):
self.row = row
self.col = col
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 23
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 24
10/17/2012
Maze Implementation
maze.py
class Maze :
# ...
# Returns the number of rows in the maze.
def numRows( self ):
return self._mazeCells.numRows()
# Returns the number of columns in the maze.
def numCols( self ):
return self._mazeCells.numCols()
Chapter 7: Stacks 25
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Maze Implementation
Maze Implementation
maze.py
class Maze :
# ...
# Fills the indicated cell
def setWall( self, row, col
assert row >= 0 and row <
col >= 0 and col <
"Cell index out of
self._mazeCells.set( row,
Chapter 7: Stacks 26
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 27
maze.py
class Maze :
# ...
# Returns True if the given cell position is a valid move.
def _validMove( self, row, col ):
return row >= 0 and row < self.numRows() and \
col >= 0 and col < self.numCols() and \
self._mazeCells[row, col] is None
# Helper method to determine if the exit was found.
def _exitFound( self, row, col ):
return row == self._exitCell.row and \
col == self._exitCell.col
# Drops a "tried" token at the given cell.
def _markTried( self, row, col ):
self._mazeCells.set( row, col, self.TRIED_TOKEN )
# Drops a "path" token at the given cell.
def _markPath( self, row, col ):
self._mazeCells.set( row, col, self.PATH_TOKEN )
2011 John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.
Chapter 7: Stacks 28