forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckSudokuSolution.java
More file actions
executable file
·67 lines (65 loc) · 2.09 KB
/
CheckSudokuSolution.java
File metadata and controls
executable file
·67 lines (65 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* */ package ch08;
/* */
/* */ import java.util.Scanner;
/* */
/* */ public class CheckSudokuSolution
/* */ {
/* */ public static void main(String[] args) {
/* 8 */ int[][] grid = readASolution();
/* */
/* 10 */ System.out.println(isValid(grid) ? "Valid solution" : "Invalid solution");
/* */ }
/* */
/* */
/* */
/* */
/* */ public static int[][] readASolution() {
/* 17 */ Scanner input = new Scanner(System.in);
/* */
/* 19 */ System.out.println("Enter a Sudoku puzzle solution:");
/* 20 */ int[][] grid = new int[9][9];
/* 21 */ for (int i = 0; i < 9; i++) {
/* 22 */ for (int j = 0; j < 9; j++)
/* 23 */ grid[i][j] = input.nextInt();
/* */ }
/* 25 */ return grid;
/* */ }
/* */
/* */
/* */ public static boolean isValid(int[][] grid) {
/* 30 */ for (int i = 0; i < 9; i++) {
/* 31 */ for (int j = 0; j < 9; j++)
/* 32 */ { if (grid[i][j] < 1 || grid[i][j] > 9 ||
/* 33 */ !isValid(i, j, grid))
/* 34 */ return false; }
/* 35 */ } return true;
/* */ }
/* */
/* */
/* */
/* */ public static boolean isValid(int i, int j, int[][] grid) {
/* 41 */ for (int column = 0; column < 9; column++) {
/* 42 */ if (column != j && grid[i][column] == grid[i][j])
/* 43 */ return false;
/* */ }
/* */ int row;
/* 46 */ for (row = 0; row < 9; row++) {
/* 47 */ if (row != i && grid[row][j] == grid[i][j]) {
/* 48 */ return false;
/* */ }
/* */ }
/* */
/* */
/* */
/* 54 */ for (row = i / 3 * 3; row < i / 3 * 3 + 3; row++) {
/* 55 */ for (int col = j / 3 * 3; col < j / 3 * 3 + 3; col++) {
/* 56 */ if (row != i && col != j && grid[row][col] == grid[i][j])
/* 57 */ return false;
/* */ }
/* 59 */ } return true;
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch08/CheckSudokuSolution.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/