Download presentation
Presentation is loading. Please wait.
Published byJoanna Daniel Modified over 9 years ago
1
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use of comments as high-level specifications: –as high-level commands, –as representation invariants. –Incremental development and testing. –Use of sentinels. –Static declarations. –Local declarations, scope, and the reuse of names. –Heuristic algorithms. n This Lecture –Representation Rules of Thumb. n Transform problems to simpler equivalent problems n Maintain duplicate representations if helpful n Choose representations that limit search spaces n Find representations that yield uniform algorithms.
2
CS 100Lecture 192 Think // Print the sum of the integers from 1 through n. System.out.println(______________________________); n Don’t use brute force just because the computer is a brute.
3
CS 100Lecture 193 Ricocheting Bullet n A 1-by-1 box has an opening of width d. Shoot a gun into the box at angle . How far does the bullet travel? n Transform problems to simpler equivalent problems. 1 foot d
4
CS 100Lecture 194 Ricocheting Bullet, continued n Transform problems to simpler equivalent problems.
5
CS 100Lecture 195 Ricocheting Bullet, continued n Transform problems to simpler equivalent problems. 2 4 6 8 y x
6
CS 100Lecture 196 Ricocheting Bullet, continued /* == the x corresponding to y and th. */ static double x( double y, double th ) { return (y / Math.tan( th )) ; } /* == the smallest even y > 0 for which the fractional part of the corresponding x is not larger than d.*/ static double min_y( double d, double th ) { int y = 2; while ( (x(y,th) - Math.floor(x(y,th))) > d ) y = y + 2; return y; } /* == x^2 */ static double sqr( double x ) { return x * x; } /* == distance traveled by bullet. */ static double distance( double d, double th ) { double y = min_y(d, th ); return Math.sqrt( sqr( x(y,th) ) + sqr(y) ); }
7
CS 100Lecture 197 Tic Tac Toe n Maintain duplicate representations if helpful B 0 1 2 012012 0 1 2 012012 MovesX sumX BB 0 1 2 3 4 5 6 7 8 9
8
CS 100Lecture 198 Magic Square
9
CS 100Lecture 199 Eight Queens n Choose representations that limit the search space B 0 1 2 3 4 5 6 7 0123456701234567 0 1 2 3 4 5 6 7 R
10
CS 100Lecture 1910 Eight Queens, continued /* Solve the Eight Queens problem. */ static void main(String args[])) { /* R[c] is row of queen in column c, for 0 <= c <= 7. */ for 0 <= c <= 7. */ int [] R = { 0, 1, 2, 3, 4, 5, 6, 7 }; int [] R = { 0, 1, 2, 3, 4, 5, 6, 7 }; /* Consider each permutation of R until one is found that represents a solution, or loop forever. */ or loop forever. */ while ( same_diagonal(R) ) while ( same_diagonal(R) ) next_permutation(R); next_permutation(R); /* Output solution R. */...}
11
CS 100Lecture 1911 Eight Queens, continued B 0 1 2 3 4 5 6 7 0123456701234567 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Positive diagonal index is row+column
12
CS 100Lecture 1912 Eight Queens, continued Negative diagonal index is column-row+7 0 1 2 3 4 5 6 7 14 13 12 11 10 9 8 B 0 1 2 3 4 5 6 7 0123456701234567
13
CS 100Lecture 1913 Eight Queens, continued // == 1 if R has two queens on same diagonal, else 0 static boolean same_diagonal( int [] R ) { boolean [] PosDiag = new boolean[15]; boolean [] PosDiag = new boolean[15]; boolean [] NegDiag = new boolean[15]; boolean [] NegDiag = new boolean[15]; // Set PosDiag and NegDiag to all false. // Set PosDiag and NegDiag to all false. for (int i = 0; i<=14; i++) { PosDiag[i] = false; NegDiag[i] = false; PosDiag[i] = false; NegDiag[i] = false; } // Set same to true if R has 2 queens on same diag. // Set same to true if R has 2 queens on same diag. boolean same = false; int c = 0; // column index while ( c <= 7 && !same ) { if ( PosDiag[ R[c] + c ] || if ( PosDiag[ R[c] + c ] || NegDiag[ c - R[c] + 7 ] ) NegDiag[ c - R[c] + 7 ] ) same = true; same = true; else { else { PosDiag[ R[c] + c ] = true; PosDiag[ R[c] + c ] = true; NegDiag[ c - R[c] + 7 ] = true; NegDiag[ c - R[c] + 7 ] = true; c++; c++; } } return same; }
14
CS 100Lecture 1914 Checkers n Find representations that yield uniform algorithms 28 29 30 31 20 21 22 23 12 13 14 15 4 5 6 7 B 0 1 2 3 4 5 6 7 0123456701234567 0 1 2 3 8 9 10 11 16 17 18 19 24 25 26 27
15
CS 100Lecture 1915 Checkers, continued 31 32 33 34 35 22 23 24 25 26 13 14 15 16 17 4 5 6 7 8 B 0 1 2 3 4 5 6 7 0123456701234567 0 1 2 3 9 10 11 12 18 19 20 21 27 28 29 30
16
CS 100Lecture 1916 Checkers, continued B 0 1 2 3 4 5 6 7 0123456701234567 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0... 0 0 0 0 0 0 1 1 0 1 0 0 0...free 0 35 1 1 1 1 1 1 0 0 0 0 0 0 0... red 0 0 0 0 0 0 1 1 0 1 0 0 0... moves red shifted right 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.