Sudoku Solver

Suduko Solver (Java)

public class Main
{
	public static void main(String[] args) {
	     int[][] board = new int[][]{
                {3, 0, 6, 5, 0, 8, 4, 0, 0},
                {5, 2, 0, 0, 0, 0, 0, 0, 0},
                {0, 8, 7, 0, 0, 0, 0, 3, 1},
                {0, 0, 3, 0, 1, 0, 0, 8, 0},
                {9, 0, 0, 8, 6, 3, 0, 0, 5},
                {0, 5, 0, 0, 9, 0, 6, 0, 0},
                {1, 3, 0, 0, 0, 0, 2, 5, 0},
                {0, 0, 0, 0, 0, 0, 0, 7, 4},
                {0, 0, 5, 2, 0, 6, 3, 0, 0}
        };
        solve(board, 0, 0);
	}
	
	public static void solve(int[][] board, int i, int j) {
	    if(i==board.length) {
	        display(board);
	        return;
	    }
	    
	    int ni=0;
	    int nj=0;
	    
	    if(j==board[0].length-1) {
	        ni=i+1;
	        nj=0;
	    }
	    else {
	        ni=i;
	        nj=j+1;
	    }
	    
	    // pehle se value hai
	    if(board[i][j]!=0) {
	        solve(board, ni, nj);
	    }
	    // we need to put value
	    else {
	        for(int po=1; po<=9; po++) {
	            if(isSafe(board, i, j, po)) {
	                board[i][j] = po;
	                solve(board, ni, nj);
	                board[i][j] = 0;
	            }
	        }
	    }
	}
	
	public static void display(int[][] board) {
	    for(int i=0; i<board.length; i++) {
	        for(int j=0; j<board.length; j++) {
                System.out.print(board[i][j] + " ");	            
	        }
	        System.out.println();
	    }
	}
	
	public static boolean isSafe(int[][] board, int row, int col, int num) {
        // check the row
        for(int i=0; i<board.length; i++) {
            if(board[row][i] == num) {
                return false;
            }
        }
        
        // check the col
        for(int i=0; i<board.length; i++) {
            if(board[i][col] == num) {
                return false;
            }
        }
        
        // check own matrix
        int rowStart = row/3 * 3;
        int colStart = col/3 * 3;
        
        for(int r=0; r<3; r++) {
            for(int c=0; c<3; c++) {
                if(board[rowStart+r][colStart+c]==num) {
                    return false;
                }
            }
        }
        
        return true;
	}
}

Suduko Solver (Python)

def solve(board, i, j):
    if i==len(board):
        display(board)
        return
    
    ni=0
    nj=0
    
    if j==len(board)-1:
        ni=i+1
        nj=0
    else:
        ni=i
        nj=j+1
    
    if board[i][j]!=0:
        solve(board, ni, nj)
    else:
        for po in range(1,9):
            if isSafe(board, i, j, po):
                board[i][j] = po
                solve(board, ni, nj)
                board[i][j] = 0
                
def display(board):
    for r in board:
        for c in r:
            print(c,end = " ")
        print()
        
def isSafe(board, row, col, num):
    # check the row
    for i in range(len(board)):
        if board[row][i] == num:
            return False
    
    # check the col
    for i in range(len(board)):
        if board[i][col] == num:
            return False
    
    # check the matrix
    rowStart = int((row/3) * 3)
    colStart = int((col/3) * 3)
        
    for r in range(2):
        for c in range(2):
            if board[rowStart+r][colStart+c]==num:
                return False
                
    return True

Leave a Comment