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
|
private boolean isValidSudoku(char[][] board) { for (int i = 0; i < board.length; i++) { if (!checkRangeValid(board, i, 0, 1, board[i].length)) { return false; } } for (int i = 0; i < board[0].length; i++) { if (!checkRangeValid(board, 0, i, board.length, 1)) { return false; } } for (int i = 0; i < board.length; i += 3) { for (int j = 0; j < board[0].length; j += 3) { if (!checkRangeValid(board, i, j, 3, 3)) { return false; } } } return true; }
private boolean checkRangeValid(char[][] board, int x, int y, int rows, int cols) { boolean[] flags = new boolean[9]; Arrays.fill(flags, false); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { char ch = board[x + i][y + j]; if (ch == '.') { continue; } int index = ch - '1'; if (flags[index]) { return false; } flags[index] = true; } } return true; }
|