Created
July 27, 2015 08:22
-
-
Save itarato/ce26002cec5b69fa46d6 to your computer and use it in GitHub Desktop.
Challenge 224 hard - Langford strings
This file contains 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 ( | |
"log" | |
) | |
var ( | |
n int = 8 | |
l []int = make([]int, n*2) | |
used map[int]bool = make(map[int]bool) | |
) | |
func fill(pos int) { | |
if pos >= n*2 { | |
log.Println("Victory!", l) | |
return | |
} else if l[pos] != 0 { // Has a number at this position. | |
fill(pos + 1) | |
} else { | |
for i := 1; i <= n; i++ { | |
is_used, ok := used[i] | |
if ok && is_used { // This number was used already. | |
continue | |
} | |
second_pos := pos + i + 1 | |
if second_pos >= n*2 || l[second_pos] != 0 { // The right part would not fit. | |
continue | |
} | |
// Place number. | |
l[pos] = i | |
l[pos+i+1] = i | |
used[i] = true | |
fill(pos + 1) | |
// Remove number | |
l[pos] = 0 | |
l[pos+i+1] = 0 | |
used[i] = false | |
} | |
} | |
} | |
func main() { | |
fill(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment