CS100A Lecture 18 - PowerPoint PPT Presentation

About This Presentation
Title:

CS100A Lecture 18

Description:

Two dimensional arrays. Reasonable size problem (a past assignment). Stepwise refinement. ... static double distance( double d, double th ) double y = min_y(d, th ) ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 15
Provided by: Mill1
Category:
Tags: cs100a | dimension | lecture | th

less

Transcript and Presenter's Notes

Title: CS100A Lecture 18


1
CS100A Lecture 18
  • 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.
  • 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.

2
Ricocheting Bullet
  • 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
?
3
Ricocheting Bullet, continued
  • Transform problems to simpler equivalent
    problems.

4
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 gt 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))) gt d )
  • y y 2
  • return y
  • / x2 /
  • static double sqr( double x ) return x x
  • / distance traveled by bullet. /
  • static double distance( double d, double th )

5
Tic Tac Toe
  • Maintain duplicate representations if helpful

MovesX
sumX
6
Magic Square
7
Eight Queens
  • Choose representations that limit the search
    space

8
Eight Queens, continued
  • / Solve the Eight Queens problem. /
  • static void main(String args))
  • / Rc is row of queen in column c,
  • for 0 lt c lt 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. /
  • ...

9
Eight Queens, continued
0 1 2 3 4 5 6
7
8 9 10 11 12 13 14
Positive diagonal index is rowcolumn
10
Eight Queens, continued
14 13 12 11 10 9 8
0 1 2 3 4 5 6
7
Negative diagonal index is column-row7
11
Eight Queens, continued
  • // 1 if R has two queens on same diagonal,
    else 0
  • static int same_diagonal( int R )
  • boolean PosDiag new boolean15
  • boolean NegDiag new boolean15
  • // Set PosDiag and NegDiag to all false.
  • for (int i 0 ilt14 i)
  • PosDiagi false NegDiagi false
  • // Set same to true if R has 2 queens on same
    diag.
  • boolean same false
  • int c 0 // column index
  • while ( c lt 7 !same )
  • if ( PosDiag Rc c
  • NegDiag c - Rc 7 )
  • same true
  • else
  • PosDiag Rc c true

12
Checkers
  • Find representations that yield uniform algorithms

B 0 1 2 3 4 5
6 7
0 1 2 3 4 5 6 7
28 29 30 31
24 25 26 27
20 21 22 23
16 17 18 19
12 13 14 15

8 9 10 11
4 5 6 7
0 1 2 3
13
Checkers, continued
B 0 1 2 3 4 5
6 7
0 1 2 3 4 5 6 7
31 32 33 34
35
27 28 29 30
22 23 24 25
26
18 19 20 21
13 14 15 16
17

9 10 11 12
4 5 6 7
8
0 1 2 3
14
Checkers, continued
B 0 1 2 3 4 5
6 7
0 1 2 3 4 5 6 7
red shifted right 5
Write a Comment
User Comments (0)
About PowerShow.com