深度优先
- 问题描述:这是一道似曾相识的小学奥数题
_._._ + _._._ = _._._
将1~9数字填入到等式并使等式成立 - 样例:173+286=459
问题转化:借助N数全排的思想,这个问题和全排问题一直,只需要找出1~9在这9个位置的全排,并判断等式的有效性即可。
func DeepFirstSearch(step int) {
if step == n {
if ((locations[0]+locations[3])*100 + (locations[1]+locations[4])*10 + locations[2] + locations[5]) == (locations[6]*100 + locations[7]*10 + locations[8]) {
fmt.Println(locations[0:3], " + ", locations[3:6], " = ", locations[6:9])
}
return
}
for i := 0; i < n; i++ {
if 0 == marks[i] {
locations[step] = i
marks[i] = 1
DeepFirstSearch2(step + 1)
marks[i] = 0
}
}
return
}