[leetcode] Sudoku Solver

from : https://leetcode.com/problems/sudoku-solver/

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character '.'.

You may assume that there will be only one unique solution.


A sudoku puzzle...


...and its solution numbers marked in red.

public class Solution {
    public void solveSudoku(char[][] board) {
        internalSolveSudoku(board);
    }
    
    private boolean internalSolveSudoku(char[][] board) {
        for (int row = 0; row < 9; ++row) {
            for (int col = 0; col < 9; ++col) {
                if ('.' == board[row][col]) {
                    for (int i = 1; i <= 9; ++i) {
                    	
                        board[row][col] = (char) ('0' + i);
                        
                        if (isValidSudoku(board, row, col)) {
                            if (internalSolveSudoku(board)) {
                                return true;
                            }
                        }
                        
                        board[row][col] = '.';
                    }
                    
                    return false;
                }
            }
        }
        
        return true;
    }
    
	private boolean isValidSudoku(char[][] board, int x, int y) {
        int row, col;
        int v = board[x][y];
        
        // Same value in the same column?
        for (row = 0; row < 9; ++row) {
            if ((x != row) && (board[row][y] == v)) {
                return false;
            }
        }
        
        // Same value in the same row?
        for (col = 0; col < 9; ++col) {
            if ((y != col) && (board[x][col] == v)) {
                return false;
            }
        }
        
        // Same value in the 3 * 3 block it belong to?
        for (row = (x / 3) * 3; row < (x / 3 + 1) * 3; ++row) {
            for (col = (y / 3) * 3; col < (y / 3 + 1) * 3; ++col) {
                if ((x != row || y != col) && (board[row][col] == v)) {
                    return false;
                }
            }
        }
        
        return true;
    }
}

public class Solution {
    public void solveSudoku(char[][] board) {
        fillSudoku(board);
    }
    
    private boolean fillSudoku(char[][] bd) {
        for(int i=0; i<9; ++i) {
            for(int j=0; j<9; ++j) {
                if(bd[i][j] == '.') {
                    for(int k=1; k<10; ++k) {
                        bd[i][j] = (char)('0'+k);
                        if(validSudoku(bd, i, j, bd[i][j])) {
                            if(fillSudoku(bd)) {
                                return true;
                            }
                        }
                        bd[i][j] = '.';
                    }
                    return false;
                }
            }
        }
        return true;
    }
    
    private boolean validSudoku(char[][] bd, int x, int y, char k) {
        for(int i=0; i<9; ++i) {
            if(i != x && k == bd[i][y] || i != y && k == bd[x][i]) {
                return false;
            }
        }
        for(int i=x/3*3, li = i+3; i<li; ++i) {
            for(int j=y/3*3, lj = j+3; j<lj; ++j) {
                if((i != x || j != y) && k == bd[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }
    
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值