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

solution to task #152

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
80 changes: 80 additions & 0 deletions sudoku.go
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
package main

import "fmt"

// SolveSudoku takes a 9x9 Sudoku grid with zeros representing empty cells and solves the puzzle.
func SolveSudoku(grid [][]int) bool {
if isSolved(grid) {
return true
}

for row := 0; row < 9; row++ {
for col := 0; col < 9; col++ {
if grid[row][col] == 0 {
for num := 1; num <= 9; num++ {
if isValid(grid, row, col, num) {
grid[row][col] = num
if SolveSudoku(grid) {
return true
}
grid[row][col] = 0 // backtrack
}
}
return false // trigger backtracking
}
}
}
return true // puzzle is solved
}

// isSolved checks if the Sudoku puzzle is already solved.
func isSolved(grid [][]int) bool {
for row := range grid {
for col := range grid[row] {
if grid[row][col] == 0 {
return false
}
}
}
return true
}

// isValid checks if placing num in grid[row][col] is valid per Sudoku rules.
func isValid(grid [][]int, row, col, num int) bool {
for i := 0; i < 9; i++ {
// Check row and column
if grid[row][i] == num || grid[i][col] == num {
return false
}

// Check 3x3 subgrid
subRow := 3 * (row / 3)
subCol := 3 * (col / 3)
if grid[subRow+i/3][subCol+i%3] == num {
return false
}
}
return true
}

func main() {
grid := [][]int{
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9},
}

if SolveSudoku(grid) {
fmt.Println("Sudoku Solved:")
for _, row := range grid {
fmt.Println(row)
}
} else {
fmt.Println("No solution exists")
}
}