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 JAVA (judge verdict: Accepted)

import java.io.*;

class Main {
   public static void main(String[] args) {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      try {
         int n = Integer.parseInt(br.readLine());
         Dice[] d = new Dice[n];
         String[][] input = new String[n][];
         for (int i = 0; i < n; i++) {
            input[i] = br.readLine().split(" ");
         }
         int[][] roll = new int[n][6];
         for (int i = 0; i < n; i++) {
            for (int j = 0; j < 6; j++) {
               roll[i][j] = Integer.parseInt(input[i][j]);
            }
         }
         for (int i = 0; i < n; i++) {
            d[i] = new Dice(roll[i][0], roll[i][1], roll[i][2], roll[i][3], roll[i][4], roll[i][5]);
         }
         boolean isSame = false;
         for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
               isSame = compare(d[i], d[j]);
               if (isSame) break;
            }
            if (isSame) break;
         }
         System.out.println(isSame ? "No" : "Yes");
      }
      catch (Exception e) {
         e.printStackTrace();
      }
   }
   static boolean compare(Dice d1, Dice d2) {
      if (check(d1, d2)) return true;
      else {
         if (rotate(d1, d2)) return true;
      }
      return false;
   }
   static boolean check(Dice d1, Dice d2) {
      if (d1.Top == d2.Top && d1.Front == d2.Front && d1.Right == d2.Right && d1.Left == d2.Left && d1.Back == d2.Back && d1.Bottom == d2.Bottom) {
         return true;
      }
      return false;
   }
   static boolean rotate(Dice d1, Dice d2) {
      for (int i = 0; i <= 6; i++) {
         if (i == 0) {
            for (int j = 0; j < 4; j++) {
               if (check(d1, d2)) return true;
               d2.spin();
            }
         }
         if (i == 1) {
            d2.north();
            for (int j = 0; j < 4; j++) {
               if (check(d1, d2)) return true;
               d2.spin();
            }
         }
         if (i == 2) {
            d2.north();
            for (int j = 0; j < 4; j++) {
               if (check(d1, d2)) return true;
               d2.spin();
            }
         }
         if (i == 3) {
            d2.north();
            for (int j = 0; j < 4; j++) {
               if (check(d1, d2)) return true;
               d2.spin();
            }
         }
         if (i == 4) {
            d2.north();
            d2.east();
            for (int j = 0; j < 4; j++) {
               if (check(d1, d2)) return true;
               d2.spin();
            }
         }
         if (i == 5) {
            d2.east();
            for (int j = 0; j < 4; j++) {
               if (check(d1, d2)) return true;
               d2.spin();
            }
         }
         if (i == 6) {
            d2.east();
            for (int j = 0; j < 4; j++) {
               if (check(d1, d2)) return true;
               d2.spin();
            }
         }
      }
      return false;
   }
}

class Dice {
   int Top;
   int Front;
   int Right;
   int Left;
   int Back;
   int Bottom;
   int work;

   Dice(int top, int front, int right, int left, int back, int bottom) {
      Top    = top;
      Left   = left;
      Back   = back;
      Front  = front;
      Right  = right;
      Bottom = bottom;
   }
   void north() {
      work   = Top;
      Top    = Front;
      Front  = Bottom;
      Bottom = Back;
      Back   = work;
   }
   void east() {
      work   = Top;
      Top    = Left;
      Left   = Bottom;
      Bottom = Right;
      Right  = work;
   }
   void west() {
      work   = Top;
      Top    = Right;
      Right  = Bottom;
      Bottom = Left;
      Left   = work;
   }
   void south() {
      work   = Top;
      Top    = Back;
      Back   = Bottom;
      Bottom = Front;
      Front  = work;
   }
   void spin() {
      work  = Front;
      Front = Right;
      Right = Back;
      Back  = Left;
      Left  = work;
   }
}