【发布时间】:2017-04-05 06:11:14
【问题描述】:
我有一个 5 x 5 的网格。我的初始位置在 (0,0)
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
# 0 0 0 0
我有一种方法可以从那个位置找到所有可能的“L”形位置
所以来自 (0,0)。
所以要么
- ( x + 2 )( y + 1 )
或
- ( x + 1 )( y + 2 )
我们有两个职位
0 0 0 0 0
0 0 0 0 0
0 # 0 0 0
0 0 0 0 0
# 0 0 0 0
or
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 # 0 0
# 0 0 0 0
然后我的方法将从所有可能的移动列表中调用它自己并找到每个移动的位置。
仅当可能移动列表中的最后一个位置与初始位置相同时,递归才会中断。
所以可能的位置移动:
(0)(0)是List((1,2),(2,1))List((possible moves for (1,2) ), (possible moves for (2,1) ) )
等等:
到目前为止我的方法
def CountTour(dimension: Int, path: Path): Int = {
// possibleMoves returns list of possible moves
val Moves = possibleMoves(dimension, path, path.head).contains(path.last)
// if the last element is not the same as first position, and has visited the whole graph, then break the recursion
if (legalMoves && path.size == (dimension * dimension)) {
1
} else {
// else call the same method again with each possible move
val newList = for (i <- legal_moves(dimension, path, path.head)) yield i
count_tours(dimension,newList)
}
}
但是这行不通。我该如何解决?
【问题讨论】:
-
你能详细说明“这行不通”吗?
-
它不会给我所有可能的职位@ScottHunter
-
它给了你什么?
-
所以对于 (0,0) 它将返回 List( (1,2),(2,1) )。但现在我需要再次调用该方法以返回 (1,2) 和 (2,1) @jwvh 的移动
标签: scala list for-loop recursion chess