Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional.

Similar presentations


Presentation on theme: "CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional."— Presentation transcript:

1 CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional arrays n Initializers for arrays n final n 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.

2 CS 100Lecture 172 Knight’s Tour n Chess is played on an 8-by-8 board. n 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. n 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. Kn X X X X X X XX

3 CS 100Lecture 173 Possible Solution 1 22 3 18 25 30 13 16 4 19 24 29 14 17 34 31 23 2 21 26 35 32 15 12 20 5 56 49 28 41 36 33 57 50 27 42 61 54 11 40 6 43 60 55 48 39 64 37 51 58 45 8 53 62 47 10 44 7 52 59 46 9 38 63

4 CS 100Lecture 174 Representation n Rule of Thumb. Avoid using literal constants in the code; define and use symbolic constants. n 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 [N][N]; /* 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 CS 100Lecture 175 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 CS 100Lecture 176 Initialize /* Set B to all Blank. */ static void Initialize() { for (int r = lo; r <= hi; r++) for (int c = lo; c <= hi; c++) for (int c = lo; c <= hi; c++) B[r][c] = Blank; B[r][c] = Blank;}

7 CS 100Lecture 177 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 CS 100Lecture 178 Display /* Print B in an 8-by-8 grid. */ static void Display() { for (int r = lo; r <= hi; r++) { for (int c = lo; c <= hi; c++) for (int c = lo; c <= hi; c++) System.out.print(B[r][c] + ” ”); System.out.print(B[r][c] + ” ”);System.out.println();}} n n Rule of Thumb. Develop your program using small procedures. Test it incrementally.

9 CS 100Lecture 179 while ( ________________________) { } Sample Intermediate State 6 43 0 0 48 39 0 37 1 22 3 18 25 30 13 16 4 19 24 29 14 17 34 31 23 2 21 26 35 32 15 12 20 5 0 49 28 41 36 33 0 0 27 42 0 0 11 40 0 0 45 8 0 0 47 10 44 7 0 0 46 9 38 0

10 CS 100Lecture 1710 Solve { /* Initial configuration. */ /* Initial configuration. */ int move = 1; // moves so far. int move = 1; // moves so far. int r = lo; // current row of Kn. int r = lo; // current row of Kn. int c = lo; // current column of Kn. int c = lo; // current column of Kn. B[r][c] = 1; B[r][c] = 1; while (r != UNDEFINED) { while (r != UNDEFINED) { /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */...... if (r != UNDEFINED) { if (r != UNDEFINED) { move++; move++; B[r][c] = move; B[r][c] = move; } }}

11 CS 100Lecture 1711 Better Solve { /* Initial configuration. */ /* Initial configuration. */ int move = 0; // moves so far. int move = 0; // moves so far. int r = lo; // current row of Kn. int r = lo; // current row of Kn. int c = lo; // current column of Kn. int c = lo; // current column of Kn. while (r != UNDEFINED) { while (r != UNDEFINED) { move++; move++; B[r][c] = move; B[r][c] = move; /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */...... }}

12 CS 100Lecture 1712 Choosing a Neighbor to Visit /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ int unvisitedR = UNDEFINED; int unvisitedC = UNDEFINED; for (int k = 0; k < 8; k++) { int Nrow = _____________________________; int Ncol = _____________________________; if ( B[Nrow][Ncol] == Blank ){ unvisitedR = Nrow; unvisitedC = Ncol; }} r = unvisitedR; c = unvisitedC;

13 CS 100Lecture 1713 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}; Kn 1 0 7 2 3 4 56

14 CS 100Lecture 1714 Boundary Conditions n where X can be any non-zero value Must revise Initialize method to fill in border. (Exercise) Must revise Initialize method to fill in border. (Exercise) x x x x x x 0 1 2 3 4 5 6 7 8 9 10 11 x x x x x x x 0 1 2 3 4 5 6 7 8 9 10 11 x x x x x x

15 CS 100Lecture 1715 Representation Revisited Static class declarations visible to all methods. 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 [N][N]; /* 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; /* is offset to neighbor k. */ final static int deltaR[] = {-1,-2,-2,-1, 1, 2,2,1}; final static int deltaC[] = {2, 1,-1,-2,-2,-1,1,2};

16 CS 100Lecture 1716 Improvement Using Heuristic /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ int fewest = 9; // min unvisited neighbors int neighborR = UNDEFINED; int neighborC = UNDEFINED; for (int k = 0; k < 8; k++) { int Nrow = r + deltaR[k]; int Ncol = c + deltaC[k]; if ( B[Nrow][Ncol] == Blank ){ int n = unvisited(Nrow, Ncol); int n = unvisited(Nrow, Ncol); if (n < fewest ) { if (n < fewest ) { neighborR = Nrow; neighborC = Ncol; neighborR = Nrow; neighborC = Ncol; fewest = n; fewest = n; } } } r = neighborR; c = neighborC; n Go where the options are running out, i.e., go to the unvisited neighbor with the fewest unvisited neighbors.

17 CS 100Lecture 1717 Unvisited /* == the number of neighbors of square that are unvisited. */ static int unvisited(int r, int c) { int count = 0; // # unvisited neighs. for (int k = 0; k < 8; k++) { int Nrow = r + deltaR[k]; int Ncol = c + deltaC[k]; if ( B[Nrow][Ncol] == Blank ) count++; } return count; }


Download ppt "CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional."

Similar presentations


Ads by Google