forked from yuyongwei/Algorithms-In-Swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudokuSolver.swift
141 lines (117 loc) · 3.27 KB
/
sudokuSolver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Write a program to solve a Sudoku puzzle by filling the empty cells.
A sudoku solution must satisfy all of the following rules:
Each of the digits 1-9 must occur exactly once in each row.
Each of the digits 1-9 must occur exactly once in each column.
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 '.'.
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.
https://leetcode.com/problems/sudoku-solver/
*/
struct Point {
let x: Int
let y: Int
var vals: Set<Character>
}
func solveSudoku(_ board: inout [[Character]]) {
let n = board.count
var todo = [Point]()
// construct todo
for i in 0..<n {
for j in 0..<n {
if board[i][j] == "." {
let sols = solutions(i, j, board)
todo.append(sols)
}
}
}
todo.sort { $0.vals.count < $1.vals.count }
func checkValid(_ board: [[Character]], _ row: Int, _ col: Int, val: Character) -> Bool {
for i in 0..<n {
if ((i != col && board[row][i] == val) ||
(i != row && board[i][col] == val)) {
return false
}
}
//3x3
let rm = row/3
let cm = col/3
let rlb = rm*3
let rub = rm*3 + 3
let clb = cm * 3
let cub = cm*3 + 3
for i in rlb..<rub {
for j in clb..<cub {
if i == row && j == col { continue }
let c = board[i][j]
if c == val {
return false
}
}
}
return true
}
func dfs(_ b: [[Character]], _ todo: [Point]) {
guard todo.count > 0 else {
board = b
return
}
var copyToDo = todo
var copyBoard = b
var point = copyToDo.removeFirst()
guard board[point.x][point.y] == "." else { return }
for v in point.vals {
if checkValid(copyBoard, point.x, point.y, val: v) {
copyBoard[point.x][point.y] = v
dfs(copyBoard, copyToDo)
}
}
}
var copy = todo
for p in todo {
if p.vals.count == 1 {
copy.removeFirst()
board[p.x][p.y] = p.vals.first!
} else {
//run
dfs(board, copy)
}
}
}
func solutions(_ row: Int, _ col: Int, _ board: [[Character]]) -> Point {
var sol:Set<Character> = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
let n = board.count
//row
for j in 0..<n {
let c = board[row][j]
if c != "." {
sol.remove(c)
}
}
//col
for i in 0..<n {
let c = board[i][col]
if c != "." {
sol.remove(c)
}
}
//3x3
let rm = row/3
let cm = col/3
let rlb = rm*3
let rub = rm*3 + 3
let clb = cm * 3
let cub = cm*3 + 3
for i in rlb..<rub {
for j in clb..<cub {
let c = board[i][j]
if c != "." {
sol.remove(c)
}
}
}
return Point(x: row, y: col, vals: sol)
}