Scala的独特功能出现意外行为

时间:2017-09-08 06:58:53

标签: algorithm scala

我试图制作一个算法来解决亚里士多德数字拼图。我在Scala上写了一组方程式,需要满足谜题的标准。一个标准是所有结果必须是不同的。为了做到这一点,我尝试了这个:

object Main extends App {
    val perms = List(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19).combinations(7)

    for(perm <- perms) {
        val j = perm(0)
        val k = perm(1)
        val n = perm(2)
        val o = perm(3)
        val p = perm(4)
        val r = perm(5)
        val s = perm(6)

        val depVars: List[Int] = List(
          76 - j - k - n - 2*o - p - r - s,
          j + n + o,
          -38 + k + o + p + r + s,
          j + k + o,
          -38 + k + n + o + p + r,
          38 - j - k - n - o - p,
          38 - k - o - r,
          -38 + n + o + p + r + s,
          38 - j - k - n - o - r,
          38 - p - s,
          38 - n - o - p,
          38 - r - s
        )

        val depVarsDis = depVars.distinct.filter(_ > 0)
        if (depVarsDis.length == 12) {
          val a = depVarsDis(0)
          val b = depVarsDis(1)
          val c = depVarsDis(2)
          val d = depVarsDis(3)
          val e = depVarsDis(4)
          val f = depVarsDis(5)
          val g = depVarsDis(6)
          val h = depVarsDis(7)
          val i = depVarsDis(8)
          val l = depVarsDis(9)
          val m = depVarsDis(10)
          val q = depVarsDis(11)

          if (a + b + c == 38 &&
              d + e + f + g == 38 &&
              h + i + j + k + l == 38 &&
              m + n + o + p == 38 &&
              q + r + s == 38 &&
              c + g + l == 38 &&
              b + f + k + p == 38 &&
              a + e + j + o + s == 38 &&
              d + i + n + r == 38 &&
              h + m + q == 38 &&
              a + d + h == 38 &&
              b + e + i + m == 38 &&
              c + f + j + n + q == 38 &&
              g + k + o + r == 38 &&
              l + p + s == 38
          ) {
            println("")
            println(s"   $a,$b,$c")
            println(s"  $d,$e,$f,$g")
            println(s" $h,$i,$j,$k,$l")
            println(s"  $m,$n,$o,$p")
            println(s"   $q,$r,$s")
            println("")
      }
    }
  }
}

但结果仍然存在重复:

 11,19,8,18,3,4,13,9,2,4,6,17,14,7,8,9,15,11,12 // 11,4,9
 3,19,16,17,4,5,12,18,1,4,5,10,14,7,8,9,6,13,19 // 19,4

如何以保证值唯一性的方式执行此操作?

0 个答案:

没有答案