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



Sample input:
3
1 2 3 4 5 6
6 2 4 3 5 1
6 5 4 3 2 1


Sample output:
No

Submission status: Accepted

Solution code:
def check(x, y):
    for i in range(6):
        order = [0, 0, 0]
        for j in range(3):
            if {x[j], x[-j-1]} != {y[z[i][j]], y[-z[i][j]-1]}:
                break
            if x[j] == y[z[i][j]]:
                order[j] = 1
                if x[j] == x[-j-1]:
                    order[j] = 2
        else:
            if 2 in order or (i < 3) == sum(order) % 2:
                return True
    return False

n = int(input())
x = [[] for i in range(n)]
for i in range(n):
    x[i] = list(map(int, input().split()))
z = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 2, 1], [2, 1, 0], [1, 0, 2]]
for i in range(n):
    for j in range(i+1, n, 1):
        if check(x[i], x[j]):
            break
    else:
        continue
    print('No')
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
def check(x, y):
    for i in range(6):
        order = [0, 0, 0]
        for j in range(3):
            if {x[j], x[-j-1]} != {y[z[i][j]], y[-z[i][j]-1]}:
                break
            if x[j] == y[z[i][j]]:
                order[j] = 1
                if x[j] == x[-j-1]:
                    order[j] = 2
        else:
            if 2 in order or (i < 3) == sum(order) % 2:
                return True
    return False

n = int(input())
x = [[] for i in range(n)]
for i in range(n):
    x[i] = input().split()
z = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 2, 1], [2, 1, 0], [1, 0, 2]]
for i in range(n):
    for j in range(i+1, n, 1):
        if check(x[i], x[j]):
            break
    else:
        continue
    print('No')
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
def check(x, y):
    for i in range(6):
        order = [0, 0, 0]
        for j in range(3):
            if {x[j], x[-j-1]} != {y[a[i][j]], y[-a[i][j]-1]}:
                break
            if x[j] == y[a[i][j]]:
                order[j] = 1
                if x[j] == x[-j-1]:
                    order[j] = 2
        else:
            if 2 in order or (i < 3) == sum(order) % 2:
                return True
    return False

a = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 2, 1], [2, 1, 0], [1, 0, 2]]
n = int(input())
x = [input().split() for i in range(n)]
for i in range(n):
    for j in range(i+1, n, 1):
        if check(x[i], x[j]):
            break
    else:
        continue
    print('No')
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
def check(x, y):
    for i in range(6):
        order = [0, 0, 0]
        for j in range(3):
            if {x[j], x[-j-1]} != {y[a[i][j]], y[-a[i][j]-1]}:
                break
            if x[j] == y[a[i][j]]:
                order[j] = 1
                if x[j] == x[-j-1]:
                    order[j] = 2
        else:
            if 2 in order or (i < 3) == sum(order) % 2:
                return True
    return False

a = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 2, 1], [2, 1, 0], [1, 0, 2]]
n = int(input())
x = [input().split() for i in range(n)]
for i in range(n):
    for j in range(i+1, n):
        if check(x[i], x[j]):
            break
    else:
        continue
    print('No')
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
def check(x, y):
    for i in range(6):
        order = [0, 0, 0]
        for j in range(3):
            k = a[i][j]
            if {x[j], x[-j-1]} != {y[k], y[-k-1]}:
                break
            if x[j] == y[k]:
                order[j] = 1
                if x[j] == x[-j-1]:
                    order[j] = 2
        else:
            if 2 in order or (i < 3) == sum(order) % 2:
                return True
    return False

a = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 2, 1], [2, 1, 0], [1, 0, 2]]
n = int(input())
x = [input().split() for i in range(n)]
for i in range(n):
    for j in range(i+1, n):
        if check(x[i], x[j]):
            break
    else:
        continue
    print('No')
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
import sys
def check(x, y):
    for i in range(6):
        order = [0, 0, 0]
        for j in range(3):
            k = a[i][j]
            if {x[j], x[-j-1]} != {y[k], y[-k-1]}:
                break
            if x[j] == y[k]:
                order[j] = 1
                if x[j] == x[-j-1]:
                    order[j] = 2
        else:
            if 2 in order or (i < 3) == sum(order) % 2:
                return True
    return False

a = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 2, 1], [2, 1, 0], [1, 0, 2]]
n = int(sys.stdin.readline())
x = [input().split() for i in range(n)]
for i in range(n):
    for j in range(i+1, n):
        if check(x[i], x[j]):
            break
    else:
        continue
    print('No')
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
import sys
def check(x, y):
    for i in range(6):
        order = [0, 0, 0]
        for j in range(3):
            k = a[i][j]
            if {x[j], x[-j-1]} != {y[k], y[-k-1]}:
                break
            if x[j] == y[k]:
                order[j] = 1
                if x[j] == x[-j-1]:
                    order[j] = 2
        else:
            if 2 in order or (i < 3) == sum(order) % 2:
                return True
    return False

a = [[0, 1, 2], [2, 0, 1], [1, 2, 0], [0, 2, 1], [2, 1, 0], [1, 0, 2]]
n = int(sys.stdin.readline())
x = [sys.stdin.readline().split() for i in range(n)]
for i in range(n):
    for j in range(i+1, n):
        if check(x[i], x[j]):
            break
    else:
        continue
    print('No')
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
n=int(input())
 
def rot(d, r):
    if r == 'N':
        return (d[1], d[5], d[2], d[3], d[0], d[4])
    if r == 'E':
        return (d[3], d[1], d[0], d[5], d[4], d[2])
    if r == 'W':
        return (d[2], d[1], d[5], d[0], d[4], d[3])
    if r == 'S':
        return (d[4], d[0], d[2], d[3], d[5], d[1])
    return d
 
def gen_all( d1 ):
    ds = { d1 }
    while 1:
        ds2 = { rot(d, r) for d in ds for r in list('NEWS') }.union(ds)
        if ds == ds2: break
        ds = ds2
    return ds
 
ds = {}
 
for i in range(n):
    d=tuple(map(int,input().split()))
    ds[i] = (d, gen_all(d))
 
dep=any ([ ds[i][0] in ds[j][1] for i in range(n) for j in range(i+1,n) ])
     
print('No' if dep else 'Yes')

Submission status: Accepted

Solution code:
import sys
n=int(input())
x1=[int(i)for i in input().split()]
for i in range(n-1):
    x2=[int(i)for i in input().split()]
    for i in range(6):
        if x2[i]==x1[0]:
            count=i
            if x2[5-i]==x1[5]:
                break
    if x2[count]==x1[0] and x2[5-count]==x1[5]:
        if count==0 or count==5:
            if count==5:
                for i in range(2):
                    x2[1],x2[0]=x2[0],x2[1]
                    x2[5],x2[1]=x2[1],x2[5]
                    x2[4],x2[5]=x2[5],x2[4]
        else:
            if count==1 or count==4:
                while x1[0]!=x2[0] or x1[5]!=x2[5]:
                    x2[1],x2[0]=x2[0],x2[1]
                    x2[5],x2[1]=x2[1],x2[5]
                    x2[4],x2[5]=x2[5],x2[4]
            else:
                while x1[0]!=x2[0] or x1[5]!=x2[5]:
                    x2[3],x2[0]=x2[0],x2[3]
                    x2[5],x2[3]=x2[3],x2[5]
                    x2[2],x2[5]=x2[5],x2[2]
        for i in range(4):
            if x2[2]==x1[2] and x2[3]==x1[3] and x2[4]==x1[4] and x2[1]==x1[1] :
                print('No')
                sys.exit()
            else:
                x2[1],x2[2]=x2[2],x2[1]
                x2[2],x2[4]=x2[4],x2[2]
                x2[4],x2[3]=x2[3],x2[4]
