The simplest Sudoku solution for Go language implementation _golang

Source: Internet
Author: User

Soduku.go

Copy Code code as follows:

Package Main
Import (
"FMT"
)
Type node []int
var Sudokumay [9][9]node
var Sudoku = [9][9]int{
{0, 0, 0, 0, 0, 0, 8, 0, 0},
{0, 8, 2, 4, 0, 0, 0, 0, 0},
{1, 9, 0, 0, 6, 3, 0, 0, 0},
{0, 5, 0, 0, 8, 0, 7, 0, 0},
{6, 7, 8, 2, 0, 9, 1, 4, 3},
{0, 0, 3, 0, 4, 0, 0, 8, 0},
{0, 0, 0, 6, 2, 0, 0, 9, 4},
{0, 0, 0, 0, 0, 5, 6, 1, 0},
{0, 0, 0, 6, 0, 0, 0, 0, 0}}
Func Main () {
N: = inited (Sudoku)
Sudokusure, _: = Sure (sudokumay)
For n > 0 {
n = subinit (sudokusure)
Output (Sudokumay)
Fmt. PRINTLN (N)
Sudokusure, _ = sure (Sudokumay)
}
Output (Sudokumay)
Fmt. Println (Isenable (Sudokumay))
Test ()
}
Func isenable (TN [9][9]node) BOOL {
For I: = 0; I < 9; i++ {
For j: = 0; J < 9; J + + {
If Len (tn[i][j]) = = 0 {
return False
}
}
}
return True
}
Func sure (May [9][9]node) (sure [9][9]int, n int) {
n = 0
For I: = 0; I < 9; i++ {
For j: = 0; J < 9; J + + {
If Len (may[i][j]) = = 1 {
SURE[I][J] = may[i][j][0]
n++
} else {
SURE[I][J] = 0
}
}
}
Return
}
Func Test () {
I, J: = 1, 3
Fmt. Println (Sudoku[i][j])
For k: = ((I/3) * 3); K < ((I/3) *3) +3; k++ {
For L: = ((J/3) * 3); L < ((J/3) *3) +3; l++ {
Fmt. Print (Sudoku[k][l])
}
Fmt. Println ("")
}
}
Func inited (Sud [9][9]int) (changecount int) {
TMP: = 0
Changecount = 0
For I: = 0; I < 9; i++ {
For j: = 0; J < 9; J + + {
If SUD[I][J]!= 0 {
SUDOKUMAY[I][J] = append (Sudokumay[i][j], sud[i][j])
} else {
For k: = 0; K < 9; k++ {
SUDOKUMAY[I][J] = append (Sudokumay[i][j], k+1)
}
SUDOKUMAY[I][J], tmp = Excludemay (i, J, Sudokumay[i][j], Sud)
Changecount + = tmp
}
}
}
Return
}
Func subinit (Sud [9][9]int) (changecount int) {
TMP: = 0
Changecount = 0
For I: = 0; I < 9; i++ {
For j: = 0; J < 9; J + + {
If SUD[I][J]!= 0 {
Sudokumay[i][j][0] = Sud[i][j]
} else {
SUDOKUMAY[I][J], tmp = Excludemay (i, J, Sudokumay[i][j], Sud)
Changecount + = tmp
}
}
}
Return
}
Func Excludemay (ti, TJ int, t node, S [9][9]int) (Rmay node, Changecount int) {
Changecount = 0
var tmpchangecount int
For I: = 0; I < 9; i++ {
If S[I][TJ]!= 0 {
T, Tmpchangecount = exclude (T, S[I][TJ])
Changecount + + Tmpchangecount
}
If S[ti][i]!= 0 {
T, Tmpchangecount = exclude (T, S[ti][i])
Changecount + + Tmpchangecount
}
}
For k: = ((TI/3) * 3); K < ((TI/3) *3) +3; k++ {
For L: = ((TJ/3) * 3); L < ((TJ/3) *3) +3; l++ {
If S[k][l]!= 0 {
T, Tmpchangecount = exclude (T, S[k][l])
Changecount + + Tmpchangecount
}
}
}
Rmay = t
Return
}
Func Excludefirstone (Smay node, n int) (Rmay node, Changecount int) {
Changecount = 0
Rmay = Smay
For I: = 0; I < Len (Smay); i++ {
If smay[i] = = N {
changecount++
Rmay = Append (Smay[:i], smay[i+1:] ...)
Return
}
if i = = Len (smay)-1 {
Return
}
}
Return
}
Func Exclude (Smay node, n int) (TMP node, Changecount int) {
var NC int
Changecount = 0
TMP, NC = Excludefirstone (Smay, N)
For NC > 0 {
TMP, NC = Excludefirstone (tmp, N)
changecount++
}
Return
}
Func Output (Sudoku [9][9]node) {
For I: = 0; I < 9; i++ {
For j: = 0; J < 9; J + + {
Fmt. Print (Sudokumay[i][j])
}
Fmt. Println ("")
}
}

The above is the article to share the entire content of the code, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.