Problem:
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 (Python):


import random

class Dice:
    def __init__(self, top, s, e, w, n, bottom):
        self.top    = top
        self.s      = s
        self.e      = e
        self.w      = w
        self.n      = n
        self.bottom = bottom

    def rotate(self, dist):
        if dist == 'N':
            self.top, self.s, self.n, self.bottom = self.s, self.bottom, self.top, self.n 
        elif dist == 'S':
            self.s, self.bottom, self.top, self.n = self.top, self.s, self.n, self.bottom 
        elif dist == 'E':
            self.top, self.e, self.w, self.bottom = self.w, self.top, self.bottom, self.e 
        elif dist == 'W':
            self.w, self.top, self.bottom, self.e = self.top, self.e, self.w, self.bottom 

if __name__ == '__main__':
    random.seed(0)

    n = input()
    dice = [Dice(*map(int, raw_input().split())) for _ in range(n)]
    ref  = [set() for _ in range(n)]

    for _ in range(1000):
        if len(ref[0]) > 23:
            break

        dist = 'NEWS'[random.randint(0,3)]
        for i in range(n):
            ref[i].add((dice[i].top, dice[i].s, dice[i].e))
            dice[i].rotate(dist)
    
    print 'Yes' if len(set(map(str, ref))) == n else 'No'

Solution (Python):
n=0
for i in range(int(input())):
	if i==0:
		d1=list(map(int,input().split()))
	else:
		d2=list(map(int,input().split()))
			
		for i in range(4):
			d2[0],d2[1],d2[4],d2[5]=d2[1],d2[5],d2[0],d2[4]
			for i in range(4):
				d2[1],d2[3],d2[4],d2[2]=d2[3],d2[4],d2[2],d2[1]
				if d1[0]==d2[0] and d1[1]==d2[1] and d1[2]==d2[2] and d1[3]==d2[3] and d1[4]==d2[4] and d1[5]==d2[5]:
					n+=1
		for i in range(4):
			d2[0],d2[3],d2[5],d2[2]=d2[3],d2[5],d2[2],d2[0]
			for i in range(4):
				d2[1],d2[3],d2[4],d2[2]=d2[3],d2[4],d2[2],d2[1]
				if d1[0]==d2[0] and d1[1]==d2[1] and d1[2]==d2[2] and d1[3]==d2[3] and d1[4]==d2[4] and d1[5]==d2[5]:
					n+=1
if n==0:
	print('Yes')
else:
	print('No')

Solution (Python):



import random


class Cube:
    def __init__(self, u, s, e, w, n, d):
        self.u = u
        self.s = s
        self.e = e
        self.w = w
        self.n = n
        self.d = d

    def rotate(self, dic):
        if dic == "N":
            tmp = self.u
            self.u = self.s
            self.s = self.d
            self.d = self.n
            self.n = tmp
        elif dic == "E":
            tmp = self.u
            self.u = self.w
            self.w = self.d
            self.d = self.e
            self.e = tmp
        elif dic == "W":
            tmp = self.u
            self.u = self.e
            self.e = self.d
            self.d = self.w
            self.w = tmp
        else:
            tmp = self.u
            self.u = self.n
            self.n = self.d
            self.d = self.s
            self.s = tmp


def main():
    N = int(input())
    cubeL = []
    for i in range(N):
        u, s, e, w, n, d = map(int, input().split())
        cube = Cube(u, s, e, w, n, d)
        cubeL.append(cube)

    ret = "Yes"
    for i in range(N):
        for j in range(i+1, N):
            flag = True
            for k in range(100):
                cubeL[j].rotate(random.choice("NEWS"))
                if cubeL[i].u == cubeL[j].u and cubeL[i].n == cubeL[j].n and cubeL[i].w == cubeL[j].w and cubeL[i].e == cubeL[j].e and cubeL[i].s == cubeL[j].s and cubeL[i].d == cubeL[j].d:
                    flag = False
                    break
            if not flag:
                break
        if not flag:
            ret = "No"
            break
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):


