• 37. Sudoku Solver


        /*
         * 37. Sudoku Solver
         * 2015.12.13 by Mingyang
         * 1.长度标准:无
         * 2.可选的范围:所有木有值得点,选取从1到9的数字
         * 3.往前走一步:如果放进去是validate的,那就放
         * 4.后退一步:若果放进去,后面是false的,就把这个点改回来
         * 5.特别的case:无了
         * 6.关于重复:无
         * 本题目的isValid是难点
         * row = i / 3 * 3; row < i / 3 * 3 + 3
         */
        public void solveSudoku(char[][] board) {
            if (board == null || board.length == 0)
                return;
            helper(board);
        }
        private boolean helper(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 num = '1'; num <= '9'; num++) {// 尝试
                            if (isValid(board, i, j, num)) {
                                board[i][j] = num;
                            if (helper(board))//这个点暂时合适,那么进入下一层
                                    return true;
                                else
                                    board[i][j] = '.';// 回退
                            }
                        }
                        return false;
                    }
                }
            }
            return true;
        }
        private boolean isValid(char[][] board, int i, int j, char c) {
            // check column
            for (int row = 0; row < 9; row++)
                if (board[row][j] == c)
                    return false;
            // check row
            for (int col = 0; col < 9; col++)
                if (board[i][col] == c)
                    return false;
            // check block
            for (int row = i / 3 * 3; row < i / 3 * 3 + 3; row++)
                for (int col = j / 3 * 3; col < j / 3 * 3 + 3; col++)
                    if (board[row][col] == c)
                        return false;
            return true;
        }
  • 相关阅读:
    快速排序算法
    HDOJ(1005) Number Sequence
    HDOJ(1004) Let the Balloon Rise
    HDOJ(1003) Max Sum
    HDOJ(1002) A + B Problem II
    HDOJ(1001) Sum Problem
    HDOJ(1000) A + B Problem
    DeepFaceLab小白入门(5):训练换脸模型!
    DeepFaceLab小白入门(4):提取人脸图片!
    DeepFaceLab小白入门(3):软件使用!
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5457399.html
Copyright © 2020-2023  润新知