Create a program that resolves a sudoku.
A valid sudoku has only one possible solution.
I can not understand why Sudoku does not fill The code must be filled in via ASCII Parameters are taken via go run . "1..3..5.2" and so on Digits are inserted instead of dots
package main
import (
"os"
"github.com/01-edu/z01"
)
func printGrid(grid [9][9]rune) {
for i := range grid {
for j := range grid {
z01.PrintRune(grid[i][j])
z01.PrintRune(' ')
}
z01.PrintRune('\n')
}
}
func isSafe(grid [9][9]rune, row int, col int, num rune) bool {
for x := 0; x < len(grid); x {
if grid[row][x] == num || grid[x][col] == num {
return false
}
}
startRow := row - row%3
startCol := col - col%3
for i := 0; i < 3; i {
for j := 0; j < 3; j {
if grid[i startRow][j startCol] == num {
return false
}
}
}
return true
}
func Sudoku(grid [9][9]rune, row int, col int) bool {
if col == 9 && row == 8 {
return true
}
if col == 9 {
row
col = 0
}
if grid[row][col] > '0' {
return Sudoku(grid, row, col 1)
}
for num := '1'; num <= '9'; num {
if isSafe(grid, row, col, num) {
grid[row][col] = num
if Sudoku(grid, row, col 1) {
return true
}
}
grid[row][col] = '0'
}
return false
}
func main() {
args := os.Args[1:]
var grid [9][9]rune
if len(args) == 9 {
for i := 0; i < len(args); i {
for j, s := range args[i] {
if s >= '1' && s <= '9' {
grid[i][j] = s
} else {
grid[i][j] = '0'
}
}
}
if Sudoku(grid, 0, 0) {
printGrid(grid)
}
} else {
for _, s := range "Error" {
z01.PrintRune(s)
}
}
}
I can't understand what the problem is, logically everything should be right
CodePudding user response:
your logic is right... need some corrections, I replace os.Args
with args := []string
because it will be painull to fill the argument of 9*9, you have to change according to your needs.
I used a real sudoku puzzle and it is solved.
package main
import (
"errors"
"fmt"
"os"
"unicode/utf8"
)
func printGrid(grid [9][9]rune) {
for i := range grid {
for j := range grid {
PrintRune(grid[i][j])
PrintRune(' ')
}
PrintRune('\n')
}
}
func isSafe(grid *[9][9]rune, row int, col int, num rune) bool {
for x := 0; x < len(grid); x {
if grid[row][x] == num || grid[x][col] == num {
return false
}
}
startRow := row - row%3
startCol := col - col%3
for i := 0; i < 3; i {
for j := 0; j < 3; j {
if grid[i startRow][j startCol] == num {
return false
}
}
}
return true
}
func Sudoku(grid *[9][9]rune, row int, col int) bool {
if col == 9 && row == 8 {
return true
}
if col == 9 {
row
col = 0
}
if grid[row][col] > '0' {
return Sudoku(grid, row, col 1)
}
for num := '1'; num <= '9'; num {
if isSafe(grid, row, col, num) {
grid[row][col] = num
if Sudoku(grid, row, col 1) {
return true
}
}
grid[row][col] = '0'
}
return false
}
func PrintRune(r rune) error {
l := utf8.RuneLen(r)
if l == -1 {
return errors.New("The rune is not a valid value to encode in UTF-8")
}
p := make([]byte, l)
utf8.EncodeRune(p, r)
_, err := os.Stdout.Write(p)
return err
}
func main() {
//args := os.Args[1:]
args := []string{
"2...7....",
"...1.2734",
"467..8..9",
"...91...8",
".1..87...",
".862541.7",
"..834..2.",
"94..2.85.",
"65..9.4.."}
var grid [9][9]rune
if len(args) == 9 {
for i := 0; i < len(args[i])-1; i {
for j, s := range []rune(args[i]) {
if s >= '1' && s <= '9' {
grid[i][j] = s
} else {
grid[i][j] = '0'
}
}
}
printGrid(grid)
fmt.Printf("\n---------\n")
if Sudoku(&grid, 0, 0) {
printGrid(grid)
}
} else {
for _, s := range "Error" {
PrintRune(s)
}
}
}
output:
2 0 0 0 7 0 0 0 0
0 0 0 1 0 2 7 3 4
4 6 7 0 0 8 0 0 9
0 0 0 9 1 0 0 0 8
0 1 0 0 8 7 0 0 0
0 8 6 2 5 4 1 0 7
0 0 8 3 4 0 0 2 0
9 4 0 0 2 0 8 5 0
---------
2 3 1 4 7 9 6 8 5
8 9 5 1 6 2 7 3 4
4 6 7 5 3 8 2 1 9
7 2 4 9 1 3 5 6 8
5 1 9 6 8 7 3 4 2
3 8 6 2 5 4 1 9 7
1 7 8 3 4 5 9 2 6
9 4 3 7 2 6 8 5 1
6 5 2 8 9 1 4 7 3
CodePudding user response:
os.Args[1:]
evaluates to a list with one element containing your input string (represented by the literal [1..3..5.2]
).
So len(args) == 9
evaluates to false
, i.e. the else clause is executed.
Also, Sudoku(grid, 0, 0)
will call the function Sudoku
with a copy of grid
. What you probably want to do, is use a pointer type instead so Sudoku
can modify the content of your grid, so change the function definition to:
func Sudoku(grid *[9][9]rune, row int, col int) bool
and call the method with the address of grid
(using the address-of operator &
) instead of the value of grid
. Lookup the difference between call by value and call by reference for more on this.