import random


class Dice() :

    mask = {'N':(1,5,2,3,0,4), 'E':(3,1,0,5,4,2),
            'W':(2,1,5,0,4,3), 'S':(4,0,2,3,5,1),'CW':(0,2,4,1,3,5)}

    way = ("N","E","W","S","CW","Nop")

    def __init__(self, data):
        self.label = data

    def move(self, data):
        if data == "Nop":
            return
        self.label = [self.label[idx] for idx in self.mask[data]]

    def get_up(self):
        return self.label[0]

    def compare_6sq(self, dice2):
        check = True
        for i in range(6):
            if self.label[i] == dice2.label[i] :
                continue
            else:
                check = False
                break

        return check
    
    def compare_4rot(self, dice2):
        ok = False
        for i in range(4):
            self.move("CW")
            if self.compare_6sq(dice2) :
                ok =True
                break
        return ok
    
    def compare(self,dice2):
        ok = False
        orderway =("Nop","E","N","E","N","E")

        for s in orderway:
            self.move(s)
            if self.compare_4rot(dice2):
                ok = True
                break
        
        return ok
    
    def check_dicelist(self, dicelist):
        
        for tdice in dicelist:
            if self.compare(tdice):
                return True

        if len(dicelist) != 1 and dicelist[0].check_dicelist(dicelist[1:]) :
            return True

        return False
    




def check_alldice(dicelist):
    return dicelist[0].check_dicelist(dicelist[1:])

length = int(input())
dicelist1 = []
for _ in range(length):
    dicelist1.append(Dice(input().split()))

if not(check_alldice(dicelist1)) :
    print ("Yes")
else:
    print ("No")

Solution (Python):
from enum import Enum
import sys
import math


class Dice():

    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.order = 'NNNNWNNNWNNNENNNENNNWNNN'

    def setNumber(self,n0,n1,n2,n3,n4,n5):
        self.number[0] = n0
        self.number[1] = n1
        self.number[2] = n2
        self.number[3] = n3
        self.number[4] = n4
        self.number[5] = n5

    def roll(self,loc):
        for i in range(6):
            self.work[i] = self.number[i]

        if loc == 'E':
            self.setNumber(self.work[3],self.work[1],self.work[0],self.work[5],self.work[4],self.work[2])
        elif loc == 'N':
            self.setNumber(self.work[1],self.work[5],self.work[2],self.work[3],self.work[0],self.work[4])
        elif loc == 'S':
            self.setNumber(self.work[4],self.work[0],self.work[2],self.work[3],self.work[5],self.work[1])
        elif loc == 'W':
            self.setNumber(self.work[2],self.work[1],self.work[5],self.work[0],self.work[4],self.work[3])

    def query(self,top_num,front_num):
        self.save_data = [i for i in range(6)]
        for i in range(len(self.number)):
            self.save_data[i] = self.number[i]
        ret =-1

        for i in range(len(self.order)):
            self.roll(self.order[i])
            if self.number[0] == top_num and self.number[1] == front_num:
                ret = self.number[2]
                break
        for i in range(len(self.number)):
            self.number[i] = self.save_data[i]

    def equal(self,another):
        ret = False;

        for i in range(len(self.order)):
            self.roll(self.order[i])
            FLG=True
            for k in range(6):
                if self.number[k] != another.number[k]:
                    FLG = False
                    break
            if FLG == True:
                return True

        return ret;

num_dice=int(input())
DICE=[]

for i in range(num_dice):
    table = list(map(int,input().split()))
    tmp_dice=Dice()
    tmp_dice.setNumber(table[0],table[1],table[2],table[3],table[4],table[5])
    DICE.append(tmp_dice)

FLG = True

for i in range(num_dice-1):
    for k in range(i+1,num_dice):
        if DICE[i].equal(DICE[k])==True:
            FLG = False
            break
            
