10-29 Class Notes CS 107

10-29 Class Notes CS 107 - 10-29 Class Notes CS 107 Friday...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Announcements: - Remember in-class portion of midterm #2 will be on Monday. Questions? Last Time: Today: The maze problem: combining arrays with recursion A detailed example combining arrays with recursion, to traverse a maze: /** * Class Maze1 illustrates navigating a maze using a 2-d array, * where the solution can be found simply by always going down * if possible, otherwise going to the right. * * Running the program gives: [1,1], [1,2], [2,2], [3,2], [4,2], [4,3], [4,4], [4,5], [5,5], [6,5], [7,5], [8,5], [8,6], [8,7], [8,8], Got to destination! [4,6], [1,3], * * Problems: * What if the solution requires going up first? * More moves are displayed after the solution is found. */ public class Maze1 { private static int startRow = 1; // starting row, static so main() can use it private static int startCol = 1; // starting column static so main() can use it private int endRow = 8; // ending row private int endCol = 8; // ending column // declare the actual maze as a 2-dimensional array // A 1 is a wall, while a 0 is an open pathway. private int[][] maze = { /* 0 1 2 3 4 5 6 7 8 9 */ /* 0 */ {1,1,1,1,1,1,1,1,1,1}, /* 1 */ {1,0,0,0,1,1,1,1,1,1}, /* 2 */ {1,1,0,1,1,1,0,0,1,1}, /* 3 */ {1,1,0,1,1,1,0,1,1,1}, /* 4 */ {1,1,0,0,0,0,0,1,1,1}, /* 5 */ {1,1,1,1,1,0,1,1,1,1}, /* 6 */ {1,0,1,1,1,0,1,0,0,1}, /* 7 */ {1,1,1,1,1,0,1,0,1,1}, /* 8 */ {1,1,0,0,0,0,0,0,0,1}, 10-29 Class Notes CS 107 Friday, October 29, 2010 12:30 PM 107 Fall 2010 Page 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
/* 5 */ {1,1,1,1,1,0,1,1,1,1}, /* 6 */ {1,0,1,1,1,0,1,0,0,1}, /* 7 */ {1,1,1,1,1,0,1,0,1,1}, /* 8 */ {1,1,0,0,0,0,0,0,0,1}, /* 9 */ {1,1,1,1,1,1,1,1,1,1} }; /** * Main method to allow execution from the command line */ public static void main(String[] args) { Maze1 instanceOfMaze1 = new Maze1(); // default constructor instanceOfMaze1.makeMove( startRow, startCol); } public void makeMove( int row, int col) { // display location System.out.print("[" + row + "," + col + "], "); // see if we've reached the destination if (( row == endRow) && ( col == endCol)) { System.out.println("\n Got to destination! "); } // see if we can move down if ( maze[ row+1][ col] != 1) { makeMove( row+1, col); // recursive call, moving down } if (maze[ row][ col+1] != 1) { // see if we can move right makeMove( row, col+1); // recursive call, moving right // Note that we never move up or left! } }//end makeMove
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/10/2012 for the course COP 2271 taught by Professor Reed during the Fall '10 term at University of Central Florida.

Page1 / 7

10-29 Class Notes CS 107 - 10-29 Class Notes CS 107 Friday...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online