Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added backtracking sudoku solver solution #161

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions sudoku.go
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
package main

func SolveSudoku(board [9][9]int) [9][9]int {
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
if board[i][j] == 0 {
for digit := 9; digit >= 1; digit-- {
board[i][j] = digit
if CheckBoardValidity(board) {
if IsBoardComplete(board) {
return board
} else {
SolveSudoku(board)
}
}
board[i][j] = 0
}
return [9][9]int{} // If no solution found, return an empty board
}
}
}
return board
}

func CheckBoardValidity(board [9][9]int) bool {
// Check duplicates by row
for row := 0; row < 9; row++ {
rowMap := make(map[int]int)
for col := 0; col < 9; col++ {
if rowMap[board[row][col]] == 1 {
return false
} else {
rowMap[board[row][col]] = 1
}
}
}

// Check duplicates by column
for col := 0; col < 9; col++ {
colMap := make(map[int]int)
for row := 0; row < 9; row++ {
if colMap[board[row][col]] == 1 {
return false
} else {
colMap[board[row][col]] = 1
}
}
}

// Check duplicates in 3x3 sections
for i := 0; i < 9; i += 3 {
for j := 0; j < 9; j += 3 {
threeByThreeMap := make(map[int]int)
for row := i; row < i+3; row++ {
for col := j; col < j+3; col++ {
if board[row][col] != 0 {
if threeByThreeMap[board[row][col]] == 1 {
return false
} else {
threeByThreeMap[board[row][col]] = 1
}
}
}
}
}
}

return true
}

func IsBoardComplete(board [9][9]int) bool {
for i := 0; i < 9; i++ {
for j := 0; j < 9; j++ {
if board[i][j] == 0 {
return false
}
}
}
return true
}
Loading