Giter VIP home page Giter VIP logo

study's Introduction

Study

2020-02-17

2020-02-19

class Solution {

    public static void main(String[] args) {
        Solution s = new Solution();
        char[][] board = new char[][]
                {{'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'}};

        s.solveSudoku(board);
    }

    public void solveSudoku(char[][] board) {
        bk(board, 0, 0 );
    }

    public boolean bk(char[][] board, int row, int col) {
        if(row > 9) {
            return true;
        }
        if(col > 8) {
            return bk(board, row+1, 0);
        }
        if(board[row][col] != '.') {
            return bk(board, row, col + 1);
        }
        char[] map = new char[] {'0','1', '2', '3','4','5','6','7','8','9'};
        for(int i = 1; i < 10; i++) {
            if(check(board, map[i] , row, col)) {
                board[row][col] = map[i];
                if(!bk(board, row, col + 1)) {
                    board[row][col] = '.';
                } f

            }
        }

        return false;
    }

    public boolean check(char[][] board, char val, int row, int col) {
        if(!checkBox(board, val, row, col)) {
            return false;
        }

        return checkEach(board, val, row, col, +1, 0) && checkEach(board, val, row, col, 0, +1);
    }

    public boolean checkBox(char[][] board,
                            int val,
                            int row,
                            int col) {
        int[][] boxRow = new int[][]{
                {0,0,0, 0,0,0,0,0,0}
                ,{0,0,0, 0,0,0,0,0,0}
                ,{0,0,0, 0,0,0,0,0,0}
                ,{2,2,2,2,2,2,2,2,2}
                ,{2,2,2,2,2,2,2,2,2}
                ,{2,2,2,2,2,2,2,2,2}
                ,{6,6,6,6,6,6,6,6,6}
                ,{6,6,6,6,6,6,6,6,6}
                ,{6,6,6,6,6,6,6,6,6}
        };
        int[][] boxCol = new int[][]{
                {0,0,0,3,3,3,6,6,6}
                ,{0,0,0,3,3,3,6,6,6}
                ,{0,0,0,3,3,3,6,6,6}
                ,{0,0,0,3,3,3,6,6,6}
                ,{0,0,0,3,3,3,6,6,6}
                ,{0,0,0,3,3,3,6,6,6}
                ,{0,0,0,3,3,3,6,6,6}
                ,{0,0,0,3,3,3,6,6,6}
                ,{0,0,0,3,3,3,6,6,6}
        };

        int iE = boxRow[row][col]+3;
        int jE = boxCol[row][col]+3;
        for(int i = boxRow[row][col]; i < iE; i++ ) {
            for(int j = boxCol[row][col] ; j< jE; j++ ) {
                if(board[i][j] == val) {
                    return false;
                }
            }
        }
        return true;
    }

    public boolean checkEach(char[][] board,
                             char val,
                             int row,
                             int col,
                             int plusRow,
                             int plusCol) {
        if(row > board.length -1 ||
                row < 0 ||
                col > board[row].length -1 ||
                col < 0) {
            return true;
        }

        if(board[row][col] == val) {
            return false;
        }

        return checkEach(board, val, row+plusRow, col + plusCol, plusRow, plusCol);
    }


} 

study's People

Contributors

kwangyong-park avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.