Created
April 14, 2014 03:47
-
-
Save 1000copy/10614432 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ("fmt" | |
| // "math" | |
| ) | |
| const max =8 | |
| func main(){ | |
| q := Queen{sum:0} | |
| q.put(0) | |
| fmt.Println(q.sum) | |
| return | |
| } | |
| type Queen struct{ | |
| desk [max]int | |
| sum int | |
| } | |
| func (this *Queen)show(){ | |
| for i:=0;i<max ;i++{ | |
| fmt.Print("(",i,",",this.desk[i],")") | |
| } | |
| fmt.Println("\n") | |
| this.sum++ | |
| } | |
| func (this *Queen)check(n int)bool{ | |
| for i:=0;i<n ;i++{ | |
| if this.kill(i,n){ | |
| return false | |
| } | |
| } | |
| return true; | |
| } | |
| func abs(l int)int{ | |
| if l>= 0 { | |
| return l | |
| }else{ | |
| return -l | |
| } | |
| } | |
| func (this *Queen)kill(i int,n int) bool{ | |
| return this.desk[i] == this.desk[n] || abs(this.desk[i]-this.desk[n]) == (n-i ) | |
| } | |
| func (this *Queen)put(n int){ | |
| // fmt.Println(max) | |
| for i:=0;i<max ;i++{ | |
| this.desk[n] = i ; | |
| if this.check(n){ | |
| if n == max -1 { | |
| this.show() | |
| }else{ | |
| this.put(n+1) | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment