Problem Description: Given the 9x9 matrix, see whether it is a valid Sudoku, not all are filled with numbers, can be.
Algorithm analysis: This problem is judged, not difficult, effective Sudoku three sufficient conditions, rows, columns, 3*3 sub-matrix, all to meet the number can not be repeated.
1 Public BooleanIsvalidsudoku (Char[] board)2 {3 if(board = =NULL|| Board.length! = 9 | | Board[0].length! = 9)4 {5 return false;6 }7 8 //Judging Rows9 for(inti = 0; I < 9; i + +)Ten { One Boolean[] m =New Boolean[9]; A for(intj = 0; J < 9; J + +) - { - if(board[i][j]! = '. ') the { - //if (m[(int) board[i][j]]) It is wrong to write this because (int) ' 1 ' is not equal to 1. - if(M[(int) (board[i][j]-' 1 ')]) - { + return false; - } +m[(int) (board[i][j]-' 1 ')] =true; A } at } - } - - //Judging Columns - for(inti = 0; I < 9; i + +) - { in Boolean[] m =New Boolean[9]; - for(intj = 0; J < 9; J + +) to { + if(Board[j][i]! = '. ') - { the if(M[(int) (board[j][i]-' 1 ')]) * { $ return false;Panax Notoginseng } -m[(int) (board[j][i]-' 1 ')] =true; the } + } A } the + //judging the 3*3 matrix, a total of 9 - for(intk = 0; K < 9; K + +) $ { $ Boolean[] m =New Boolean[9]; - for(inti = k/3*3; I < k/3*3 + 3; i + +) - { the for(intj = k%3*3; J < k%3*3 + 3; J + +) - {Wuyi if(board[i][j]! = '. ') the { - if(M[(int) (board[i][j]-' 1 ')]) Wu { - return false; About } $m[(int) (board[i][j]-' 1 ')] =true; - } - } - } A } + the return true; -}
Valid Sudoku, whether it is a valid Sudoku