|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * 37. Sudoku Solver |
| 5 | + * |
4 | 6 | * Write a program to solve a Sudoku puzzle by filling the empty cells.
|
5 | 7 | * Empty cells are indicated by the character '.'.
|
6 | 8 | * You may assume that there will be only one unique solution.
|
7 |
| - * A sudoku puzzle... |
8 |
| - * ...and its solution numbers marked in red. |
9 | 9 | */
|
10 | 10 | public class _37 {
|
11 | 11 |
|
12 |
| - public void solveSudoku(char[][] board) { |
13 |
| - if (board == null || board.length == 0) { |
14 |
| - return; |
| 12 | + public static class Solution1 { |
| 13 | + public void solveSudoku(char[][] board) { |
| 14 | + if (board == null || board.length == 0) { |
| 15 | + return; |
| 16 | + } |
| 17 | + solve(board); |
15 | 18 | }
|
16 |
| - solve(board); |
17 |
| - } |
18 | 19 |
|
19 |
| - private boolean solve(char[][] board) { |
20 |
| - for (int i = 0; i < board.length; i++) { |
21 |
| - for (int j = 0; j < board[0].length; j++) { |
22 |
| - if (board[i][j] == '.') { |
23 |
| - for (char c = '1'; c <= '9'; c++) { |
24 |
| - //try 1 to 9 |
25 |
| - if (isValid(board, i, j, c)) { |
26 |
| - board[i][j] = c; |
| 20 | + private boolean solve(char[][] board) { |
| 21 | + for (int i = 0; i < board.length; i++) { |
| 22 | + for (int j = 0; j < board[0].length; j++) { |
| 23 | + if (board[i][j] == '.') { |
| 24 | + for (char c = '1'; c <= '9'; c++) { |
| 25 | + //try 1 to 9 |
| 26 | + if (isValid(board, i, j, c)) { |
| 27 | + board[i][j] = c; |
27 | 28 |
|
28 |
| - if (solve(board)) { |
29 |
| - return true; |
30 |
| - } else { |
31 |
| - board[i][j] = '.';//recover it to be '.' |
| 29 | + if (solve(board)) { |
| 30 | + return true; |
| 31 | + } else { |
| 32 | + board[i][j] = '.';//recover it to be '.' |
| 33 | + } |
32 | 34 | }
|
33 | 35 | }
|
| 36 | + return false; |
34 | 37 | }
|
35 |
| - return false; |
36 | 38 | }
|
37 | 39 | }
|
| 40 | + return true; |
38 | 41 | }
|
39 |
| - return true; |
40 |
| - } |
41 | 42 |
|
42 |
| - private boolean isValid(char[][] board, int row, int col, char c) { |
43 |
| - for (int i = 0; i < 9; i++) { |
44 |
| - if (board[i][col] != '.' && board[i][col] == c) { |
45 |
| - return false;//check row |
46 |
| - } |
47 |
| - if (board[row][i] != '.' && board[row][i] == c) { |
48 |
| - return false;//check column |
49 |
| - } |
50 |
| - if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] != '.' && board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) { |
51 |
| - return false; //check 3*3 block |
| 43 | + private boolean isValid(char[][] board, int row, int col, char c) { |
| 44 | + for (int i = 0; i < 9; i++) { |
| 45 | + if (board[i][col] != '.' && board[i][col] == c) { |
| 46 | + return false;//check row |
| 47 | + } |
| 48 | + if (board[row][i] != '.' && board[row][i] == c) { |
| 49 | + return false;//check column |
| 50 | + } |
| 51 | + if (board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] != '.' && board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) { |
| 52 | + return false; //check 3*3 block |
| 53 | + } |
52 | 54 | }
|
| 55 | + return true; |
53 | 56 | }
|
54 |
| - return true; |
55 | 57 | }
|
56 | 58 |
|
57 | 59 | }
|
0 commit comments