Nine problem (multiple ways of backtracking, go language implementation)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Nine problem (backtracking, go language implementation)

Problem Recurrence:

There are 1~10 10 number, from which to select the 9 number of non-repeating fill to nine Gongge, now requires the adjacent (upper and lower, left and right) two number of the sum of Prime, ask how many kinds of filling?

This problem is relatively simple, so directly to the code.

Solution One

package mainimport ("FMT") var pos [9]intvar Sub []int = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10  }var num []int = []int{1, 2, 3, 5, 7, 11, 13, 17, 19}/* lookup from prime number, found return true*/func searchfromnum (n int) bool {for i: = 0; i < 9; i++ {if n = = Num[i] {return True}}return false}/* test result is correct */func check (i, n int) bool {//portrait if i-3 >= 0 {if searchfromnum (pos[i]+pos[i-3]) = = False {return false}}//horizontal if i%3! = 0 {if searchfromnum (pos[i]+pos[i-1]) = = False {return False}}retur n True}var down, up int = 0, 9/* fills in 1~10 to nine Gongge solution, backtracking */func fillbox (i, n, R int, Count *int) {if i = = n {(*count) ++for I: = 0; I < R; i++ {for J: = 0; J < R; J + + {FMT. Printf ("%3d", Pos[i*r+j])}fmt. Println ()}fmt. Println ("============") return}for j: = down; J <= up; J + + {//first put pos[i] = sub[j]if sub[j]! =-1 && check (i, n) {sub[j] = -1fillbox (i+1, N, R, count) sub[j] = Pos[i]}}}fun C Main () {count: = 0fillBox (0, 9, 3, &count) fmt. Println (count)} 

solution two

Package Mainimport ("FMT") var pos [9]intvar Sub []int = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}var num []int = []int{1, 2, 3,  5, 7, 11, 13, 17, 19}/* lookup from prime number, find return True*/func searchfromnum (n int) bool {for i: = 0; i < 9; i++ {if n = = Num[i] {return True}}return false}/* test result is correct */func check (n int) bool {//Line adjacent for I: = 0, i < n; i++ {for J: = 0; J < N-1; J + + {if SE Archfromnum (pos[i*n+j]+pos[i*n+j+1]) = = False {return false}}}//column adjacent for j: = 0; J < N; J + + {for I: = 0; i < n-1; i++ {if Searchfromnum (pos[i*n+j]+pos[(i+1) *n+j]) = = False {return False}}}return True}var Dow n, up int = 0, 9/* fill in 0~8 to nine Gongge solution, full array (enum) */func Fillbox (i, n, R int, Count *int) {if i = = n {if check (r) {(*count) ++for I: = 0 ; I < R; i++ {for J: = 0; J < R; J + + {FMT. Printf ("%3d", Pos[i*r+j])}fmt. Println ()}fmt. Println ("============")}return}for j: = down; J <= up; J + + {if SUB[J]! =-1 {pos[i] = sub[j]sub[j] = -1fillbox (i+1, N, R, count) sub[j] = Pos[i]}}}func Main () {count: = 0fillBox ( 0, 9, 3, &count) fmt. Println (count)} 

backtracking non-recursive solution:

Package Mainimport ("FMT") var num [9]int = [9]int{1, 2, 3, 5, 7, one, three,, 19}var pos [9]int//store continuous 1~10var sum, down, u  p, r int = 9, 0, ten, 3func backTrack () int {sum--isnoconflict: = TRUE//default True no conflict count: = 0//Statistics 0i: = 0pos[0] = 1//Start value is starting from 1 for {if isnoconflict {if i = = sum {count++for I: = 0, i < R; i++ {for J: = 0; J < R; J + + {FMT. Printf ("%3d", Pos[i*r+j])}fmt. Println ()}for pos[i] = = up {I--if i = =-1 {return count}//at this time I occupy a value that does not participate in check (), so the value is not important, it is equal to undo the previous occupation}pos[i]++} else {I++pos[i] = 1//Start value is from 1}} else {//conflict for pos[i] = = up {I--if i = =-1 {return count}//the value I occupy at this time is not involved in check (), so the value is not important, it is equal to undo the previous occupation}pos[i]++} Isnoconflict = Check (i)}}func main () {FMT. Println (BackTrack ())}/* Verify the result is correct */func check (i int) bool {//Search current value has been used before for j: = 0; J < i; J + + {if pos[j] = = Pos[i] { Return false}}//portrait If i-3 >= 0 {if searchfromnum (pos[i]+pos[i-3]) = = False {return false}}//transverse if i%3! = 0 {if search Fromnum (pos[i]+pos[i-1]) = = False {return False}}return true}/* Find from prime number, find return True*/func searchfromnum (n int) bool {for i: = 0; i < 9; i++ {if n = num[i] {return True}}return false} 

 

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.