if FLG == True:
    print("Yes")
else:
    print("No")

Solution (Python):
class Dice():
	def __init__(self):
		self.number = [_ for _ in range(6)]
		self.work = [_ for _ in range(6)]
		self.order = "NNNNWNNNWNNNENNNENNNWNNN"
	def setNumber(self,n0,n1,n2,n3,n4,n5):
		self.number[0] = n0
		self.number[1] = n1
		self.number[2] = n2
		self.number[3] = n3
		self.number[4] = n4
		self.number[5] = n5

	def roll(self,loc):
		for _ in range(6):
			self.work[_] = self.number[_]
		if loc == "E":
			self.setNumber(self.work[3],self.work[1],self.work[0],self.work[5],self.work[4],self.work[2])
		elif loc == "N":
			self.setNumber(self.work[1],self.work[5],self.work[2],self.work[3],self.work[0],self.work[4])
		elif loc == "S":
			self.setNumber(self.work[4],self.work[0],self.work[2],self.work[3],self.work[5],self.work[1])
		elif loc == "W":
			self.setNumber(self.work[2],self.work[1],self.work[5],self.work[0],self.work[4],self.work[3])
	def get_top(self):
		return self.number[0]
	def get_right(self,n1,n2):
		if(n1 == self.number[0]):
			if(n2 == self.number[1]): return(self.number[2])
			elif(n2 == self.number[2]): return(self.number[4])
			elif(n2 == self.number[3]): return(self.number[1])
			elif(n2 == self.number[4]): return(self.number[3])
		elif(n1 == self.number[1]):
                        if(n2 == self.number[0]): return(self.number[3])
                        elif(n2 == self.number[2]): return(self.number[0])
                        elif(n2 == self.number[3]): return(self.number[5])
                        elif(n2 == self.number[5]): return(self.number[2])
		elif(n1 == self.number[2]):
                        if(n2 == self.number[0]): return(self.number[1])
                        elif(n2 == self.number[1]): return(self.number[5])
                        elif(n2 == self.number[4]): return(self.number[0])
                        elif(n2 == self.number[5]): return(self.number[4])
		elif(n1 == self.number[3]):
                        if(n2 == self.number[0]): return(self.number[4])
                        elif(n2 == self.number[1]): return(self.number[0])
                        elif(n2 == self.number[4]): return(self.number[5])
                        elif(n2 == self.number[5]): return(self.number[1])
		elif(n1 == self.number[4]):
                        if(n2 == self.number[0]): return(self.number[2])
                        elif(n2 == self.number[2]): return(self.number[5])
                        elif(n2 == self.number[3]): return(self.number[0])
                        elif(n2 == self.number[5]): return(self.number[3])
		elif(n1 == self.number[5]):
                        if(n2 == self.number[1]): return(self.number[3])
                        elif(n2 == self.number[2]): return(self.number[1])
                        elif(n2 == self.number[3]): return(self.number[4])
                        elif(n2 == self.number[4]): return(self.number[2])

	def equals(self,another):
		ret = False
		for i in range(len(self.order)):
			self.roll(self.order[i])
			flg = True
			for k in range(6):
				if self.number[k] != another.number[k]:
					flg = False
					break
			if flg == True:
				return True
		return ret

n = int(input())
dice = []
for _ in range(n):
	data = list(map(int,input().split()))
	dice.append(Dice())
	dice[_].setNumber(data[0],data[1],data[2],data[3],data[4],data[5])

flag = True
for _ in range(n-1):
	for i in range(_+1,n):
		if(dice[_].equals(dice[i])):
			flag = False
			break

print("Yes" if flag else "No")