print('Yes')

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????? 4

?¬?????±??????????????????????????????????????????¢??????????????\??¬????????§????????????????????°????????????????????????????????????
   +--+
 ???|???|
+--+--+--+--+
|???|???|???|???|
+--+--+--+--+
 ???|???|
   +--+

"""

class Dice:
    T = 0
    S = 1
    E = 2
    W = 3
    N = 4
    B = 5

    """ ????????????????????? """
    def __init__(self):
        """ ????????????????????? """
        self.side = [1, 2, 3, 4, 5, 6]
        self.top = 0

    def setDice(self, arr):
        """ ??¢?????????????????? """
        self.side = arr[0:6]

    def Turn(self, dir):
        """ ?????????????????¢?????? """
        s = self.side

        if dir == "N":     # ????????¢??????
            #    Top 0     South 1   East 2    West 3    North 4   Bottom 5
            t = [s[Dice.S],s[Dice.B],s[Dice.E],s[Dice.W],s[Dice.T],s[Dice.N]]
        elif dir == "S": # ????????¢??????
            t = [s[Dice.N],s[Dice.T],s[Dice.E],s[Dice.W],s[Dice.B],s[Dice.S]]
        elif dir == "E": # ??±?????¢??????
            t = [s[Dice.W],s[Dice.S],s[Dice.T],s[Dice.B],s[Dice.N],s[Dice.E]]
        elif dir == "W": # ?\??????¢??????
            t = [s[Dice.E],s[Dice.S],s[Dice.B],s[Dice.T],s[Dice.N],s[Dice.W]]
        elif dir == "L": # ???????????¢
            t = [s[Dice.T],s[Dice.W],s[Dice.S],s[Dice.N],s[Dice.E],s[Dice.B]]
        elif dir == "R": # ???????????¢
            t = [s[Dice.T],s[Dice.E],s[Dice.N],s[Dice.S],s[Dice.W],s[Dice.B]]

        self.side = t
#        print("{0} {1}".format(dir, self.State()))

    def Uturns(self, tval, sval):
        """ ?????¢??¨?????¢???????????????????????????????????§?????¢ """
    
        if self.side[Dice.E] == tval:
            self.Turn("W")
        elif self.side[Dice.W] == tval:
            self.Turn("E")
        elif self.side[Dice.N] == tval:
            self.Turn("S")
        elif self.side[Dice.S] == tval:
            self.Turn("N")
        elif self.side[Dice.B] == tval:
            self.Turn("N")
            self.Turn("N")

        for i in range(3): # ?????¢?????????????????????????????§????????????
            if self.side[Dice.S] == sval:
                break
            self.Turn("L")
            

    def Search(self, tval, sval, dir):
        """ ?????¢??¨?????¢?????????????????????????????¢??????????????? """
        self.Uturns(tval,sval)
        return self.side[dir]

    def State(self):
            return "T0:{0} S1:{1} W2:{2} E3:{3} N4:{4} B5:{5}".format(self.side[0],self.side[1], self.side[2],self.side[3], self.side[4],self.side[5])

# ?????????
d = []
num = int(input().strip())

for i in range(num): # ??????????????????
    tmp = Dice()
    tmp.setDice(list(map(int,input().strip().split())))
    d.append(tmp)

ret = "Yes"

for i in range(num-1):
    for j in range(i + 1,num):
        # ??????????????¢?????°???????????????????¢??????????
        s1 = set(d[i].side)
        s2 = set(d[j].side)
        if (s1 ^ s2): # ??????????????????
            break;

        d[j].Uturns(d[i].side[Dice.T], d[i].side[Dice.S])
        s1 = d[i].State()
        s2 = d[j].State()
        if s1 == s2:
            ret = "No"
            break

    if ret == "No":
        break

print(ret)

Submission status: Accepted

Solution code:
class dice:
	def __init__(self,data):
		self.faces = data
	
	def turn(self,direction):
		if direction == 'N':
			idx = [1,5,2,3,0,4]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'E':
			idx = [3,1,0,5,4,2]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'W':
			[self.turn('E') for i in range(3)]
		elif direction == 'c':
			self.turn('N')
			self.turn('W')
			self.turn('S')

def issame(d1,d2):
	for o in 'cccWcccEcccNcccWcccWccc':
		if d1.faces == d2.faces:
			break
		else:
			d2.turn(o)
	if d1.faces == d2.faces:
		return True
	else:
		return False

if __name__ == '__main__':
	from itertools import combinations
	n = int(input())
	dices = []
	for i in range(n):
		dices.append(dice([int(s) for s in input().split()]))
	ans = True
	for d1,d2 in combinations(dices,2):
		if issame(d1,d2):
			ans = False
			break
	print({True:'Yes',False:'No'}[ans])

Submission status: Accepted

Solution code:
class dice:
	def __init__(self,data):
		self.faces = data
	
	def turn(self,direction):
		if direction == 'N':
			idx = [1,5,2,3,0,4]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'E':
			idx = [3,1,0,5,4,2]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'W':
			[self.turn('E') for i in range(3)]
		elif direction == 'c':
			self.turn('N')
			self.turn('W')
			self.turn('S')

def isunique(d1,d2):
	for o in 'cccWcccEcccNcccWcccWccc':
		if d1.faces == d2.faces:
			break
		else:
			d2.turn(o)
	if d1.faces == d2.faces:
		return False
	else:
		return True

if __name__ == '__main__':
	from itertools import combinations
	n = int(input())
	dices = []
	for i in range(n):
		dices.append(dice([int(s) for s in input().split()]))
	ans = True
	check = [isunique(d1,d2) for d1,d2 in combinations(dices,2)]
	if all(check):
		print('Yes')
	else:
		print('No')

Submission status: Accepted

Solution code:
class dice:
	def __init__(self,data):
		self.faces = data
	
	def turn(self,direction):
		if direction == 'N':
			idx = [1,5,2,3,0,4]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'S':
			[self.turn('N') for i in range(3)]
		elif direction == 'E':
			idx = [3,1,0,5,4,2]
			self.faces = [self.faces[i] for i in idx]
		elif direction == 'W':
			[self.turn('E') for i in range(3)]
		elif direction == 'c':
			self.turn('N')
			self.turn('W')
			self.turn('S')

def min_side(dice):
	num = [int(''.join([str(i) for i in dice.faces]))]
	for o in 'cccWcccEcccNcccWcccWccc':
		dice.turn(o)
		num.append(int(''.join([str(i) for i in dice.faces])))
	return min(num)

if __name__ == '__main__':
	from itertools import combinations
	n = int(input())
	dices = []
	for i in range(n):
		dices.append(dice([int(s) for s in input().split()]))
	ans = True
	mins1 = [min_side(d) for d in dices]
	mins2 = list(set(mins1))
	if len(mins1) == len(mins2):
		print('Yes')
	else:
		print('No')

Submission status: Accepted

Solution code:
class Dice:
    __slots__ = ['n_list', 'pair_list']

    def __init__(self, n_tup):
        self.n_list = list(n_tup)
        self.pair_list = [self.return_dice_round_pair(self.n_list, 2, 3, 5, 4),
                          self.return_dice_round_pair(self.n_list, 1, 4, 6, 3),
                          self.return_dice_round_pair(self.n_list, 1, 2, 6, 5),
                          self.return_dice_round_pair(self.n_list, 1, 5, 6, 2),
                          self.return_dice_round_pair(self.n_list, 1, 3, 6, 4),
                          self.return_dice_round_pair(self.n_list, 2, 4, 5, 3)
                          ]

    @staticmethod
    def roll_switch(n_list, from_idx_list, to_idx_list):
        n_list[from_idx_list[0] - 1], \
        n_list[from_idx_list[1] - 1], \
        n_list[from_idx_list[2] - 1], \
        n_list[from_idx_list[3] - 1] \
            = n_list[to_idx_list[0] - 1], \
              n_list[to_idx_list[1] - 1], \
              n_list[to_idx_list[2] - 1], \
              n_list[to_idx_list[3] - 1]
        return n_list

    def roll(self, direction):
        if direction == "N":
            self.n_list = self.roll_switch(self.n_list, [1, 2, 6, 5], [2, 6, 5, 1])

        elif direction == "E":
            self.n_list = self.roll_switch(self.n_list, [1, 3, 6, 4], [4, 1, 3, 6])

        if direction == "S":
            self.n_list = self.roll_switch(self.n_list, [1, 2, 6, 5], [5, 1, 2, 6])

        if direction == "W":
            self.n_list = self.roll_switch(self.n_list, [1, 3, 6, 4], [3, 6, 4, 1])

    def spin(self):
        self.n_list = self.roll_switch(self.n_list, [2, 3, 5, 4], [3, 5, 4, 2])

    @staticmethod
    def return_dice_round_pair(n_list, a, b, c, d):
        return [(n_list[a - 1], n_list[b - 1]),
                (n_list[b - 1], n_list[c - 1]),
                (n_list[c - 1], n_list[d - 1]),
                (n_list[d - 1], n_list[a - 1])]

    def return_right_num(self, top, forward):
        for i in range(6):
            if (top, forward) in self.pair_list[i]:
                return self.n_list[i]


def comparison_dice(dice_src, dice_tmp):
    # ?????¢????????????
    def compare_back(dice_tmp, dice_src):
        if dice_tmp.n_list[5] != dice_src.n_list[5]:
            return False
        else:
            if compare_with_spin(dice_tmp, dice_src):
                return True
            else:
                return False

    def compare_with_spin(dice_tmp, dice_src):
        # ??????4??¢???????????????????????§spin?????????
        for k in range(4):
            dice_tmp.spin()
            if dice_tmp.n_list == dice_src.n_list:
                return True
        else:
            return False

    # ?????¢???????????????
    for i in range(4):
        dice_tmp.roll("W")
        if dice_tmp.n_list[0] == dice_src.n_list[0]:
            if compare_back(dice_tmp, dice_src):
                return True
    else:
        for j in range(4):
            dice_tmp.roll("N")
            if dice_tmp.n_list[0] == dice_src.n_list[0]:
                if compare_back(dice_tmp, dice_src):
                    return True
        else:
            return False


cnt = int(input())
dice_line = []
for i in range(cnt):
    dice_line.append([int(x) for x in input().split()])

flg = False
for j in range(cnt):
    for k in range(j + 1, cnt):
        if comparison_dice(Dice(dice_line[j]), Dice(dice_line[k])):
            print("No")
            flg = True
            break
    if flg: break
else:
    print("Yes")

Submission status: Accepted

Solution code:
def roll(die, d):
    if d == "E":
        return [die[3], die[1], die[0], die[5], die[4], die[2]]
    if d == "N":
        return [die[1], die[5], die[2], die[3], die[0], die[4]]
    if d == "S":
        return [die[4], die[0], die[2], die[3], die[5], die[1]]
    if d == "W":
        return [die[2], die[1], die[5], die[0], die[4], die[3]]
    if d == "L":
        return [die[0], die[3], die[1], die[4], die[2], die[5]]
    if d == "R":
        return [die[0], die[2], die[4], die[1], die[3], die[5]]

def checkdice(die1, die2):
    ans = False
    for d in "RRRNRRRNRRRSRRRSRRRNRRRR":
        if die1 == die2:
            ans = True
            break
        die2 = roll(die2, d)
    return ans

dice = []
msg = "Yes"
n = int(input())
for _ in range(n):
    dice.append(list(map(int, input().split())))
for i in range(n):
    for j in range(i + 1, n):
        if checkdice(dice[i], dice[j]):
            msg = "No"
print(msg)

Submission status: Accepted

Solution code:
def roll(die, d):
    if d == "E":
        return [die[3], die[1], die[0], die[5], die[4], die[2]]
    if d == "N":
        return [die[1], die[5], die[2], die[3], die[0], die[4]]
    if d == "S":
        return [die[4], die[0], die[2], die[3], die[5], die[1]]
    if d == "W":
        return [die[2], die[1], die[5], die[0], die[4], die[3]]
    if d == "L":
        return [die[0], die[3], die[1], die[4], die[2], die[5]]
    if d == "R":
        return [die[0], die[2], die[4], die[1], die[3], die[5]]

def checkdice(die1, die2):
    ans = False
    for d in "RRRNRRRNRRRSRRRSRRRNRRRR":
        if die1 == die2:
            ans = True
            break
        die2 = roll(die2, d)
    return ans

dice = []
msg = "Yes"
n = int(input())
for _ in range(n):
    dice.append(list(map(int, input().split())))
for i in range(n):
    for j in range(i + 1, n):
        if checkdice(dice[i], dice[j]):
            msg = "No"
            break
    if msg == "No":
        break
print(msg)

Submission status: Accepted

Solution code:
# Aizu Problem ITP_1_11_D: Dice IV
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input2.txt", "rt")



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

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:
                    return True
    return False

def all_different(N, dices):
    for i in range(N - 1):
        dice1 = dices[i]
        for j in range(i + 1, N):
            dice2 = dices[j]
            if sortdice(dice1, dice2, a, b, c):
                return False
    return True

    
N = int(input())
dices = []
for _ in range(N):
    dices.append(list(map(int,input().split())))
print("Yes" if all_different(N, dices) else "No")

Submission status: Accepted

Solution code:
# your code goes here
#dice4
def r(D,s):
    if s==0:
        t=D[0]
        D[0]=D[1]
        D[1]=D[5]
        D[5]=D[4]
        D[4]=t
    elif s==2:
        t=D[0]
        D[0]=D[4]
        D[4]=D[5]
        D[5]=D[1]
        D[1]=t        
    elif s==3:
        t=D[0]
        D[0]=D[3]
        D[3]=D[5]
        D[5]=D[2]
        D[2]=t        
    elif s==1:
        t=D[0]
        D[0]=D[2]
        D[2]=D[5]
        D[5]=D[3]
        D[3]=t
    elif s==4:
        D=r(r(D,1),1)
    return D
def ch(C,D):
    k=0
    while k<6 and C[k]==D[k]:
        k+=1
    if k==6:
        return 1
    else:
        return 0
def d3(C,D):
    nd=0
    if ch(C,D)==1:
        nd=1
    i=0
    while i<4 and nd==0:
        D=r(D,i)
        j=i+1
        j%=4
        k=0
        if ch(C,D)==1:
            nd=1
 #   if i==0:
    #    print(D)
        while k<3 and nd==0:
            D=r(D,j)
            if ch(C,D)==1:
                nd=1
            k+=1
  #      if i==0:
        #    print(D)
        D=r(D,j)
 #   if i==0:
   #     print(D)
        m=i+2
        m%=4
 #   if i==0:
    #    print(m)    
        D=r(D,m)
 #   if i==0:
 #       print(D)
        i+=1
    i=0
    while nd==0 and i<4:
        D=r(D,0)
        D=r(D,1)
        D=r(D,0)
        if ch(C,D)==1:
            nd=1
        D=r(r(D,0),0)
        if ch(C,D)==1:
            nd=1
        i+=1
    if nd==1:
        return 1
    else:
        return 0
n=int(input())
Dl=[[int(i) for i in input().split()] for i in range(n)]
i1=0
ne=1
while i1<n-1 and ne==1:
    i2=i1+1
    while i2<n and ne==1:
        if d3(Dl[i1],Dl[i2])==1:
            ne=0
        i2+=1
    i1+=1
if ne==0:
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
def North(dice):
    w = dice[0]
    dice[0] = dice[1]
    dice[1] = dice[5]
    dice[5] = dice[4]
    dice[4] = w
    return dice
def East(dice):
    w = dice[1]
    dice[1] = dice[2]
    dice[2] = dice[4]
    dice[4] = dice[3]
    dice[3] = w
    return dice

def West(dice):
    w = dice[0]
    dice[0] = dice[2]
    dice[2] = dice[5]
    dice[5] = dice[3]
    dice[3] = w
    return dice

maxloop = 4
Ncnt = 0
Ecnt = 0
Wcnt = 0

n = int(input())
Dice = [['' for i2 in range(6)] for i1 in range(n)]
for i in range(n):
    Dice[i] = input().split()

Flag = False
Nloop = True
Eloop = True
Wloop = True

for i in range(n - 1):
    for j in range(i + 1, n):
        while Eloop:
            while Nloop:
                while Wloop:
                    if Dice[i] == Dice[j]:
                        Flag = True
                        Nloop = False
                        Eloop = False
                        Wloop = False
                        break
                    Dice[i] = West(Dice[i])
                    Wcnt += 1
                    if Wcnt == maxloop:
                        Wloop = False
                Dice[i] = North(Dice[i])
                Wcnt = 0
                Wloop = True
                Ncnt += 1
                if Ncnt == maxloop:
                    Nloop = False
            Dice[i] = East(Dice[i])
            Ncnt = 0
            Nloop = True
            Ecnt += 1
            if Ecnt == maxloop:
                Eloop = False
        Ecnt = 0
        Eloop = True
        if Flag == True:
            print("No")
            break
    else:
        continue
    break

if Flag == False:
    print("Yes")

Submission status: Accepted

Solution code:
class Checker:
    d = {
        1: { 2: (3,4,5,6), 3: (5,2,4,6), 4: (2,5,3,6), 5: (4,3,2,6) },
        2: { 1: (4,3,6,5), 3: (1,6,4,5), 4: (6,1,3,5), 6: (3,4,1,5) },
        3: { 1: (2,5,6,4), 2: (6,1,5,4), 5: (1,6,2,4), 6: (5,2,1,4) },
        4: { 1: (5,2,6,3), 2: (1,6,5,3), 5: (6,1,2,3), 6: (2,5,1,3) },
        5: { 1: (3,4,6,2), 3: (6,1,4,2), 4: (1,6,3,2), 6: (4,3,1,2) },
        6: { 2: (4,3,5,1), 3: (2,5,4,1), 4: (5,2,3,1), 5: (3,4,2,1) }
        }

    def __init__(self, s):
        l = s.split()
        self.tbl = {}
        for c,i in zip(l, range(1,7)):
            if c in self.tbl:
                self.tbl[c].append(i)
            else:
                self.tbl[c] = [i]
        self.pip = dict(zip(range(1,7), l))

    def __call__(self, s):
        l = s.split()

        if not (l[0] in self.tbl):
            return False
        if not (l[1] in self.tbl):
            return False

        for i0 in self.tbl[l[0]]:
            for i1 in self.tbl[l[1]]:
                if i1 in self.d[i0]:
                    for i,j in zip(self.d[i0][i1], range(2,6)):
                        if self.pip[i] != l[j]:
                            break
                    else:
                        return True
        return False

def g():
    n = int(input()) - 1
    c = Checker(input())
    for _ in range(n):
        yield c(input())

if any(g()):
    print("No")
else:
    print("Yes")

Submission status: Accepted

Solution code:
class Dice3():
    def __init__(self, v): # v [Top, Front, Right, Left, Back, Bottom ]
        
        self.dv = [(v[i], v[5-i],
                    ''.join([v[l] for l in ((i+2)%6, (i+4)%6, 5-(i+2)%6, 5-(i+4)%6, (i+2)%6, (i+4)%6, 5-(i+2)%6)]) )
                   for i in  range(6)]
    def IsDifferent(self, d):
        for i in range(6):
            if d[0] != self.dv[i][0] or d[5] != self.dv[i][1]: continue
            if not( ''.join(d[1:3]) in self.dv[i][2]): continue
            p = self.dv[i][2].index( ''.join(d[1:3]) )
            if d[4] != self.dv[i][2][p+2] or d[3] != self.dv[i][2][p+3]: continue
            return False
        return True


dices = [input().split() for n in range(int(input()))]
for i, D1 in enumerate(dices[:-1]):
    d = Dice3(D1)
    for D2 in dices[i+1:]:
        if not d.IsDifferent(D2):
            print('No')
            break
    else:
        continue
    break
else:
    print('Yes')

Submission status: Accepted

Solution code:
class Dice:
    def __init__(self, faces = None):
        self.dice = {'N':2, 'E':4, 'S':5, 'W':3}
        self.faces = {1:1, 2:2, 3:3, 4:4, 5:5, 6:6}
        self.currTop = 1
        if faces is not None:
            self.faces = faces

    def top(self):
        return self.currTop

    def topFace(self):
        return self.faces[self.currTop]

    def bottom(self):
        return 7 - self.currTop

    def bottomFace(self):
        return self.faces[self.bottom()]

    def rot(self, direction):
        newTop = self.dice[direction]
        currTop = self.currTop
        self.currTop = newTop
        if direction == 'N':
            self.dice['N'] = 7 - currTop
            self.dice['S'] = currTop
        elif direction == 'S':
            self.dice['N'] = currTop
            self.dice['S'] = 7 - currTop
        elif direction == 'E':
            self.dice['E'] = 7 - currTop
            self.dice['W'] = currTop
        elif direction == 'W':
            self.dice['E'] = currTop
            self.dice['W'] = 7 - currTop

    def yaw(self, direction):
        newDice = {}
        if direction == 'E':
            newDice['N'] = self.dice['E']
            newDice['E'] = self.dice['S']
            newDice['S'] = self.dice['W']
            newDice['W'] = self.dice['N']
        if direction == 'W':
            newDice['N'] = self.dice['W']
            newDice['E'] = self.dice['N']
            newDice['S'] = self.dice['E']
            newDice['W'] = self.dice['S']
        self.dice = newDice

def compareDice(d1, d2):
    d2indexes = [d2index for d2index,d2face in d2.faces.items() if d1.topFace() == d2face]

    for d2topIndex in d2indexes:
        if dbg: print('d2topIndex:%d' % (d2topIndex) )

        # search d1.topFace() in d2 and rotate to make d2.topFace() equal to d1.topFace().
        key = [k for k,v in d2.dice.items() if d2topIndex == v]
        if len(key) == 0:
            d2.rot('N')
            key = [k for k,v in d2.dice.items() if d2topIndex == v]
        d2.rot(key[0])

        if d1.topFace() != d2.topFace() or d1.bottomFace() != d2.bottomFace():
            continue

        # rotate(yaw) to make frontIdx front.
        cnt = 0
        while True:
            if dbg: print('cnt:%d' % (cnt) )
            if cnt > 4:
                break
            d2.yaw('E')
            cnt += 1

            # compare two dices.
            d1State = {k:d1.faces[v] for k,v in d1.dice.items()}
            d2State = {k:d2.faces[v] for k,v in d2.dice.items()}
            if d1State == d2State:
                return True

    return False

dbg = False

n = int(input())
dices = []

for i in range(n):
    faces = {k:v for k,v in zip(range(1,7), map(int, input().split()))}
    dices.append(Dice(faces))

for i in range(n):
    for j in range(i+1,n):
        if dbg: print('%d-%d' % (i,j) )
        ret = compareDice(dices[i], dices[j])
        if dbg: print(ret)
        if ret == True:
            print('No')
            exit()

print('Yes')