Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object ProblemB extends App {
- def comparePretty(listA: Seq[Int], listB: Seq[Int]): Boolean = {
- if (listA.isEmpty) {
- false
- } else {
- val aHead = listA.head
- val bHead = listB.head
- if (aHead == bHead) {
- comparePretty(listA.tail, listB.tail)
- } else {
- if (aHead < bHead) {
- true
- } else {
- false
- }
- }
- }
- }
- def swap[A](arr: List[A], index1: Int, index2: Int): List[A] = {
- val tmp = arr(index1)
- arr.updated(index1, arr(index2)).updated(index2,tmp)
- }
- var visited: Set[List[Int]] = Set()
- def findPrettiest(permutationMatrix: Set[List[Int]], currentList: List[Int], prettiestSoFar: List[Int]): List[Int] = {
- if (visited.contains(currentList)) {
- if (comparePretty(currentList, prettiestSoFar)) currentList else prettiestSoFar
- } else {
- val validSwaps = permutationMatrix.map { permutation =>
- swap(currentList, permutation(0), permutation(1))
- }.filter { swap =>
- !visited.contains(swap)
- }
- validSwaps.map {
- swap =>
- val prettier = if (comparePretty(swap.toList, prettiestSoFar.toList)) {
- swap
- } else {
- prettiestSoFar
- }
- visited = visited + currentList
- findPrettiest(permutationMatrix, swap, prettier)
- }.foldLeft(currentList) {
- (left, right) => if (comparePretty(left.toList, right.toList)) {
- left
- } else {
- right
- }
- }
- }
- }
- val size = scala.io.StdIn.readLine.toInt
- val cList = scala.io.StdIn.readLine.split(" ").map(_.toInt).toList
- val permutationMatrix = (for {
- i: Int <- (0 to size - 1).toList
- } yield {
- val strP = scala.io.StdIn.readLine.substring(i)
- strP.zipWithIndex.filter {
- case (v, _) => v == '1'
- }.map {
- case (_, index) => index + i
- }.map {
- index => List(i, index)
- }.toList
- }).flatten.toSet
- println(findPrettiest(permutationMatrix, cList, cList).mkString(" "))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement