[leetcode] Sudoku Solver

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

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.

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

思路:類似於N-Queens的回溯方案,對於每個空格,依次填入每個數字,如果合法,繼續下去,直至填滿。

注意:此時isValid只需比較當前填入的值所在地區的合法性!

 

/** * http://blog.csdn.net/zxzxy1988/article/details/8586289 * http://blog.csdn.net/linhuanmars/article/details/20748761 * @author Dong Jiang * */public class Solution {    public void solveSudoku(char[][] board) {        solve(board);    }    public boolean solve(char[][] board) {        for (int i = 0; i < 9; i++) {            for (int j = 0; j < 9; j++) {                if (board[i][j] == ‘.‘) {                    for (int k = 1; k <= 9; k++) {                        board[i][j] = (char) (‘0‘ + k);                        if (isValid(board, i, j) && solve(board))                            return true;                        board[i][j] = ‘.‘;                    }                    return false;                }            }        }        return true;    }    private boolean isValid(char[][] board, int x, int y) {        int i, j;        for (i = 0; i < 9; i++)            if (i != x && board[i][y] == board[x][y])                return false;        for (j = 0; j < 9; j++)            if (j != y && board[x][j] == board[x][y])                return false;        for (i = 3 * (x / 3); i < 3 * (x / 3 + 1); i++)            for (j = 3 * (y / 3); j < 3 * (y / 3 + 1); j++)                if (i != x && j != y && board[i][j] == board[x][y])                    return false;        return true;    }    public static void main(String[] args) {        char[][] board = { { ‘5‘, ‘3‘, ‘.‘, ‘.‘, ‘7‘, ‘.‘, ‘.‘, ‘.‘, ‘.‘ },                { ‘6‘, ‘.‘, ‘.‘, ‘1‘, ‘9‘, ‘5‘, ‘.‘, ‘.‘, ‘.‘ }, { ‘.‘, ‘9‘, ‘8‘, ‘.‘, ‘.‘, ‘.‘, ‘.‘, ‘6‘, ‘.‘ },                { ‘8‘, ‘.‘, ‘.‘, ‘.‘, ‘6‘, ‘.‘, ‘.‘, ‘.‘, ‘3‘ }, { ‘4‘, ‘.‘, ‘.‘, ‘8‘, ‘.‘, ‘3‘, ‘.‘, ‘.‘, ‘1‘ },                { ‘7‘, ‘.‘, ‘.‘, ‘.‘, ‘2‘, ‘.‘, ‘.‘, ‘.‘, ‘6‘ }, { ‘.‘, ‘6‘, ‘.‘, ‘.‘, ‘.‘, ‘.‘, ‘2‘, ‘8‘, ‘.‘ },                { ‘.‘, ‘.‘, ‘.‘, ‘4‘, ‘1‘, ‘9‘, ‘.‘, ‘.‘, ‘5‘ }, { ‘.‘, ‘.‘, ‘.‘, ‘.‘, ‘8‘, ‘.‘, ‘.‘, ‘7‘, ‘9‘ } };        new Solution().solveSudoku(board);    }}

參考

http://blog.csdn.net/linhuanmars/article/details/20748761

http://blog.csdn.net/zxzxy1988/article/details/8586289

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.