Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Intro_Java_brief_Liang2011.pdf
Скачиваний:
194
Добавлен:
26.03.2016
Размер:
10.44 Mб
Скачать

244 Chapter 7

Multidimensional Arrays

 

The program prompts the user to enter the number of points (lines 6–7). The points are read

 

from the console and stored in a two-dimensional array named points (lines 12–15). The

 

program uses variable shortestDistance (line 19) to store the distance between two near-

 

est points, and the indices of these two points in the points array are stored in p1 and p2

 

(line 18).

 

For each point at index i, the program computes the distance between points[i] and

 

points[j] for all j > i (lines 23–34). Whenever a shorter distance is found, the variable

 

shortestDistance and p1 and p2 are updated (lines 28–32).

 

The distance between two points (x1, y1) and (x2, y2) can be computed using the

 

formula 21x2 - x122 + 1y2 - y122 (lines 43–46).

 

The program assumes that the plane has at least two points. You can easily modify the pro-

 

gram to handle the case if the plane has zero or one point.

multiple closest pairs

Note that there might be more than one closest pair of points with the same minimum dis-

 

tance. The program finds one such pair. You may modify the program to find all closest pairs

 

in Programming Exercise 7.8.

 

Tip

input file

It is cumbersome to enter all points from the keyboard. You may store the input in a file, say

 

FindNearestPoints.txt, and compile and run the program using the following command:

 

java FindNearestPoints < FindNearestPoints.txt

Video Note

Sudoku

fixed cells free cells

7.7 Problem: Sudoku

This book teaches how to program using a wide variety of problems with various levels of difficulty. We use simple, short, and stimulating examples to introduce programming and problem-solving techniques and use interesting and challenging examples to motivate students. This section presents an interesting problem of a sort that appears in the newspaper every day. It is a number-placement puzzle, commonly known as Sudoku. This is a very challenging problem. To make it accessible to the novice, this section presents a solution to a simplified version of the Sudoku problem, which is to verify whether a solution is correct. The complete solution for solving the Sudoku problem is presented in Supplement VII.A.

Sudoku is a 9 * 9 grid divided into smaller 3 * 3 boxes (also called regions or blocks), as shown in Figure 7.4(a). Some cells, called fixed cells, are populated with numbers from 1 to 9. The objective is to fill the empty cells, also called free cells, with numbers 1 to 9 so that every row, every column, and every 3 * 3 box contains the numbers 1 to 9, as shown in Figure 7.4(b).

5

3

 

7

 

 

5

3

4

6

7

8

9

1

2

6

 

1

9

5

 

6

7

2

1

9

5

3

4

8

 

9

8

 

6

 

1

9

8

3

4

2

5

6

7

8

 

 

6

 

3

8

5

9

7

6

1

4

2

3

4

 

8

 

3

1

Solution

2

6

8

5

3

7

9

1

 

 

4

7

 

 

2

 

6

7

1

3

9

2

4

8

5

6

 

6

 

 

 

 

9

6

1

5

3

7

2

8

4

 

 

4

1

9

5

2

8

7

4

1

9

6

3

5

 

 

 

8

7

9

3

4

5

2

8

6

1

7

9

 

 

(a) Puzzle

 

 

 

 

(b) Solution

 

 

 

FIGURE 7.4 The Sudoku puzzle in (a) is solved in (b).

 

 

 

 

 

 

 

 

 

 

7.7

Problem: Sudoku 245

For convenience, we use value 0 to indicate a free cell, as shown in Figure 7.5(a). The grid

representing a grid

can be naturally represented using a two-dimensional array, as shown in Figure 7.5(a).

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5

3

0

0

7

0

0

0

0

 

int[][] grid =

 

 

 

6

0

0

1

9

5

0

0

0

 

{{5, 3, 0, 0, 7, 0, 0, 0, 0},

 

 

 

0

9

8

0

0

0

0

6

0

 

{6, 0, 0, 1, 9, 5, 0, 0, 0},

 

 

 

 

{0, 9, 8, 0, 0, 0, 0, 6, 0},

 

 

 

8

0

0

0

6

0

0

0

3

 

 

 

 

 

{8, 0, 0, 0, 6, 0, 0, 0, 3},

 

 

 

4

0

0

8

0

3

0

0

1

 

{4, 0, 0, 8, 0, 3, 0, 0, 1},

 

 

 

7

0

0

0

2

0

0

0

6

 

{7, 0, 0, 0, 2, 0, 0, 0, 6},

 

 

 

 

{0, 6, 0, 0, 0, 0, 2, 8, 0},

 

 

 

0

6

0

0

0

0

0

0

0

 

 

 

 

 

{0, 0, 0, 4, 1, 9, 0, 0, 5},

 

 

 

0

0

0

4

1

9

0

0

5

 

{0, 0, 0, 0, 8, 0, 0, 7, 9}

 

 

 

0

0

0

0

8

0

0

7

9

 

};

 

 

 

 

 

 

 

(a)

 

 

 

 

 

(b)

 

 

FIGURE 7.5 A grid can be represented using a two-dimensional array.

To find a solution for the puzzle we must replace each 0 in the grid with an appropriate number from 1 to 9. For the solution in Figure 7.4(b), the grid should be as shown in Figure 7.6.

A solution grid is

{{5, 3, 4, 6, 7, 8, 9, 1, 2}, {6, 7, 2, 1, 9, 5, 3, 4, 8}, {1, 9, 8, 3, 4, 2, 5, 6, 7}, {8, 5, 9, 7, 6, 1, 4, 2, 3}, {4, 2, 6, 8, 5, 3, 7, 9, 1}, {7, 1, 3, 9, 2, 4, 8, 5, 6}, {9, 6, 1, 5, 3, 7, 2, 8, 4}, {2, 8, 7, 4, 1, 9, 6, 3, 5}, {3, 4, 5, 2, 8, 6, 1, 7, 9} };

FIGURE 7.6 A solution is stored in grid.

A simplified version of the Sudoku problem is to check the validity of a solution. The program in Listing 7.4 prompts the user to enter a solution and reports whether it is valid.

LISTING 7.4 CheckSudokuSolution.java

1 import java.util.Scanner;

2

