Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
3
Indexable
Never
package main

import (
	"fmt"
	"os"
)

const boardSize = 9

func main() {
	// Read command-line input as strings representing rows of the Sudoku grid
	boardStrings := os.Args[1:]

	// Check if the number of input rows matches the board size
	if len(boardStrings) != boardSize {
		fmt.Println("Error")
		return
	}

	// Create a 2D rune slice to represent the Sudoku board
	board := make([][]rune, boardSize)
	for i, s := range boardStrings {
		// Check if the length of each row matches the board size
		if len(s) != boardSize {
			fmt.Println("Error")
			return
		}
		board[i] = []rune(s)
	}

	// Validate the initial configuration of the Sudoku board
	if !isValidSudoku(board) {
		fmt.Println("Error")
		return
	}

	// Solve the Sudoku board using backtracking algorithm
	if !solveBoard(board) {
		fmt.Println("Error")
		return
	}

	// Print the solved Sudoku board
	for _, row := range board {
		for _, num := range row {
			fmt.Printf("%c ", num)
		}
		fmt.Println()
	}
}

// Function to validate the entire Sudoku board
func isValidSudoku(board [][]rune) bool {
	for i := 0; i < boardSize; i++ {
		// Check each row, column, and 3x3 subgrid for validity
		if !isValidRow(board, i) || !isValidColumn(board, i) || !isValidSubgrid(board, i/3*3, i%3*3) {
			return false
		}
	}
	return true
}

// Function to validate a row in the Sudoku board
func isValidRow(board [][]rune, row int) bool {
	seen := make(map[rune]bool)
	for _, num := range board[row] {
		if num != '.' && seen[num] {
			return false
		}
		seen[num] = true
	}
	return true
}

// Function to validate a column in the Sudoku board
func isValidColumn(board [][]rune, col int) bool {
	seen := make(map[rune]bool)
	for i := 0; i < boardSize; i++ {
		num := board[i][col]
		if num != '.' && seen[num] {
			return false
		}
		seen[num] = true
	}
	return true
}

// Function to validate a 3x3 subgrid in the Sudoku board
func isValidSubgrid(board [][]rune, startRow, startCol int) bool {
	seen := make(map[rune]bool)
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			num := board[startRow+i][startCol+j]
			if num != '.' && seen[num] {
				return false
			}
			seen[num] = true
		}
	}
	return true
}

// Function to solve the Sudoku board using backtracking
func solveBoard(board [][]rune) bool {
	// Find the next empty cell in the board
	row, col := findEmptyCell(board)
	if row == -1 {
		return true // Entire board is filled, solution found
	}
	for digit := '1'; digit <= '9'; digit++ {
		// Try placing each valid digit in the empty cell
		if isValidPlacement(board, row, col, digit) {
			board[row][col] = digit
			if solveBoard(board) {
				return true // Recursive solution found
			}
			board[row][col] = '.' // Backtrack
		}
	}
	return false // No valid digit found, backtrack
}

// Function to validate placing a digit in a specific cell
func isValidPlacement(board [][]rune, row, col int, digit rune) bool {
	for i := 0; i < boardSize; i++ {
		// Check the row, column, and 3x3 subgrid for conflicts
		if board[row][i] == digit || board[i][col] == digit || board[row/3*3+i/3][col/3*3+i%3] == digit {
			return false
		}
	}
	return true
}

// Function to find the next empty cell in the Sudoku board
func findEmptyCell(board [][]rune) (int, int) {
	for i, r := range board {
		for j, c := range r {
			if c == '.' {
				return i, j // Return coordinates of the empty cell
			}
		}
	}
	return -1, -1 // No empty cell found
}