CS100J Lecture 18 - PowerPoint PPT Presentation

About This Presentation
Title:

CS100J Lecture 18

Description:

Knight's Tour. Chess is played on an 8-by-8 board. A knight can move 2 in one direction (horizontal or vertical) and 1 in the other ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 18
Provided by: Mill1
Category:
Tags: cs100j | knight | lecture

less

Transcript and Presenter's Notes

Title: CS100J Lecture 18


1
CS100J Lecture 18
  • Previous Lecture
  • Programming concepts
  • Two-dimensional arrays
  • Java Constructs
  • Constructors for two-dimensional arrays
  • Initializers for arrays
  • final
  • Reading
  • Lewis Loftus Section 6.3
  • Savitch, Section 6.5
  • 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.

2
Knights Tour
  • Chess is played on an 8-by-8 board.
  • A knight can move 2 in one direction (horizontal
    or vertical) and 1 in the other direction
    (vertical or horizontal). For example, Kn can
    move to each square marked X.
  • Problem. Write a program that tries to find a
    "knight's tour", e.g., starting in the upper left
    corner, the knight visits each of the 64 squares
    exactly once.

3
Possible Solution
4
Representation
  • Rule of Thumb. Avoid using literal constants in
    the code define and use symbolic constants.
  • Static class declarations visible to all methods.
  • class Knight
  • / Chess board B is N-by-N int array, for N
    8. /
  • final static int N 8
  • static int B new int NN
  • / Unvisited squares are Blank. /
  • static final int Blank 0
  • / Board subscripts range from lo to hi. /
  • static final int lo 0
  • static final int hi 7
  • / UNDEFINED, an illegal coordinate. /
  • static final int UNDEFINED -1

5
Main
  • / Try to find a Knight's Tour. /
  • static void main(String args)
  • / Set B to all Blank. /
  • Initialize()
  • / Set B to arrangement of the integers 1,2,3,...
    representing consecutive positions of the knight
    during the tour. Squares that the knight cannot
    reach remain Blank. /
  • Solve()
  • / Print B in an 8-by-8 grid. /
  • Display()

6
Initialize
  • / Set B to all Blank. /
  • static void Initialize()
  • for (int r lo r lt hi r)
  • for (int c lo c lt hi c)
  • Brc Blank

7
Solve
  • / Set B to arrangement of the integers 1,2,3,...
    representing consecutive positions of the knight
    during the tour. Squares that the knight cannot
    reach remain Blank. /
  • static void Solve()
  • / This procedure "stub" permits us to test the
    main program, Initialize, and Display routines.
    /

8
Display
  • / Print B in an 8-by-8 grid. /
  • static void Display()
  • for (int r lo r lt hi r)
  • for (int c lo c lt hi c)
  • System.out.print(Brc )
  • System.out.println()
  • Rule of Thumb. Develop your program using small
    procedures. Test it incrementally.

9
Sample Intermediate State
  • while ( ________________________)

10
Pattern
  • / Start at the beginning /
  • . . .
  • while ( / not past the end / )
  • / Process the current place /
  • . . .
  • / Go on to the next place (or to past the
    end). /
  • . . .

11
Solve
  • / Initial configuration. /
  • int move 0 // moves so far.
  • int r lo // current row of Kn.
  • int c lo // current column of Kn.
  • while (r ! UNDEFINED)
  • / Visit ltr,cgt. /
  • move
  • Brc move
  • / Let ltr,cgt be coordinates of an unvisited
    "neighbor" of current ltr,cgt, or
    ltUNDEFINED,UNDEFINEDgt if current square has no
    unvisited neighbors. /
  • . . .

12
Choosing a Neighbor to Visit
  • / Let ltr,cgt be coordinates of an unvisited
    "neighbor" of current ltr,cgt, or
    ltUNDEFINED,UNDEFINEDgt if current square has no
    unvisited neighbors. /
  • int unvisitedR UNDEFINED
  • int unvisitedC UNDEFINED
  • for (int k 0 k lt 8 k)
  • int Nrow _____________________________
  • int Ncol _____________________________
  • if ( BNrowNcol Blank )
  • unvisitedR Nrow
  • unvisitedC Ncol
  • r unvisitedR
  • c unvisitedC

13
Neighbors
  • // 0 1 2 3 4 5 6 7
  • int deltaR -1, -2, -2, -1, 1, 2, 2, 1
  • int deltaC 2, 1, -1, -2, -2, -1, 1, 2

14
Boundary Conditions
  • where X can be any non-zero value

0 1 2 3 4 5 6 7 8 9
10 11
0 1 2 3 4 5 6 7 8 9 10 11
x x x x x x x x x x x x
x x x x x x x x x x x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x x x x x x x x x x x
x x x x x x x x x x x x
15
Representation Revisited
  • Static class declarations visible to all methods.
  • / Chess board B is N-by-N int array, for N
    12. /
  • final int N 12
  • int B new int NN
  • / Unvisited squares are Blank. /
  • final int Blank 0
  • / Board subscripts range from lo to hi. /
  • final int lo 2
  • final int hi 9
  • / UNDEFINED, an illegal coordinate. /
  • final int UNDEFINED -1
  • / ltdeltaRk,deltaCkgt is ltrow,colgt offset to
    neighbor k. /
  • final static int deltaR
  • -1,-2,-2,-1, 1, 2,2,1
  • final static int deltaC

16
Improvement Using Heuristic
  • Go where the options are running out, i.e., go to
    the unvisited neighbor with the fewest unvisited
    neighbors.
  • / Let ltr,cgt be coordinates of an unvisited
    "neighbor" of current ltr,cgt, or
    ltUNDEFINED,UNDEFINEDgt if current square has no
    unvisited neighbors. /
  • int fewest 9 // min unvisited neighbors
  • int neighborR UNDEFINED
  • int neighborC UNDEFINED
  • for (int k 0 k lt 8 k)
  • int Nrow r deltaRk
  • int Ncol c deltaCk
  • if ( BNrowNcol Blank )
  • int n unvisited(Nrow, Ncol)
  • if (n lt fewest )
  • neighborR Nrow neighborC Ncol
  • fewest n
  • r neighborR

17
Unvisited
  • / the number of neighbors of square ltr,cgt
    that are unvisited. /
  • static int unvisited(int r, int c)
  • int count 0 // unvisited neighs.
  • for (int k 0 k lt 8 k)
  • int Nrow r deltaRk
  • int Ncol c deltaCk
  • if ( BNrowNcol Blank ) count
  • return count
Write a Comment
User Comments (0)
About PowerShow.com