Created
January 30, 2022 08:48
-
-
Save anil477/80f3cc22e7e090172f33afa68441ca26 to your computer and use it in GitHub Desktop.
37. Sudoku Solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
// 37. Sudoku Solver | |
// https://leetcode.com/problems/sudoku-solver/ | |
public void solveSudoku(char[][] board) { | |
if(board == null || board.length == 0) | |
return; | |
solve(board); | |
} | |
public boolean solve(char[][] board){ | |
for(int i = 0; i < board.length; i++){ | |
for(int j = 0; j < board[0].length; j++){ | |
if(board[i][j] == '.'){ | |
for(char c = '1'; c <= '9'; c++){ //trial. Try 1 through 9 | |
if(isValid(board, i, j, c)){ | |
board[i][j] = c; //Put c for this cell | |
if(solve(board)) | |
return true; //If it's the solution return true | |
else | |
board[i][j] = '.'; //Otherwise go back | |
} | |
} | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
private boolean isValid(char[][] board, int row, int col, char c){ | |
int regionRow = 3 * (row / 3); //region start row | |
int regionCol = 3 * (col / 3); //region start col | |
// System.out.println("row: "+ row + " col: " + col + " regionRow: " + regionRow+ " regionCol: " + regionCol); | |
for (int i = 0; i < 9; i++) { | |
if (board[i][col] == c) return false; //check row | |
if (board[row][i] == c) return false; //check column | |
// System.out.println("row: "+ (regionRow + i / 3) + " col: " + (regionCol + i % 3)); | |
if (board[regionRow + i / 3][regionCol + i % 3] == c) return false; //check 3*3 block | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment