Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.

Similar presentations


Presentation on theme: "CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity."— Presentation transcript:

1 CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity –Java Constructs n Conditional Expressions n This Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional arrays n Initializers for arrays n final –Reading n Lewis & Loftus Section 6.3 n Savitch, Section 6.5

2 CS 100Lecture 172 The 15 Puzzle (Sam Loyd 1878) 231 56 9 78 101112 141315 4 591 26 3 1014 71115 8412 13 initial configuration n 15 numbered tiles in a 4-by-4 frame: final configuration Problem. Given a sequence of “moves” as input, say whether or not the sequence of moves changes the initial configuration into the final configuration.

3 CS 100Lecture 173 Representation of Moves n How should a move be represented in the input data? MoveNameMeans 1moveDownfill the blank from above 2moveUpfill the blank from below 3moveRightfill the blank from left 4moveLeftfill the blank from right 0endMoveend of data n Named Constants in Java final int endMove = 0; final int moveDown = 1; final int moveUp = 2; final int moveRight = 3; final int moveLeft = 4;

4 CS 100Lecture 174 Representation of the Tiles n How should the tiles be represented? n We need 15 values to represent the tiles and one to represent the blank? // Use 1..15 for the tiles and 0 for blank. final int Blank = 0; or // Use 1..15 for the tiles and 16 for blank. final int Blank = 16; n Rule of Thumb. Choose uniform representations if at all possible

5 CS 100Lecture 175 Representation of the Frame n We need 16 variables, one to represent the tile at each position in the frame. n We could use the declaration: int[] puz = new int[16]; to get variables puz[0],..., puz[15], but this is unwieldy and unnatural. n Rule of Thumb. Unless there is a compelling reason algorithmic reason to do otherwise, use data that is structured similarly to the physical device being simulated. n We use a two-dimensional array.

6 CS 100Lecture 176 Two Dimensional Arrays n Declaration of two-dimensional array: // Create 4-by-4 array named puz. int [][] puz = new int[4][4]; n which is equivalent to: // Create 4-by-4 array named puz. int [][] puz = new int[4][]; puz[0] = new int[4]; puz[1] = new int[4]; puz[2] = new int[4]; puz[3] = new int[4]; 0 1 2 3 01230123 puz

7 CS 100Lecture 177 Rows and Columns n Vocabulary –row-major order in a 2-D array is 1st row, then 2nd row, then 3rd row, etc. –column-major order in a 2-D array is 1st column, then 2nd column, then 3rd column, etc. n The default interpretation of a 2-D array is to consider the first index the row, and the second index the column: –puz[r][c] is the variable in row r and column c –puz[r] is row r

8 CS 100Lecture 178 Program Outline /* If the sequence of moves transforms the puzzle from the initial configuration into the final configuration, output “yes”, else output “no”. */ static void test() { TokenReader in = new TokenReader(System.in); final int Blank = 16; final int endMove = 0; final int moveDown = 1; final int moveUp = 2; final int moveRight = 3; final int moveLeft = 4; /* Let puz be the initial configuration, the integers 1..16 in row major order. */... /* Update puz according to each move. */... /* Say whether puz is final configuration, i.e., 1..16 in column major order. */... }

9 CS 100Lecture 179 Initialization n Solution 1: /* Let puz be the initial configuration, the integers 1..16 in row major order. */ int [][] puz = new int [4][4]; for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) for (int c = 0; c < 4; c++){ puz[r][c] = ____________________; puz[r][c] = ____________________;} n Solution 2: /* Let puz be the initial configuration, the integers 1..16 in row major order. */ int [][] puz = new int [4][4]; for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) for (int c = 0; c < 4; c++) puz[r][c] = ____________________________;

10 CS 100Lecture 1710 Puzzle Updating n Rule of Thumb. Use relevant patterns. /* Update puz according to each move. */ int m;// current move m = in.readInt(); while ( m != endMove ) { // Process move m....... m = in.readInt(); }

11 CS 100Lecture 1711 Relative Coordinate System Introduce variables rowB, colB to track the blank. Introduce variables rowB, colB to track the blank. /* rowB,colB is position of blank, initially the lower-right hand corner. */ int rowB = 3; int colB = 3; n Picture the frame relative to the position of blank (in general, after the blank has moved around puz[rowB-1] [colB-1] puz[rowB] [colB-1] puz[rowB+1] [colB-1] puz[rowB-1] [colB] puz[rowB] [colB] puz[rowB+1] [colB] puz[rowB-1] [colB+1] puz[rowB] [colB+1] puz[rowB+1] [colB+1]

12 CS 100Lecture 1712 Exhaustive Solution // Process move m. if ( m==moveDown ){ // fill from above puz[rowB][colB] = puz[rowB-1][colB]; puz[rowB-1][colB] = Blank; rowB--;} else if ( m==moveUp ) { // fill from below puz[rowB][colB] = puz[rowB+1][colB]; puz[rowB+1][colB] = Blank; rowB++;} else if ( m==moveRight) {// fill from left puz[rowB][colB] = puz[rowB][colB-1]; puz[rowB][colB-1] = Blank; colB--;} else {// fill from right puz[rowB][colB] = puz[rowB][colB+1]; puz[rowB][colB+1] = Blank; colB++;}

13 CS 100Lecture 1713 Puzzle Updating: Uniform Treatment n Rule of Thumb. Avoid case analysis if possible. /* Update puz according to each move. */ int m;// current move m = in.readInt(); while ( m != endMove ) { // Process move m. // Blank moves to rowNew, colNew. int rowNew = _________________________; int colNew = _________________________; puz[rowB][colB] = puz[rowNew][colNew]; puz[rowNew][colNew] = Blank; rowB = rowNew; colB = colNew; m = in.readInt(); }

14 CS 100Lecture 1714 Array Initializers n A 1-dimensional array can be initialized with: { comma-separated list of expressions } n Example: /* Offsets to new position of blank on move m are delatR[m] and deltaC[m]. */ int [] deltaR = {0, -1, 1, 0, 0}; int [] deltaR = {0, -1, 1, 0, 0}; int [] deltaC = {0, 0, 0, -1, 1}; int [] deltaC = {0, 0, 0, -1, 1}; n A 2-dimensional array can be initialized with: { comma-separated list of array-initializers } n Example (solution 3 for setting the initial configuration): int [][] puz = { {1,2,3,4}, {5,6,7,8},{9,10,11,12}, {13,14,15,16} };

15 CS 100Lecture 1715 Puzzle Updating: Final Version /* Update puz according to each move. */ /* Offsets to new position of blank on move m are delatR[m] and deltaC[m]. */ int [] deltaR = {0, -1, 1, 0, 0}; int [] deltaC = {0, 0, 0, -1, 1}; /* rowB,colB is position of blank, initially the lower-right hand corner. int rowB = 3; int colB = 3; int m;// current move m = in.readInt(); while ( m != endMove ) { // Process move m. // Blank moves to rowNew, colNew. int rowNew = rowB + deltaR[m]; int rowNew = rowB + deltaR[m]; int colNew = colB + deltaC[m]; puz[rowB][colB] = puz[rowNew][colNew]; rowB = rowNew; colB = colNew; m = in.readInt(); } puz[rowB][colB] = Blank;


Download ppt "CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity."

Similar presentations


Ads by Google