首页 > 其他 > 详细

Leetcode-Sudoku Solver

时间:2014-12-22 08:13:01      阅读:351      评论:0      收藏:0      [点我收藏+]

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 }

 

Leetcode-Sudoku Solver

原文:http://www.cnblogs.com/lishiblog/p/4177418.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!