|
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. |
8 | | - * |
| 8 | + * <p> |
9 | 9 | * The Sudoku board could be partially filled, where empty cells are filled |
10 | 10 | * with the character '.'. |
11 | | - * |
| 11 | + * <p> |
12 | 12 | * Note: |
13 | 13 | * A valid Sudoku board (partially filled) is not necessarily solvable. Only |
14 | 14 | * the filled cells need to be validated. |
15 | | - * |
| 15 | + * <p> |
16 | 16 | * Tags: Hash table |
17 | 17 | */ |
18 | 18 | class ValidSudoku { |
19 | | - public static void main(String[] args) { |
20 | | - ValidSudoku v = new ValidSudoku(); |
21 | | - |
22 | | - char[][] board = new char[9][9]; |
23 | | - for (int i = 0; i < board.length; i++) { |
24 | | - for (int j = 0; j < board[i].length; j++) { |
25 | | - if (i == 0 && j == 0) { |
26 | | - board[i][j] = '.'; |
27 | | - } else if (i == 0 && j != 0) { |
28 | | - board[i][j] = (char)('0' + j + 1); |
29 | | - } else { |
30 | | - board[i][j] = '.'; |
31 | | - } |
32 | | - } |
| 19 | + public static void main(String[] args) { |
| 20 | + ValidSudoku v = new ValidSudoku(); |
| 21 | + |
| 22 | + char[][] board = new char[9][9]; |
| 23 | + for (int i = 0; i < board.length; i++) { |
| 24 | + for (int j = 0; j < board[i].length; j++) { |
| 25 | + if (i == 0 && j == 0) { |
| 26 | + board[i][j] = '.'; |
| 27 | + } else if (i == 0 && j != 0) { |
| 28 | + board[i][j] = (char) ('0' + j + 1); |
| 29 | + } else { |
| 30 | + board[i][j] = '.'; |
33 | 31 | } |
34 | | - v.printBoard(board); |
35 | | - System.out.println(v.isValidSudoku(board)); |
| 32 | + } |
36 | 33 | } |
37 | | - |
38 | | - public boolean isValidSudoku(char[][] board) { |
39 | | - boolean[][] row = new boolean[9][9]; |
40 | | - boolean[][] col = new boolean[9][9]; |
41 | | - boolean[][] box = new boolean[9][9]; |
42 | | - |
43 | | - for (int i = 0; i < 9; i++) { |
44 | | - for (int j = 0; j < 9; j++) { |
45 | | - if (board[i][j] != '.') { |
46 | | - int num = board[i][j] - '0' - 1; |
47 | | - int k = i / 3 * 3 + j / 3; |
48 | | - if (row[i][num] || col[j][num] || box[k][num]) { |
49 | | - return false; |
50 | | - } |
51 | | - row[i][num] = col[j][num] = box[k][num] = true; |
52 | | - } |
53 | | - } |
| 34 | + v.printBoard(board); |
| 35 | + System.out.println(v.isValidSudoku(board)); |
| 36 | + } |
| 37 | + |
| 38 | + public boolean isValidSudoku(char[][] board) { |
| 39 | + boolean[][] row = new boolean[9][9]; |
| 40 | + boolean[][] col = new boolean[9][9]; |
| 41 | + boolean[][] box = new boolean[9][9]; |
| 42 | + |
| 43 | + for (int i = 0; i < 9; i++) { |
| 44 | + for (int j = 0; j < 9; j++) { |
| 45 | + if (board[i][j] != '.') { |
| 46 | + int num = board[i][j] - '0' - 1; |
| 47 | + int k = i / 3 * 3 + j / 3; |
| 48 | + if (row[i][num] || col[j][num] || box[k][num]) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + row[i][num] = col[j][num] = box[k][num] = true; |
54 | 52 | } |
55 | | - return true; |
| 53 | + } |
56 | 54 | } |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Use three arrays of integers to do masking |
| 60 | + */ |
| 61 | + public boolean isValidSudoku2(char[][] board) { |
| 62 | + int[] row = new int[9]; |
| 63 | + int[] col = new int[9]; |
| 64 | + int[] sqr = new int[9]; |
| 65 | + |
| 66 | + for (int i = 0; i < 9; i++) { |
| 67 | + for (int j = 0; j < 9; j++) { |
| 68 | + if (board[i][j] != '.') { |
| 69 | + int num = board[i][j] - '0'; |
| 70 | + if ((row[i] & 1 << num) > 0) return false; // already in row |
| 71 | + else row[i] |= 1 << num; |
| 72 | + |
| 73 | + if ((col[j] & 1 << num) > 0) return false;// already in col |
| 74 | + else col[j] |= 1 << num; |
| 75 | + |
| 76 | + int sqrIdx = (i - i % 3) + j / 3; // note the square idx |
| 77 | + if ((sqr[sqrIdx] & 1 << num) > 0) return false; // already |
| 78 | + else sqr[sqrIdx] |= 1 << num; |
57 | 79 |
|
58 | | - /** |
59 | | - * Use three arrays of integers to do masking |
60 | | - */ |
61 | | - public boolean isValidSudoku2(char[][] board) { |
62 | | - int[] row = new int[9]; |
63 | | - int[] col = new int[9]; |
64 | | - int[] sqr = new int[9]; |
65 | | - |
66 | | - for (int i = 0; i < 9; i++){ |
67 | | - for (int j = 0; j < 9; j++) { |
68 | | - if (board[i][j] != '.') { |
69 | | - int num = board[i][j] - '0'; |
70 | | - if ((row[i] & 1 << num) > 0) return false; // already in row |
71 | | - else row[i] |= 1 << num; |
72 | | - |
73 | | - if ((col[j] & 1 << num) > 0) return false;// already in col |
74 | | - else col[j] |= 1 << num; |
75 | | - |
76 | | - int sqrIdx = (i - i % 3) + j / 3; // note the square idx |
77 | | - if ((sqr[sqrIdx] & 1 << num) > 0) return false; // already |
78 | | - else sqr[sqrIdx] |= 1 << num; |
79 | | - |
80 | | - } |
81 | | - } |
82 | 80 | } |
83 | | - return true; |
84 | | - |
| 81 | + } |
85 | 82 | } |
86 | | - |
87 | | - /** |
88 | | - * hashtable, index as key, mask as value |
89 | | - */ |
90 | | - public boolean isValidSudoku3(char[][] board) { |
91 | | - Map<Integer, Integer> row = new HashMap<Integer, Integer>(); |
92 | | - Map<Integer, Integer> col = new HashMap<Integer, Integer>(); |
93 | | - Map<Integer, Integer> sqr = new HashMap<Integer, Integer>(); |
94 | | - |
95 | | - for (int i = 0; i < board.length; i++) { |
96 | | - for (int j = 0; j < board[i].length; j++) { |
97 | | - if (board[i][j] != '.') { |
98 | | - int num = board[i][j] - '0'; |
99 | | - int rowMask = row.containsKey(i) ? row.get(i) : 0; |
100 | | - if ((rowMask & 1 << num) > 0) { |
101 | | - return false; |
102 | | - } else { |
103 | | - row.put(i, rowMask | 1 << num); |
104 | | - } |
105 | | - |
106 | | - int colMask = col.containsKey(j) ? col.get(j) : 0; |
107 | | - if ((colMask & 1 << num) > 0) { |
108 | | - return false; |
109 | | - } else { |
110 | | - col.put(j, colMask | 1 << num); |
111 | | - } |
112 | | - |
113 | | - int sqrIdx = (i - i % 3) + j / 3; |
114 | | - int sqrMask = sqr.containsKey(sqrIdx) ? sqr.get(sqrIdx) : 0; |
115 | | - if ((sqrMask & 1 << num) > 0) { |
116 | | - return false; |
117 | | - } else { |
118 | | - sqr.put(sqrIdx, sqrMask | 1 << num); |
119 | | - } |
120 | | - } |
121 | | - } |
| 83 | + return true; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * hashtable, index as key, mask as value |
| 88 | + */ |
| 89 | + public boolean isValidSudoku3(char[][] board) { |
| 90 | + Map<Integer, Integer> row = new HashMap<>(); |
| 91 | + Map<Integer, Integer> col = new HashMap<>(); |
| 92 | + Map<Integer, Integer> sqr = new HashMap<>(); |
| 93 | + |
| 94 | + for (int i = 0; i < board.length; i++) { |
| 95 | + for (int j = 0; j < board[i].length; j++) { |
| 96 | + if (board[i][j] != '.') { |
| 97 | + int num = board[i][j] - '0'; |
| 98 | + int rowMask = row.containsKey(i) ? row.get(i) : 0; |
| 99 | + if ((rowMask & 1 << num) > 0) { |
| 100 | + return false; |
| 101 | + } else { |
| 102 | + row.put(i, rowMask | 1 << num); |
| 103 | + } |
| 104 | + |
| 105 | + int colMask = col.containsKey(j) ? col.get(j) : 0; |
| 106 | + if ((colMask & 1 << num) > 0) { |
| 107 | + return false; |
| 108 | + } else { |
| 109 | + col.put(j, colMask | 1 << num); |
| 110 | + } |
| 111 | + |
| 112 | + int sqrIdx = (i - i % 3) + j / 3; |
| 113 | + int sqrMask = sqr.containsKey(sqrIdx) ? sqr.get(sqrIdx) : 0; |
| 114 | + if ((sqrMask & 1 << num) > 0) { |
| 115 | + return false; |
| 116 | + } else { |
| 117 | + sqr.put(sqrIdx, sqrMask | 1 << num); |
| 118 | + } |
122 | 119 | } |
123 | | - |
124 | | - return true; |
| 120 | + } |
125 | 121 | } |
126 | 122 |
|
127 | | - private void printBoard(char[][] board) { |
128 | | - for (int i = 0; i < board.length; i++) { |
129 | | - for (int j = 0; j < board[i].length; j++) { |
130 | | - System.out.print(board[i][j] + " "); |
131 | | - } |
132 | | - System.out.println(); |
133 | | - } |
134 | | - System.out.println("-----------------"); |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + private void printBoard(char[][] board) { |
| 127 | + for (int i = 0; i < board.length; i++) { |
| 128 | + for (int j = 0; j < board[i].length; j++) { |
| 129 | + System.out.print(board[i][j] + " "); |
| 130 | + } |
| 131 | + System.out.println(); |
135 | 132 | } |
| 133 | + System.out.println("-----------------"); |
| 134 | + } |
136 | 135 | } |
0 commit comments