Solution (Python):
class Dice(object):
    def __init__(self, s1, s2, s3, s4, s5, s6):
        self.s1 = s1
        self.s2 = s2
        self.s3 = s3
        self.s4 = s4
        self.s5 = s5
        self.s6 = s6
    def east(self):
        prev_s6 = self.s6
        self.s6 = self.s3
        self.s3 = self.s1
        self.s1 = self.s4
        self.s4 = prev_s6
    def west(self):
        prev_s6 = self.s6
        self.s6 = self.s4
        self.s4 = self.s1
        self.s1 = self.s3
        self.s3 = prev_s6
    def north(self):
        prev_s6 = self.s6
        self.s6 = self.s5
        self.s5 = self.s1
        self.s1 = self.s2
        self.s2 = prev_s6
    def south(self):
        prev_s6 = self.s6
        self.s6 = self.s2
        self.s2 = self.s1
        self.s1 = self.s5
        self.s5 = prev_s6
    def rotate(self):
        prev_s2 = self.s2
        self.s2 = self.s4
        self.s4 = self.s5
        self.s5 = self.s3
        self.s3 = prev_s2
    def top(self):
        return self.s1
    def front(self):
        return self.s2
    def right(self):
        return self.s3

    def isSameDice(self, anotherDice):
        if self.s1 == anotherDice.s1 and \
           self.s2 == anotherDice.s2 and \
           self.s3 == anotherDice.s3 and \
           self.s4 == anotherDice.s4 and \
           self.s5 == anotherDice.s5 and \
           self.s6 == anotherDice.s6:
            return True
        return False


dices = []
n = int(input())
for i in range(n):
    s1, s2, s3, s4, s5, s6 = map(int, input().split())
    dices.append(Dice(s1, s2, s3, s4, s5, s6))

flag = False
for idx1, dice1 in enumerate(dices):
    for idx2, dice2 in enumerate(dices):
        if idx1 == idx2:
            continue

        for i in range(6):
            if i % 2 == 0:
                dice2.north()
            else:
                dice2.west()

            for j in range(4):
                dice2.rotate()
                if dice2.isSameDice(dice1):
                    flag = True
                    break
            if flag:
                break
        if flag:
            break
    if flag:
        break

if flag:
    print("No")
else:
    print("Yes")

Solution (Python):
class Dice(object):
    def __init__(self, s1, s2, s3, s4, s5, s6):
        self.s1 = s1
        self.s2 = s2
        self.s3 = s3
        self.s4 = s4
        self.s5 = s5
        self.s6 = s6
    def east(self):
        prev_s6 = self.s6
        self.s6 = self.s3
        self.s3 = self.s1
        self.s1 = self.s4
        self.s4 = prev_s6
    def west(self):
        prev_s6 = self.s6
        self.s6 = self.s4
        self.s4 = self.s1
        self.s1 = self.s3
        self.s3 = prev_s6
    def north(self):
        prev_s6 = self.s6
        self.s6 = self.s5
        self.s5 = self.s1
        self.s1 = self.s2
        self.s2 = prev_s6
    def south(self):
        prev_s6 = self.s6
        self.s6 = self.s2
        self.s2 = self.s1
        self.s1 = self.s5
        self.s5 = prev_s6
    def rotate(self):
        prev_s2 = self.s2
        self.s2 = self.s4
        self.s4 = self.s5
        self.s5 = self.s3
        self.s3 = prev_s2
    def top(self):
        return self.s1
    def front(self):
        return self.s2
    def right(self):
        return self.s3

    def isSameDice(self, anotherDice):
        if self.s1 == anotherDice.s1 and \
           self.s2 == anotherDice.s2 and \
           self.s3 == anotherDice.s3 and \
           self.s4 == anotherDice.s4 and \
           self.s5 == anotherDice.s5 and \
           self.s6 == anotherDice.s6:
            return True
        return False


dices = []
n = int(input())
for i in range(n):
    s1, s2, s3, s4, s5, s6 = map(int, input().split())
    dices.append(Dice(s1, s2, s3, s4, s5, s6))

flag = False
for i in range(n):
    j = i+1
    while j < n:
        for k in range(6):
            if k % 2 == 0:
                dices[j].north()
            else:
                dices[j].west()

            for l in range(4):
                dices[j].rotate()
                if dices[j].isSameDice(dices[i]):
                    flag = True
                    break
            if flag:
                break
        if flag:
            break
        j += 1
    if flag:
        break

if flag:
    print("No")
else:
    print("Yes")

Solution (Python):
class Dice(object):
    def __init__(self, s1, s2, s3, s4, s5, s6):
        self.s1 = s1
        self.s2 = s2
        self.s3 = s3
        self.s4 = s4
        self.s5 = s5
        self.s6 = s6
    def east(self):
        prev_s6 = self.s6
        self.s6 = self.s3
        self.s3 = self.s1
        self.s1 = self.s4
        self.s4 = prev_s6
    def west(self):
        prev_s6 = self.s6
        self.s6 = self.s4
        self.s4 = self.s1
        self.s1 = self.s3
        self.s3 = prev_s6
    def north(self):
        prev_s6 = self.s6
        self.s6 = self.s5
        self.s5 = self.s1
        self.s1 = self.s2
        self.s2 = prev_s6
    def south(self):
        prev_s6 = self.s6
        self.s6 = self.s2
        self.s2 = self.s1
        self.s1 = self.s5
        self.s5 = prev_s6
    def rotate(self):
        prev_s2 = self.s2
        self.s2 = self.s4
        self.s4 = self.s5
        self.s5 = self.s3
        self.s3 = prev_s2
    def top(self):
        return self.s1
    def front(self):
        return self.s2
    def right(self):
        return self.s3

    def isSameDice(self, anotherDice):
        for i in range(6):
            if i % 2 == 0:
                self.north()
            else:
                self.west()
            for j in range(4):
                self.rotate()
                if self.s1 == anotherDice.s1 and \
                   self.s2 == anotherDice.s2 and \
                   self.s3 == anotherDice.s3 and \
                   self.s4 == anotherDice.s4 and \
                   self.s5 == anotherDice.s5 and \
                   self.s6 == anotherDice.s6:
                    return True
        return False


dices = []
n = int(input())
for i in range(n):
    s1, s2, s3, s4, s5, s6 = map(int, input().split())
    dices.append(Dice(s1, s2, s3, s4, s5, s6))

flag = False
for i in range(n):
    j = i+1
    while j < n:
        if dices[i].isSameDice(dices[j]):
            flag = True
            break
        j += 1

if flag:
    print("No")
else:
    print("Yes")

Solution (Python):
mask = [[i for i in range(6)], (1, 5, 2, 3, 0, 4), (2, 1, 5, 0, 4,3),(3, 1, 0, 5, 4, 2), (4, 0, 2, 3, 5, 1)]
mask += [[mask[1][i] for i in mask[1]]]

def set_top(dice, top):
    return [dice[i] for i in mask[top]]

def twist(dice):
    return [dice[i] for i in (0, 3, 1, 4, 2, 5)]

def equal(dice1, dice2):
    for i in range(6):
        tmp_dice = set_top(dice2, i)
        for _ in range(4):
            if dice1 == tmp_dice:
                return True
            tmp_dice = twist(tmp_dice)
    return False

def diff_check_all(dices, n):
    for i in range(n -1):
        for j in range(i + 1, n):
            if equal(dices[i], dices[j]):
                return False
    return True
            

n = int(input())
dices = [input().split() for _ in range(n)]

print('Yes' if diff_check_all(dices, n) else 'No')

Solution (Python):
mask = [[i for i in range(6)], (1, 5, 2, 3, 0, 4), (2, 1, 5, 0, 4,3),(3, 1, 0, 5, 4, 2), (4, 0, 2, 3, 5, 1)]
mask += [[mask[1][i] for i in mask[1]]]

def set_top(dice, top):
    return [dice[i] for i in mask[top]]

def twist(dice):
    return [dice[i] for i in (0, 3, 1, 4, 2, 5)]

def equal(dice1, dice2):
    if sorted(dice1) != sorted(dice2):
        return False
    
    for i in range(6):
        tmp_dice = set_top(dice2, i)
        for _ in range(4):
            if dice1 == tmp_dice:
                return True
            tmp_dice = twist(tmp_dice)
    return False

def diff_check_all(dices, n):
    for i in range(n -1):
        for j in range(i + 1, n):
            if equal(dices[i], dices[j]):
                return False
    return True
            

n = int(input())
dices = [input().split() for _ in range(n)]

print('Yes' if diff_check_all(dices, n) else 'No')

Solution (Python):
import sys
 
IDX_TBL = {1:[2,3,5,4], 2:[3,1,4,6], 3:[1,2,6,5],
           4:[1,5,6,2], 5:[1,3,6,4], 6:[2,4,5,3]}
OPPSITE_TBL = {1:6, 2:5, 3:4, 4:3, 5:2, 6:1}

def is_same(l1, l2):
    if l1[1] in l2:
        idx_list = [i for i,x in enumerate(l2) if x==l1[1]]
        for idx in idx_list:
            nstr1 = "".join([l1[i] for i in IDX_TBL[1]])
            nstr2 = "".join([l2[i] for i in IDX_TBL[idx]])
            if nstr2 in nstr1*2:
                if l1[6] == l2[7-l2.index(l1[1])]:
                    return True
    return False

n=input()
nums_list = []
for _ in range(n):
    nums_list.append(["_"] + raw_input().split(" "))

for i in range(0, n):
    for j in range(i+1, n):
        if is_same(nums_list[i], nums_list[j]):
            print "No"
            sys.exit(0)
print "Yes"

Solution (Python):
class Dice:
    def __init__(self, data):
        self.data = data

    def __eq__(self, value):

            
        for i in range(4):
            if self.data[0] == value.data[0]:
                for j in range(4):
                    if self.data == value.data:
                        return True
                    self.move('R')
            self.move('E')

        for i in range(4):
            if self.data[0] == value.data[0]:
                for j in range(4):
                    if self.data == value.data:
                        return True
                    self.move('R')
            self.move('N')
        return False

    def move(self, direction):
        if direction == 'E':
            self.data[0],self.data[3], self.data[5], self.data[2] = \
                self.data[3],self.data[5], self.data[2], self.data[0] 
        elif direction == 'N':
            self.data[0],self.data[1], self.data[5], self.data[4] = \
                self.data[1],self.data[5], self.data[4], self.data[0]
        elif direction == 'S':
            self.data[0],self.data[1], self.data[5], self.data[4] = \
                self.data[4],self.data[0], self.data[1], self.data[5]
        elif direction == 'W':
            self.data[0],self.data[2], self.data[5], self.data[3] = \
                self.data[2],self.data[5], self.data[3], self.data[0]
        elif direction == 'R':
            self.data[1],self.data[2], self.data[4], self.data[3] = \
                self.data[2],self.data[4], self.data[3], self.data[1]

a = []
for i in range(int(input())):
    a += [Dice(input().split())]

p = 'Yes'
for i in range(len(a) - 1):
    for j in range(i + 1,len(a)):
        if a[i] == a[j]:
            p = 'No'
            break
print(p)

Solution (Python):

"""
Created on Sun Feb 22 11:38:11 2015

@author: hirose
"""



from random import *
import copy

def command(hougaku, label):
    temp = {}
    temp = copy.deepcopy(label)
    if(hougaku == 'E'):
        label[1] = temp[4]
        label[3] = temp[1]
        label[4] = temp[6]
        label[6] = temp[3]
    elif(hougaku == 'W'):
        label[1] = temp[3]
        label[3] = temp[6]
        label[4] = temp[1]
        label[6] = temp[4]
    elif(hougaku == 'N'):
        label[1] = temp[2]
        label[2] = temp[6]
        label[5] = temp[1]
        label[6] = temp[5]
    elif(hougaku == 'S'):
        label[1] = temp[5]
        label[2] = temp[1]
        label[5] = temp[6]
        label[6] = temp[2]

if __name__=="__main__":
    n = input()
    label = {}
    for i in range(n):
        label[i] = {}
        temp = map(int, raw_input().split())
        for j in range(6):
            label[i][j+1] = temp[j]
        
    flag = 0
    for i in range(n-1):
        
        for j in range(i+1,n):
            count = 0
            
            while True:
                if(label[i] == label[j]):
                    flag = 1
                    break
                x = random()
                if(x < 0.25):
                    command('E', label[j])
                elif(x < 0.50):
                    command('W', label[j])
                elif(x < 0.75):
                    command('N', label[j])
                else:
                    command('S', label[j])
                count += 1
                if(count > 50):
                    break
            if flag == 1:
                break
        if flag == 1:
            break
    if(flag == 0):
        print 'Yes'
    else:
        print 'No'

Solution (Python):
import sys

class Dice(object):
    def __init__(self, dice):
        self.__dice = tuple(dice)

    def roll_north(self):
        self.__dice = (self.__dice[1], self.__dice[5], self.__dice[2],
                       self.__dice[3], self.__dice[0], self.__dice[4])

    def roll_south(self):
        self.__dice = (self.__dice[4], self.__dice[0], self.__dice[2],
                       self.__dice[3], self.__dice[5], self.__dice[1])

    def roll_west(self):
        self.__dice = (self.__dice[2], self.__dice[1], self.__dice[5],
                       self.__dice[0], self.__dice[4], self.__dice[3])

    def roll_east(self):
        self.__dice = (self.__dice[3], self.__dice[1], self.__dice[0],
                       self.__dice[5], self.__dice[4], self.__dice[2])

    def number(self, face_id):
        return self.__dice[face_id - 1]

    def equals(self, dice):
        for i in range(4):
            for j in range(4):
                if self.__dice == dice.__dice:
                    return True
                dice.roll_north()
            dice.roll_west()
        return False

n = int(sys.stdin.readline())
dices = []
for i in range(n):
    dices.append(Dice([int(a) for a in sys.stdin.readline().split()]))

for i in range(n):
    for j in range(i + 1, n):
        if dices[i].equals(dices[j]):
            print("No")
            exit(0)
print("Yes")

Solution (Python):
import copy, sys

class Dice(object):
    def __init__(self, nums):
        self.top, self.front, self.right, self.left, self.back, self.bottom = nums

    def roll(self, directions):
        if directions == 'E':
            self.top, self.right, self.left, self.bottom = self.left, self.top, self.bottom, self.right
            return

        if directions == 'N':
            self.top, self.front, self.back, self.bottom = self.front, self.bottom, self.top, self.back
            return

        if directions == 'S':
            self.top, self.front, self.back, self.bottom = self.back, self.top, self.bottom, self.front
            return

        if directions == 'W':
            self.top, self.right, self.left, self.bottom = self.right, self.bottom, self.top, self.left
            return

        self.roll(directions[0])
        self.roll(directions[1:])

    def equal(self, dice):
        d = copy.copy(self)

        if(dice.front == d.top):
            d.roll('S')
        elif(dice.front == d.right):
            d.roll('WS')
        elif(dice.front == d.left):
            d.roll('ES')
        elif(dice.front == d.back):
            d.roll('SS')
        elif(dice.front == d.bottom):
            d.roll('N')

        if(dice.top == d.right):
            d.roll('W')
        elif(dice.top == d.left):
            d.roll('E')
        elif(dice.top == d.bottom):
            d.roll('WW')

        return not(dice.top != d.top or dice.front != d.front or dice.right != d.right or dice.left != d.left or dice.back != d.back or dice.bottom != d.bottom)

if __name__ == '__main__':
    n = int(input())
    dices = []
    for i in range(n):
        d = Dice([int(_) for _ in input().split()])
        for dice in dices:
            if d.equal(dice):
                print('No')
                sys.exit()
        dices.append(d)
    print('Yes')

Solution (Python):
import copy, sys

class Dice(object):
    def __init__(self, nums):
        self.top, self.front, self.right, self.left, self.back, self.bottom = nums

    def roll(self, directions):
        if directions == 'E':
            self.top, self.right, self.left, self.bottom = self.left, self.top, self.bottom, self.right
            return

        if directions == 'N':
            self.top, self.front, self.back, self.bottom = self.front, self.bottom, self.top, self.back
            return

        if directions == 'S':
            self.top, self.front, self.back, self.bottom = self.back, self.top, self.bottom, self.front
            return

        if directions == 'W':
            self.top, self.right, self.left, self.bottom = self.right, self.bottom, self.top, self.left
            return

        self.roll(directions[0])
        self.roll(directions[1:])

    def equal(self, dice):
        d = copy.copy(self)

        if(dice.front == d.top):
            d.roll('S')
        elif(dice.front == d.right):
            d.roll('WS')
        elif(dice.front == d.left):
            d.roll('ES')
        elif(dice.front == d.back):
            d.roll('SS')
        elif(dice.front == d.bottom):
            d.roll('N')

        if(dice.top == d.right):
            d.roll('W')
        elif(dice.top == d.left):
            d.roll('E')
        elif(dice.top == d.bottom):
            d.roll('WW')

        return not(dice.top != d.top or dice.front != d.front or dice.right != d.right or dice.left != d.left or dice.back != d.back or dice.bottom != d.bottom)

if __name__ == '__main__':
    n = int(input())
    dices = [[] for _ in range(600)]
    for i in range(n):
        nums = [int(_) for _ in input().split()]
        d = Dice(nums)
        s = sum(nums)
        for dice in dices[s]:
            if d.equal(dice):
                print('No')
                sys.exit()
        dices[s].append(d)
    print('Yes')

Solution (Python):


import time
import sys
import io
import re
import math
import itertools



mod=1000000007

pi=3.141592653589
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]

ans=chk=0
a=[0,2,5,3]
b=[0,1,5,4]
c=[1,2,4,3]

def rolling(word,dice):
    if word=='S':
        dice[0],dice[1],dice[5],dice[4]=dice[4],dice[0],dice[1],dice[5]
    elif word=='N':
        dice[0],dice[1],dice[5],dice[4]=dice[1],dice[5],dice[4],dice[0]
    elif word=='W':
        dice[0],dice[2],dice[5],dice[3]=dice[2],dice[5],dice[3],dice[0]
    else:
        dice[0],dice[2],dice[5],dice[3]=dice[3],dice[0],dice[2],dice[5]
    return dice

def sortdice(dice1,dice2,p,q,r):
    for i in range(4):
        dice1[p[0]],dice1[p[1]],dice1[p[2]],dice1[p[3]]=dice1[p[1]],dice1[p[2]],dice1[p[3]],dice1[p[0]]
        for j in range(4):
            dice1[q[0]],dice1[q[1]],dice1[q[2]],dice1[q[3]]=dice1[q[1]],dice1[q[2]],dice1[q[3]],dice1[q[0]]
            for k in range(4):
                dice1[r[0]],dice1[r[1]],dice1[r[2]],dice1[r[3]]=dice1[r[1]],dice1[r[2]],dice1[r[3]],dice1[r[0]]
                if dice1==dice2:
                    print 'No'
                    exit()
    return 0
n=int(raw_input())
l=[]
for i in range(n):
    l.append(map(int,raw_input().split()))
for i in range(0,n-1):
    for j in range(i+1,n):
        sortdice(l[i],l[j],a,b,c)
print 'Yes'