CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size...
-
Upload
joanna-daniel -
Category
Documents
-
view
213 -
download
1
Transcript of CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size...
CS 100 Lecture 19 1
CS100J Lecture 19CS100J Lecture 19
Previous LecturePrevious Lecture
– Two dimensional arrays.Two dimensional arrays.– Reasonable size problem (a past assignment).Reasonable size problem (a past assignment).– Stepwise refinement.Stepwise refinement.– Use of comments as high-level specifications:Use of comments as high-level specifications:– as high-level commands,as high-level commands,– as representation invariants.as representation invariants.– Incremental development and testing.Incremental development and testing.– Use of sentinels.Use of sentinels.– Static declarations.Static declarations.– Local declarations, scope, and the reuse of Local declarations, scope, and the reuse of names.names.– Heuristic algorithms.Heuristic algorithms.
This LectureThis Lecture
– Representation Rules of Thumb.Representation Rules of Thumb. Transform problems to simpler equivalent Transform problems to simpler equivalent
problemsproblems Maintain duplicate representations if helpfulMaintain duplicate representations if helpful Choose representations that limit search spacesChoose representations that limit search spaces Find representations that yield uniform algorithms.Find representations that yield uniform algorithms.
CS 100 Lecture 19 2
ThinkThink
// Print the sum of the integers from 1 through n.// Print the sum of the integers from 1 through n.
System.out.println(______________________________);System.out.println(______________________________);
Don’t use brute force just because the computer Don’t use brute force just because the computer is a brute.is a brute.
CS 100 Lecture 19 3
Ricocheting BulletRicocheting Bullet
A 1-by-1 box has an opening of width d. Shoot a A 1-by-1 box has an opening of width d. Shoot a gun into the box at angle gun into the box at angle . How far does the . How far does the bullet travel? bullet travel?
Transform problems to simpler equivalent Transform problems to simpler equivalent problems.problems.
1 foot
1 foot
d
CS 100 Lecture 19 4
Ricocheting Bullet, continuedRicocheting Bullet, continued
Transform problems to simpler equivalent Transform problems to simpler equivalent problems.problems.
CS 100 Lecture 19 5
Ricocheting Bullet, continuedRicocheting Bullet, continued
Transform problems to simpler equivalent Transform problems to simpler equivalent problems.problems.
2
4
6
8y
x
CS 100 Lecture 19 6
Ricocheting Bullet, continuedRicocheting 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 ToeTic Tac Toe
Maintain duplicate representations if helpfulMaintain duplicate representations if helpful
B 0 1 2
0
1
2
0 1 2
0
1
2
MovesX sumX
BB
0 1 2 3 4 5 6 7 8 9
CS 100 Lecture 19 8
Magic SquareMagic Square
CS 100 Lecture 19 9
Eight QueensEight Queens
Choose representations that limit the search Choose representations that limit the search spacespaceB 0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7
0 1 2 3 4 5 6 7
R
CS 100 Lecture 19 10
Eight Queens, continuedEight Queens, continued
/* Solve the Eight Queens problem. *//* Solve the Eight Queens problem. */
staticstatic void main(String args[])) void main(String args[]))
{{
/* R[c] is row of queen in column c,/* 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/* Consider each permutation of R until
one is found that represents a solution,one is found that represents a solution,
or loop forever. */or loop forever. */
whilewhile ( same_diagonal(R) ) ( same_diagonal(R) )
next_permutation(R);next_permutation(R);
/* Output solution R. *//* Output solution R. */
......
}}
CS 100 Lecture 19 11
Eight Queens, continuedEight Queens, continued
B 0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7
0 1 2 3 4 5 6 7
8
9
10
11
12
13
14
Positive diagonal index is row+column
CS 100 Lecture 19 12
Eight Queens, continuedEight 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
01
2
3
4
5
6
7
CS 100 Lecture 19 13
Eight Queens, continuedEight Queens, continued
// == 1 if R has two queens on same diagonal, else 0// == 1 if R has two queens on same diagonal, else 0staticstatic boolean same_diagonal( int [] R ) { boolean same_diagonal( int [] R ) { boolean [] PosDiag = boolean [] PosDiag = newnew boolean[15]; boolean[15]; boolean [] NegDiag = boolean [] NegDiag = newnew boolean[15]; boolean[15]; // Set PosDiag and NegDiag to all false.// Set PosDiag and NegDiag to all false.
forfor (int i = 0; i<=14; i++) { (int i = 0; i<=14; i++) { PosDiag[i] = PosDiag[i] = falsefalse; NegDiag[i] = ; NegDiag[i] = falsefalse;; }}
// 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 = boolean same = falsefalse;;int c = 0; // column indexint c = 0; // column indexwhilewhile ( c <= 7 && !same ) { ( c <= 7 && !same ) { ifif ( PosDiag[ R[c] + c ] || ( PosDiag[ R[c] + c ] ||
NegDiag[ c - R[c] + 7 ] )NegDiag[ c - R[c] + 7 ] ) same = same = truetrue;; else else {{ PosDiag[ R[c] + c ] = PosDiag[ R[c] + c ] = truetrue;; NegDiag[ c - R[c] + 7 ] = NegDiag[ c - R[c] + 7 ] = truetrue;; c++;c++; }} }}
returnreturn same; same;}}
CS 100 Lecture 19 14
CheckersCheckers
Find representations that yield uniform Find representations that yield uniform algorithmsalgorithms
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
0
1
2
3
4
5
6
7 0 1 2 3
8 9 10 11
16 17 18 19
24 25 26 27
CS 100 Lecture 19 15
Checkers, continuedCheckers, 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
0
1
2
3
4
5
6
7 0 1 2 3
9 10 11 12
18 19 20 21
27 28 29 30
CS 100 Lecture 19 16
Checkers, continuedCheckers, continued
B 0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7
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 351 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