題目
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
Example 1:
Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
Example 2:
Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
- The given board contain only digits 1-9 and the character '.'.
- The given board size is always 9x9.
解題思路
玩過數(shù)獨(dú)的人都應(yīng)該清楚,本題就是要求對(duì)給出的9*9數(shù)組進(jìn)行驗(yàn)證祝沸,如果滿足:
1胳喷、橫向數(shù)字只有1-9且不重復(fù)("."為補(bǔ)充占位晌坤,后續(xù)不再解釋)
2晨川、豎向數(shù)字只有1-9且不重復(fù)
3刷喜、把99的矩陣分成9個(gè)33的矩陣荣回,每個(gè)矩陣中數(shù)字只有1-9且不重復(fù)
依照上面的解題思路侄榴,就是要對(duì)9*9的矩陣進(jìn)行3次判斷雹锣,全部校驗(yàn)通過則通過,反之不通過癞蚕。
代碼實(shí)現(xiàn)
以下為具體代碼實(shí)現(xiàn)蕊爵,僅供參考
public class Main {
public static void main(String[] args) {
Main main = new Main();
char[][] arr = main.init();
main.isValidSudoku(arr);
}
// 包含元素集合
private Character[] demo = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '.'};
public boolean isValidSudoku(char[][] board) {
if (checkHorizontal(board) && checkVertical(board) && checkGrid(board)) {
System.out.println(true);
return true;
} else {
System.out.println(false);
return false;
}
}
/**
* 初始化數(shù)組
*
* @return
*/
private char[][] init() {
char[][] arr = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
};
// char[][] arr = {{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
// {'6', '.', '.', '1', '9', '5', '.', '.', '.'},
// {'.', '9', '8', '.', '.', '.', '.', '6', '.'},
// {'8', '.', '.', '.', '6', '.', '.', '.', '3'},
// {'4', '.', '.', '8', '.', '3', '.', '.', '1'},
// {'7', '.', '.', '.', '2', '.', '.', '.', '6'},
// {'.', '6', '.', '.', '.', '.', '2', '8', '.'},
// {'.', '.', '.', '4', '1', '9', '.', '.', '5'},
// {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
return arr;
}
/**
* 判斷橫向9個(gè)數(shù)字是否有重復(fù)
*
* @param arr
* @return
*/
private boolean checkHorizontal(char[][] arr) {
List<Character> strings = Arrays.asList(demo);
Set<Character> set = new HashSet<>();
for (char[] a : arr) {
set.clear();
for (char aa : a) {
if (aa == '.') {
continue;
} else if (strings.contains(aa) && !set.contains(aa)) {
set.add(aa);
} else {
return false;
}
}
}
return true;
}
/**
* 判斷豎向9個(gè)數(shù)字是否有重復(fù)
*
* @param arr
* @return
*/
private boolean checkVertical(char[][] arr) {
char[][] tem = new char[9][9];
// 將原9*9矩陣翻轉(zhuǎn)
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
tem[j][i] = arr[i][j];
}
}
return checkHorizontal(tem);
}
/**
* 判斷3*3的9個(gè)數(shù)字是否有重復(fù)
*
* @param arr
* @return
*/
private boolean checkGrid(char[][] arr) {
char[][] tem = new char[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
// [i / 3 * 3 + j / 3]:確認(rèn)當(dāng)前元素屬于3*3矩陣中第幾塊
// [j % 3 * 3 + i % 3]:確認(rèn)當(dāng)前元素在所屬元素塊中是第幾個(gè)元素
tem[i / 3 * 3 + j / 3][j % 3 * 3 + i % 3] = arr[i][j];
}
}
return checkHorizontal(tem);
}
}