zoukankan      html  css  js  c++  java
  • Leetcode-Sudoku Solver

    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...

    Analysis:

    Use three list of HashSet<Integer> to indicate the current availability of each row, col and block. Use recursive method. At each point, iterate through every available number, take the number out from the avaialble sets of the row, col and block, and then go to next point.

    Solution:

     1 public class Solution {
     2     public void solveSudoku(char[][] board) {
     3         if (board.length!=9) return;
     4         if (board[0].length!=9) return;
     5 
     6         //Construct the available set for each point.
     7         List<Set<Integer>> rowAva = new ArrayList<Set<Integer>>();
     8         List<Set<Integer>> colAva = new ArrayList<Set<Integer>>();
     9         List<Set<Integer>> areaAva = new ArrayList<Set<Integer>>();
    10         Set<Integer> base = new HashSet<Integer>();
    11         for (int i=1;i<=9;i++) base.add(i);
    12         for (int i=0;i<9;i++){
    13             Set<Integer> temp = new HashSet<Integer>();
    14             temp.addAll(base);
    15             rowAva.add(temp);
    16             temp = new HashSet<Integer>();
    17             temp.addAll(base);
    18             colAva.add(temp);
    19             temp = new HashSet<Integer>();
    20             temp.addAll(base);
    21             areaAva.add(temp);
    22         }
    23         //Preprocessing: take out each exsiting number from the ava set of the row and the col.
    24         for (int i=0;i<9;i++)
    25             for (int j=0;j<9;j++)
    26                 if (board[i][j]!='.'){
    27                     int val = board[i][j]-'0';
    28                     rowAva.get(i).remove(val);
    29                     colAva.get(j).remove(val);
    30                     int ind = (i/3)*3+(j/3);
    31                     areaAva.get(ind).remove(val);
    32                 }
    33     
    34         //Recursively find out the solution.
    35         solveSudokuRecur(board,0,0,rowAva,colAva,areaAva);
    36     }
    37 
    38     public boolean solveSudokuRecur(char[][] board, int x, int y, List<Set<Integer>> rowAva, List<Set<Integer>> colAva, List<Set<Integer>> areaAva){
    39         if (x==9) return true;
    40 
    41         char cur = board[x][y];
    42         int nextX = (x*9+y+1)/9;
    43         int nextY = (x*9+y+1)%9;
    44         //it is filled.
    45         if (cur!='.')  return solveSudokuRecur(board,nextX,nextY,rowAva,colAva,areaAva);
    46         //if it is not filled, then try to fill every possible value.
    47         int ind = (x/3)*3+(y/3);
    48         for (int i=1;i<=9;i++)
    49             if (rowAva.get(x).contains(i) && colAva.get(y).contains(i) && areaAva.get(ind).contains(i)){
    50                 board[x][y] = (char)(i+'0');
    51                 //remove i from ava sets.
    52                 rowAva.get(x).remove(i);
    53                 colAva.get(y).remove(i);
    54                 areaAva.get(ind).remove(i);                
    55                 boolean valid = solveSudokuRecur(board,nextX,nextY,rowAva,colAva,areaAva);
    56                 if (valid) return true;
    57                 //backtracking.
    58                 areaAva.get(ind).add(i);
    59                 colAva.get(y).add(i);
    60                 rowAva.get(x).add(i);
    61                 board[x][y]='.';
    62             }
    63         return false;
    64     }
    65             
    66             
    67 }
  • 相关阅读:
    如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites]
    JS 传播事件、取消事件默认行为、阻止事件传播
    脚本化CSS类-HTML5 classList属性
    offset、client、scroll开头的属性归纳总结
    使用insertBefore实现insertAdjacentHTML()
    HTML5 数据集属性dataset
    圣杯布局与双飞翼布局
    Lazyload Angular
    MongoDB学习笔记
    HTML5调用电脑摄像头拍照
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4177418.html
Copyright © 2011-2022 走看看