forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DFS] move solution to Sudoku Solver to DFS
- Loading branch information
Yi Gu
committed
Nov 30, 2016
1 parent
7fc189b
commit fd395da
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/sudoku-solver/ | ||
* Primary idea: Iterate through the whole matrix, try to fill out empty space with all | ||
* possible cases and check the vaildity | ||
* | ||
* Time Complexity: O(n^4), Space Complexity: O(1) | ||
*/ | ||
|
||
class SudokuSolver { | ||
func solveSudoku(_ board: inout [[Character]]) { | ||
guard board.count != 0 || board[0].count != 0 else { | ||
return | ||
} | ||
helper(&board) | ||
} | ||
|
||
private func helper(_ board: inout [[Character]]) -> Bool { | ||
let m = board.count, n = board[0].count | ||
|
||
for i in 0 ..< m { | ||
for j in 0 ..< n { | ||
if board[i][j] == "." { | ||
for num in 1 ... 9 { | ||
if isValid(board, i, j, Character(String(num))) { | ||
board[i][j] = Character(String(num)) | ||
|
||
if helper(&board) { | ||
return true | ||
} else { | ||
board[i][j] = "." | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
private func isValid(_ board: [[Character]], _ i: Int, _ j: Int, _ num: Character) -> Bool { | ||
let m = board.count, n = board[0].count | ||
|
||
// check row | ||
for x in 0 ..< n { | ||
if board[i][x] == num { | ||
return false | ||
} | ||
} | ||
|
||
// check col | ||
for y in 0 ..< m { | ||
if board[y][j] == num { | ||
return false | ||
} | ||
} | ||
|
||
// check square | ||
for x in i / 3 * 3 ..< i / 3 * 3 + 3 { | ||
for y in j / 3 * 3 ..< j / 3 * 3 + 3 { | ||
if board[x][y] == num { | ||
return false | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} | ||
} |