Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
2.8 kB
2
Indexable
Never
package main

import (
	"fmt"
	"os"
)

const boardSize = 9

func main() {
	boardStrings := os.Args[1:]
	if len(boardStrings) != boardSize {
		fmt.Println("Error")
		return
	}
	board := make([][]rune, boardSize)
	for i, s := range boardStrings {
		if len(s) != boardSize {
			fmt.Println("Error")
			return
		}
		board[i] = []rune(s)
	}
	if !isValidSudoku(board) {
		fmt.Println("Error")
		return
	}
	if !solveBoard(board) {
		fmt.Println("Error")
		return
	}
	for _, row := range board {
		for _, num := range row {
			fmt.Printf("%c ", num)
		}
		fmt.Println()
	}
}

func isValidSudoku(board [][]rune) bool {
	for i := 0; i < boardSize; i++ {
		if !isValidRow(board, i) || !isValidColumn(board, i) || !isValidSubgrid(board, i/3*3, i%3*3) {
			return false
		}
	}
	return true
}

func isValidRow(board [][]rune, row int) bool {
	seen := make(map[rune]bool)
	for _, num := range board[row] {
		if num != '.' {
			if seen[num] {
				return false
			}
			seen[num] = true
		}
	}
	return true
}

func isValidColumn(board [][]rune, col int) bool {
	seen := make(map[rune]bool)
	for i := 0; i < boardSize; i++ {
		num := board[i][col]
		if num != '.' {
			if seen[num] {
				return false
			}
			seen[num] = true
		}
	}
	return true
}

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 != '.' {
				if seen[num] {
					return false
				}
				seen[num] = true
			}
		}
	}
	return true
}

func isValidInitialConfiguration(board [][]rune) bool {
	for _, row := range board {
		for _, num := range row {
			if num != '.' && (num < '1' || num > '9') {
				return false
			}
		}
	}
	return true
}

func solveBoard(board [][]rune) bool {
    row, col := -1, -1
    for i, r := range board {
        for j, c := range r {
            if c == '.' {
                row, col = i, j
                break
            }
        }
        if row != -1 {
            break
        }
    }
    if row == -1 {
        return true
    }
    for digit := '1'; digit <= '9'; digit++ {
        if isValidPlacement(board, row, col, digit) {
            board[row][col] = digit
            if solveBoard(board) {
                return true
            }
            board[row][col] = '.'
        }
    }
    return false
}

func isValidPlacement(board [][]rune, row, col int, digit rune) bool {
    for i := 0; i < boardSize; i++ {
        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
}