Skip to content

Commit c6c64be

Browse files
committed
update 37 Sudoku Solver
1 parent e5dfcb1 commit c6c64be

File tree

5 files changed

+141
-85
lines changed

5 files changed

+141
-85
lines changed

Combination/37_SudokuSolver.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* @Author: [email protected]
3+
* @Last Modified time: 2016-07-17 13:59:43
4+
*/
5+
6+
class Solution {
7+
private:
8+
bool rows_hash[9][9];
9+
bool cols_hash[9][9];
10+
bool panel_hash[9][9];
11+
const vector<int> nums_list = {0,1,2,3,4,5,6,7,8};
12+
13+
bool dfs_search(int pos, vector<vector<char>>& board){
14+
if(pos==81){
15+
return true;
16+
}
17+
int r = pos / 9, c = pos % 9;
18+
if(board[r][c] != '.'){
19+
return dfs_search(pos+1, board);
20+
}
21+
else{
22+
for(const auto& n: nums_list){
23+
if(put_num(n, r, c)){
24+
board[r][c] = '1' + n;
25+
if(dfs_search(pos+1, board)){
26+
return true;
27+
}
28+
board[r][c] = '.';
29+
backtrack(n, r, c);
30+
}
31+
}
32+
return false;
33+
}
34+
}
35+
36+
bool put_num(int num, int row, int col){
37+
int panel_pos = row / 3 * 3 + col / 3;
38+
if(rows_hash[row][num] || cols_hash[col][num] || panel_hash[panel_pos][num]){
39+
return false;
40+
}
41+
rows_hash[row][num] = cols_hash[col][num] = panel_hash[panel_pos][num] = 1;
42+
return true;
43+
}
44+
45+
void backtrack(int num, int row, int col){
46+
int panel_pos = row / 3 * 3 + col / 3;
47+
rows_hash[row][num] = cols_hash[col][num] = panel_hash[panel_pos][num] = 0;
48+
}
49+
50+
public:
51+
/*
52+
According to:
53+
https://discuss.leetcode.com/topic/27787/c-clear-solution-using-dfs-beating-90-c-coder
54+
*/
55+
void solveSudoku(vector<vector<char>>& board) {
56+
memset(rows_hash, 0, sizeof(rows_hash));
57+
memset(cols_hash, 0, sizeof(cols_hash));
58+
memset(panel_hash, 0, sizeof(panel_hash));
59+
60+
for(int i=0; i<9; i++){
61+
for(int j=0; j<9;j++){
62+
if(board[i][j] != '.'){
63+
int num = board[i][j] - '1';
64+
put_num(num, i, j);
65+
}
66+
}
67+
}
68+
dfs_search(0, board);
69+
}
70+
};

Combination/37_SudokuSolver.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
nums_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
7+
8+
def solveSudoku(self, board):
9+
""" Hash and Backtracking.
10+
11+
:type board: List[List[str]]
12+
:rtype: void Do not return anything, modify board in-place instead.
13+
"""
14+
# Pay Attention, can not define two-degree array as: [[0]*9]*9
15+
self.rows_hash, self.cols_hash = [[0] * 9 for i in range(9)], [[0] * 9 for i in range(9)]
16+
self.panel_hash = [[0] * 9 for i in range(9)]
17+
18+
# Add all existing number to hash.
19+
for i in xrange(9):
20+
for j in xrange(9):
21+
if board[i][j] != ".":
22+
self.try_num(int(board[i][j]) - 1, i, j)
23+
24+
self.dfs_search(0, board)
25+
26+
def dfs_search(self, cur, board):
27+
if cur == 81:
28+
return True
29+
r, c = cur / 9, cur % 9
30+
31+
# The existing number must be valid, because we are promised that
32+
# there will be only one unique solution.
33+
if board[r][c] != ".":
34+
return self.dfs_search(cur + 1, board)
35+
36+
else:
37+
for n in self.nums_list:
38+
if self.try_num(n - 1, r, c):
39+
board[r][c] = str(n)
40+
if self.dfs_search(cur + 1, board):
41+
return True
42+
# Remember to bacrtrack here.
43+
board[r][c] = "."
44+
self.backtrack(n - 1, r, c)
45+
return False
46+
47+
def try_num(self, num, row, col):
48+
panel_pos = row / 3 * 3 + col / 3
49+
if (self.rows_hash[row][num] or self.cols_hash[col][num] or
50+
self.panel_hash[panel_pos][num]):
51+
return False
52+
else:
53+
self.rows_hash[row][num] = 1
54+
self.cols_hash[col][num] = 1
55+
self.panel_hash[panel_pos][num] = 1
56+
return True
57+
58+
def backtrack(self, num, row, col):
59+
panel_pos = row / 3 * 3 + col / 3
60+
self.rows_hash[row][num] = 0
61+
self.cols_hash[col][num] = 0
62+
self.panel_hash[panel_pos][num] = 0
63+
64+
"""
65+
["..9748...","7........",".2.1.9...","..7...24.",".64.1.59.",".98...3..","...8.3.2.","........6","...2759.."]
66+
["53..7....","6..195...",".98....6.","8...6...3","4..8.3..1","7...2...6",".6....28.","...419..5","....8..79"]
67+
"""

HashTable/36_ValidSudoku.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/*
22
* @Author: [email protected]
3-
* @Last Modified time: 2016-07-13 21:09:57
3+
* @Last Modified time: 2016-07-17 10:15:37
44
*/
55

66
class Solution
77
{
88
public:
9-
/*
9+
/* Hash Table
10+
Use three 9*9 array to keep the used numbers in row, col, panel.
1011
According to:
1112
https://discuss.leetcode.com/topic/8241/my-short-solution-by-c-o-n2
1213
*/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
# Combination
311311

312312
* 030. [Substring with Concatenation of All Words](Combination/30_SubstringWithConcatenationOfAllWords.py)
313+
* 037. [Sudoku Solver](Combination/37_SudokuSolver.py)
313314
* 140. [Word Break II](Combination/140_WordBreakII.py)
314315
* 146. [LRU Cache](Combination/146_LRUCache.py)
315316
* 300. [Longest Increasing Subsequence](Combination/300_LongestIncreasingSubsequence.py)

Week06/37.py

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)