算法题之判断数独是否有效问题
题目描述
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。上图是一个部分填充的有效的数独。数独部分空格内已填入了数字,空白格用 ‘.’ 表示。
示例 1:
输入:
[
[“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”]
]
输出: true
示例 2:
输入:
[
[“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”]
]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。说明:一个有效的数独(部分已被填充)不一定是可解的。只需要根据以上规则,验证已经填入的数字是否有效即可。给定数独序列只包含数字 1-9 和字符 ‘.’ 。给定数独永远是 9x9 形式的。
题解
步骤图解
java代码实现
public class JudgeSudoku {
/*
* 判断一个9X9的数独是否有效:
* */
public static void main(String[] args) {
char[][] board = {
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '3', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
};
JudgeSudoku judgeSudoku = new JudgeSudoku();
boolean isValidSudoku = judgeSudoku.isValidSudoku(board);
System.out.println("该数独是否为有效的(true:有效;false:无效)");
if (isValidSudoku == true) {
System.out.println("该数独为有效的");
}else {
System.out.println("该数独为无效的");
}
}
private boolean isValidSudoku(char[][] board) {
int row = board.length;//定义数独的行数:9
int col = board.length;//定义数独的列数:9
boolean flag = true;
char[][] temp = new char[1][9];//定义一个只有一行的二维数组:用来存储下面每个小宫格的九个元素
for (int i = 0; i < 9; i++) {
flag = judgeRow(i,board,col);//判断每一行是否符合数独规定
if (!flag) {
return false;
}
flag = judgeCol(i,board,row);//判断每一列是否符合数独规定
if (!flag) {
return false;
}
}
//判断每一个小的九宫格是否符合规定
for (int i = 0; i < 9; i = i + 3) {
for (int j = 0; j < 9; j = j + 3) {
flag = judgeSudoku(i,j,board,temp);
if (!flag) {
return false;
}
}
}
//如果上面都判断通过,则说明此数独为有效的,直接返回true表示有效
return true;
}
private boolean judgeSudoku(int row,int col,char[][] board,char[][] temp) {
int count = 0;
for (int i = row; i < row + 3; i++) {
for (int j = col; j < col + 3; j++) {
temp[0][count++] = board[i][j];
}
}
return judgeRow(0,temp,9);
}
//暴力匹配法判断每一行是否存在相同的的值
private boolean judgeRow(int row, char[][] board, int col) {
for (int j = 0; j < col; j++) {
for (int k = j+1; k < col; k++) {
if (board[row][j] == board[row][k] && board[row][j] != '.') {
return false;
}
}
}
return true;
}
//暴力匹配法判断每一列是否存在相同的的值
private boolean judgeCol(int col, char[][] board, int row) {
for (int j = 0; j < row; j++) {
for (int k = j+1; k < row; k++) {
if (board[j][col] == board[k][col] && board[j][col] != '.') {
return false;
}
}
}
return true;
}
}