Advertisement
Guest User

Untitled

a guest
Sep 21st, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.41 KB | None | 0 0
  1. object ProblemB extends App {
  2.     def comparePretty(listA: Seq[Int], listB: Seq[Int]): Boolean  = {
  3.         if (listA.isEmpty) {
  4.             false
  5.         } else {
  6.             val aHead = listA.head
  7.             val bHead = listB.head
  8.             if (aHead == bHead) {
  9.                 comparePretty(listA.tail, listB.tail)
  10.             } else {
  11.                 if (aHead < bHead) {
  12.                     true
  13.                 } else {
  14.                     false
  15.                 }
  16.             }
  17.         }
  18.     }
  19.  
  20.     def swap[A](arr: List[A], index1: Int, index2: Int): List[A] = {
  21.         val tmp = arr(index1)
  22.         arr.updated(index1, arr(index2)).updated(index2,tmp)
  23.     }
  24.  
  25.     var visited: Set[List[Int]] = Set()
  26.     def findPrettiest(permutationMatrix: Set[List[Int]], currentList: List[Int], prettiestSoFar: List[Int]): List[Int] = {
  27.         if (visited.contains(currentList)) {
  28.             if (comparePretty(currentList, prettiestSoFar)) currentList else prettiestSoFar
  29.         } else {
  30.             val validSwaps = permutationMatrix.map { permutation =>
  31.                 swap(currentList, permutation(0), permutation(1))
  32.             }.filter { swap =>
  33.                 !visited.contains(swap)
  34.             }
  35.  
  36.             validSwaps.map {
  37.                 swap =>
  38.                     val prettier = if (comparePretty(swap.toList, prettiestSoFar.toList)) {
  39.                         swap
  40.                     } else {
  41.                         prettiestSoFar
  42.                     }
  43.                     visited = visited + currentList
  44.                     findPrettiest(permutationMatrix, swap, prettier)
  45.  
  46.             }.foldLeft(currentList) {
  47.                 (left, right) => if (comparePretty(left.toList, right.toList)) {
  48.                     left
  49.                 } else {
  50.                     right
  51.                 }
  52.             }
  53.         }
  54.     }
  55.  
  56.     val size = scala.io.StdIn.readLine.toInt
  57.     val cList = scala.io.StdIn.readLine.split(" ").map(_.toInt).toList
  58.     val permutationMatrix = (for {
  59.         i: Int <- (0 to size - 1).toList
  60.     } yield {
  61.         val strP = scala.io.StdIn.readLine.substring(i)
  62.         strP.zipWithIndex.filter {
  63.             case (v, _) => v == '1'
  64.         }.map {
  65.             case (_, index) => index + i
  66.         }.map {
  67.             index => List(i, index)
  68.         }.toList
  69.     }).flatten.toSet
  70.  
  71.     println(findPrettiest(permutationMatrix, cList, cList).mkString(" "))
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement