Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100J Lecture 19 Previous Lecture This Lecture

Similar presentations


Presentation on theme: "CS100J Lecture 19 Previous Lecture This Lecture"— Presentation transcript:

1 CS100J Lecture 19 Previous Lecture This 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. This Lecture Representation Rules of Thumb. Transform problems to simpler equivalent problems Maintain duplicate representations if helpful Choose representations that limit search spaces Find representations that yield uniform algorithms. CS 100 Lecture 19

2 Think // Print the sum of the integers from 1 through n.
System.out.println(______________________________); Don’t use brute force just because the computer is a brute. CS 100 Lecture 19

3 Ricocheting Bullet 1 foot 1 foot d 
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? Transform problems to simpler equivalent problems. 1 foot 1 foot d CS 100 Lecture 19

4 Ricocheting Bullet, continued
Transform problems to simpler equivalent problems. CS 100 Lecture 19

5 Ricocheting Bullet, continued
Transform problems to simpler equivalent problems. y 8 6 4 2 x CS 100 Lecture 19

6 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) ); CS 100 Lecture 19

7 Tic Tac Toe MovesX sumX BB B 0 1 2 1 2 0 1 2 1 2 0 1 2 3 4 5 6 7 8 9
Maintain duplicate representations if helpful B 1 2 1 2 MovesX sumX BB CS 100 Lecture 19

8 Magic Square CS 100 Lecture 19

9 Eight Queens Choose representations that limit the search space B 1 2 3 4 5 6 7 R CS 100 Lecture 19

10 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. */ 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. */ while ( same_diagonal(R) ) next_permutation(R); /* Output solution R. */ ... } CS 100 Lecture 19

11 Eight Queens, continued
8 9 10 11 12 13 14 B 1 2 3 4 5 6 7 Positive diagonal index is row+column CS 100 Lecture 19

12 Eight Queens, continued
B 1 2 3 4 5 6 7 14 13 12 11 10 9 8 Negative diagonal index is column-row+7 CS 100 Lecture 19

13 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 [] NegDiag = new boolean[15]; // Set PosDiag and NegDiag to all false. for (int i = 0; i<=14; i++) { PosDiag[i] = false; NegDiag[i] = false; } // 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 ] || NegDiag[ c - R[c] + 7 ] ) same = true; else { PosDiag[ R[c] + c ] = true; NegDiag[ c - R[c] + 7 ] = true; c++; return same; CS 100 Lecture 19

14 Checkers Find representations that yield uniform algorithms B 1 2 3 4 5 6 7 CS 100 Lecture 19

15 Checkers, continued B 1 2 3 4 5 6 7 CS 100 Lecture 19

16 Checkers, continued B 1 2 3 4 5 6 7 red red shifted right 5 free moves CS 100 Lecture 19


Download ppt "CS100J Lecture 19 Previous Lecture This Lecture"

Similar presentations


Ads by Google