Use an ImmutableMap[(Int,Int)->Int] in place of the Array[Array[Int]]. Use a List[Int] instead of an Array[Int] for bestIndices.