Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Dice IV

Write a program which reads $n$ dices constructed in the same way as Dice I, and determines whether they are all different. For the determination, use the same way as Dice III.

Input

In the first line, the number of dices $n$ is given. In the following $n$ lines, six integers assigned to the dice faces are given respectively in the same way as Dice III.

Output

Print "Yes" if given dices are all different, otherwise "No" in a line.

Constraints

$2 \leq n \leq 100$

$0 \leq $ the integer assigned to a face $ \leq 100$

Sample Input 1

3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1

Sample Output 1

No

Sample Input 2

3
1 2 3 4 5 6
6 5 4 3 2 1
5 4 3 2 1 6

Sample Output 2

Yes

Solution in Scala (judge verdict: Accepted)

import scala.io.StdIn.{readInt,readLine}
/**
  * AOJ
  */

object Main {

  def main(args: Array[String]): Unit = {

    val n = readInt()
    var dices = List.empty[Array[Int]]
    for(_ <- 1 to n){
      dices :+= readLine().split(" ").map(_.toInt)
    }
    println(if(differentFromEachOther(dices)) "Yes" else "No")
  }

  def differentFromEachOther(dices:List[Array[Int]]): Boolean ={
    for(i <- 0 until dices.size){
      val dice1List = createDiceArray(dices(i))
      for(j <- (i + 1) until dices.size) {
        if (dice1List.exists(_ sameElements dices(j))) return false
      }
    }
    return true
  }

  def createDiceArray(dice:Array[Int]) : List[Array[Int]] = {
    var dices = List.empty[Array[Int]]
    for(i <- createDiceArray(dice,"WNNNW"); j <- createDiceArray(i,"HHH")){
      dices +:= j
    }
    dices
  }

  def createDiceArray(dice:Array[Int],commands:String) : List[Array[Int]] = {
    var dices = List(dice)
    for(cmd <- commands.toCharArray){
      dices :+= getNextStatus(cmd,dices.last)
    }
    dices
  }

  def getNextStatus(cmd:Char,dice:Array[Int]): Array[Int] = cmd match {
    case 'N' => Array(dice(1), dice(5), dice(2), dice(3), dice(0), dice(4))
    case 'H' => Array(dice(0), dice(2), dice(4), dice(1), dice(3), dice(5))
    case 'W' => Array(dice(2), dice(1), dice(5), dice(0), dice(4), dice(3))
  }
}