37-Sudoku Solver

题目描述:

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

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

A sudoku solution must satisfy all of the following rules:

  1. Each of the digits 1-9 must occur exactly once in each row.
  2. Each of the digits 1-9 must occur exactly once in each column.
  3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.'.


A sudoku puzzle...

37-Sudoku Solver_第1张图片
...and its solution numbers marked in red.

Note:

  • The given board contain only digits 1-9 and the character '.'.
  • You may assume that the given Sudoku puzzle will have a single unique solution.
  • The given board size is always 9x9.

 

解答:

package com.jack.algorithm;

/**
 * create by jack 2019/6/30
 *
 * @author jack
 * @date: 2019/6/30 10:14
 * @Description:
 * 使用数字字符1-9填充二维数组
 */
public class SudokuSolver {

    /**
     * 37-题目描述:
     * https://leetcode.com/problems/sudoku-solver/
     *
     * @param board
     */
    public static void solveSudoku(char[][] board) {
        if (solve(board));
    }

    /**
     * 判断某一行是否已经存在字符num,存在返回true,不存在返回false
     * @param board
     * @param row
     * @param num
     * @return
     */
    private static boolean usedRow(char[][] board, int row, char num) {
        //i小于二维数组的列数
        for (int i = 0; i < board[0].length; i++) {
            if (board[row][i] == num) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断num这个字符是否在这一列被使用了,被使用了返回true,没被使用返回false
     * @param board
     * @param col
     * @param num
     * @return
     */
    private static boolean usedCol(char[][] board, int col, char num){
        for(int i=0; i

 

源码:

源码

 

你可能感兴趣的:(LeetCode,leetcode)