3 public class CheckSudokuSolution {

4 public static void main(String[] args) {

5// Read a Sudoku solution

6 int[][] grid = readASolution();

7

8 System.out.println(isValid(grid) ? "Valid solution" :

9 "Invalid solution");

10 }

11

12/** Read a Sudoku solution from the console */

13public static int[][] readASolution() {

14// Create a Scanner

15Scanner input = new Scanner(System.in);

read input

solution valid?

read solution

246 Chapter 7

Multidimensional Arrays

 

16

 

 

 

 

 

 

 

 

 

 

 

 

 

 

17

 

System.out.println("Enter a Sudoku puzzle solution:");

 

18

 

int[][] grid = new int[9][9];

 

19

 

for (int i = 0; i < 9; i++)

 

20

 

for (int j = 0; j < 9; j++)

 

21

 

grid[i][j] = input.nextInt();

 

22

 

 

 

 

 

 

 

 

 

 

 

 

 

 

23

 

return grid;

 

24

}

 

 

 

 

 

 

 

 

 

 

 

 

 

25

 

 

 

 

 

 

 

 

 

 

 

 

 

 

26

/** Check whether a solution is valid */

check solution

27

public static boolean isValid(int[][] grid) {

 

 

28

 

// Check whether each row has numbers 1 to 9

check rows

29

 

for (int i = 0; i < 9; i++)

 

 

 

30

 

if

(!is1To9(grid[i])

) // If grid[i] does not contain 1 to 9

 

31

 

return false;

 

32

 

 

 

 

 

 

 

 

 

 

 

 

 

 

33

 

// Check whether each column has numbers 1 to 9

check columns

34

 

for (int j = 0; j < 9; j++) {

 

 

 

35

 

// Obtain a column in the one-dimensional array

 

36

 

int[] column = new int[9];

 

37

 

for (int i = 0; i < 9; i++) {

 

38

 

column[i] = grid[i][j];

 

39

}

 

 

 

 

 

 

 

 

 

 

 

 

40

 

 

 

 

 

 

 

 

 

 

 

 

 

 

41

 

if

(!is1To9(column)

) // If column does not contain 1 to 9

 

42

 

return false;

 

43

}

 

 

 

 

 

 

 

 

 

 

 

 

44

 

 

 

 

 

 

 

 

 

 

 

 

 

 

45

 

// Check whether each 3-by-3 box has numbers 1 to 9

check small boxes

46

 

for (int i = 0; i < 3; i++) {

 

 

 

47

 

for (int j = 0; j < 3; j++) {

 

48

 

// The starting element in a small 3-by-3 box

 

49

 

int k = 0;

 

50

 

int[] list = new int[9]; // Get all numbers in the box to list

 

51

 

for (int row = i * 3; row < i * 3 + 3; row ++)

 

52

 

 

for (int column = j * 3; column < j * 3 + 3; column++)

 

53

 

 

 

list[k++] = grid[row][column];

 

54

 

 

 

 

 

 

 

 

 

 

 

 

 

 

55

 

if

(!is1To9(list)

) // If list does not contain 1 to 9

 

56

 

 

 

return false;

 

57

}

 

 

 

 

 

 

 

 

 

 

 

 

58

}

 

 

 

 

 

 

 

 

 

 

 

 

59

 

 

 

 

 

 

 

 

 

 

 

 

 

all valid

60

 

return true; // The fixed cells are valid

 

61

}

 

 

 

 

 

 

 

 

 

 

 

 

 

62

 

 

 

 

 

 

 

 

 

 

 

 

 

 

63

/** Check whether the one-dimensional array contains 1 to 9 */

contains 1 to 9 ?

64

public static boolean is1To9(int[] list) {

 

65

 

// Make a copy of the array

 

66

 

int[] temp = new int[list.length];

copy of array

67

 

System.arraycopy(list, 0, temp, 0, list.length);

 

 

68

 

 

 

 

 

 

 

 

 

 

 

 

 

 

69

 

// Sort the array

sort array

70

 

java.util.Arrays.sort(temp);

 

 

 

 

 

71

 

 

 

 

 

 

 

 

 

 

 

 

 

 

72

 

// Check whether the list contains 1, 2, 3, ..., 9

check 1 to 9

73

 

for (int i = 0; i < 9; i++)

7.7 Problem: Sudoku 247

74if (temp[i] != i + 1)

75return false;

76

77return true; // The list contains exactly 1 to 9

78}

79}

Enter a Sudoku puzzle solution: 9 6 3 1 7 4 2 5 8 1 7 8 3 2 5 6 4 9 2 5 4 6 8 9 7 3 1 8 2 1 4 3 7 5 9 6 4 9 6 8 5 2 3 1 7 7 3 5 9 6 1 8 2 4 5 8 9 7 1 3 4 6 2 3 1 7 2 4 6 9 8 5 6 4 2 5 9 8 1 7 3

Valid solution

The program invokes the readASolution() method (line 6) to read a Sudoku solution and return a two-dimensional array representing a Sudoku grid.

The isValid(grid) method (lines 27–61) checks whether every row contains numbers 1 to 9 (lines 29–31). grid is a two-dimensional array. grid[i] is a one-dimensional array for the ith row. Invoking is1To9(grid[i]) returns true if the row grid[i] contains exactly numbers from 1 to 9 (line 30).

To check whether each column in grid has numbers 1 to 9, get a column into a onedimensional array (lines 36–39) and invoke the is1To9 method to check whether it has 1 to 9 (line 41).

To check whether each small 3 * 3 box in grid has numbers 1 to 9, get a box into a onedimensional array (lines 49–53) and invoke the is1To9 method to check whether it has 1 to 9 (line 55).

How do you locate all the cells in the same box? First, locate the starting cells of the 3 * 3 boxes. They are at (3i, 3j) for i = 0, 1, 2 and j = 0, 1, 2, as illustrated in Figure 7.7.

isValid method check rows

check columns

check small boxes

grid[0][0]

grid[0][6]

grid[6][3]

The location of the starting cell for each grid is at (3*i, 3*j) for i = 0, 1, 2 and j = 0, 1, 2. For example, grid[6][3]).

FIGURE 7.7 The location of the first cell in a 3 * 3 box determines the locations of other cells in the box.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]