LeetCode: Sudoku Solver (数独求解)

采用backtracking求解:首先找到一个需要求解的位置,对这个位置尝试求解,如果求解成功则返回,否则恢复这个位置之前的状态,退回一部,尝试其他数字。递归地调用这个过程进行求解。

Java Code:

public class Solution {
  public void solveSudoku(char[][] board) {
    // Start typing your Java solution below
    // DO NOT write main() function
    solveSudoku(board, 0, 0);
  }

  private boolean solveSudoku(char[][] board, int i, int j) {
    int[] position = findPosition(board, i, j);
    if (position == null) {
      return true;
    } else {
      i = position[0];
      j = position[1];
    }
    for (int num = 1; num <= 9; num++) {
      board[i][j] = Character.forDigit(num, 10);
      if (checkValidity(board, i, j)) {
        if (solveSudoku(board, i, j)) {
          return true;
        }
      }
    }
    board[i][j] = '.';
    return false;
  }

  private int[] findPosition(char[][] board, int i, int j) {
    int ii = i;
    int jj = j;
    for (; ii < board.length; ii++) {
      if (ii == i) {
        jj = j;
      } else {
        jj = 0;
      }
      for (; jj < board[ii].length; jj++) {
        char c = board[ii][jj];
        if (!Character.isDigit(c)) {
          return new int[]{ii, jj};
        }
      }
    }
    return null;
  }

  private boolean checkValidity(char[][] board, int i, int j) {
    boolean[] a = new boolean[board.length];
    java.util.Arrays.fill(a, false);
    for (int x = 0; x < board[i].length; x++) {
      char c = board[i][x];
      if (Character.isDigit(c)) {
        if (a[c - '0' - 1] == false) {
          a[c - '0' - 1] = true;
        } else {
          return false;
        }
      }
    }
    java.util.Arrays.fill(a, false);
    for (int y = 0; y < board.length; y++) {
      char c = board[y][j];
      if (Character.isDigit(c)) {
        if (a[c - '0' - 1] == false) {
          a[c - '0' - 1] = true;
        } else {
          return false;
        }
      }
    }
    java.util.Arrays.fill(a, false);
    int x = 0;
    int y = 0;
    if (i < 3) {
      x = 0;
    } else if (i < 6) {
      x = 3;
    } else {
      x = 6;
    }
    if (j < 3) {
      y = 0;
    } else if (j < 6) {
      y = 3;
    } else {
      y = 6;
    }
    int xx = x;
    int yy = y;
    int xBound = x + 3;
    int yBound = y + 3;
    for (x = xx; x < xBound; x++) {
      for (y = yy; y < yBound; y++) {        
        char c = board[x][y];
        if (Character.isDigit(c)) {
          if (a[c - '0' - 1] == false) {
            a[c - '0' - 1] = true;
          } else {
            return false;
          }
        }
      }
    }
    return true;
  }

  public void test() {
    char[][] board = {{'.','.','9','7','4','8','.','.','.'},
                      {'7','.','.','.','.','.','.','.','.'},
                      {'.','2','.','1','.','9','.','.','.'},
                      {'.','.','7','.','.','.','2','4','.'},
                      {'.','6','4','.','1','.','5','9','.'},
                      {'.','9','8','.','.','.','3','.','.'},
                      {'.','.','.','8','.','3','.','2','.'},
                      {'.','.','.','.','.','.','.','.','6'},
                      {'.','.','.','2','7','5','9','.','.'}};
    solveSudoku(board);
    for (int i = 0; i < 9; i++) {
      for (int j = 0; j < 9; j++) {
        System.out.print(board[i][j] + " ");
      }
      System.out.println();
    }
  }

  public static void main(String[] args) {
    new Solution().test();
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值