トップページ -> AOJの解答例 -> ITP1_11の解答例

ITP1_11の解答例(Python)

サイコロに関する問題です. クラスを定義して解きます. 解答例はなかなか酷いです.

ITP1_11_A: Dice I

指定された通りにサイコロを転がした後に上面の数字を答えます. まずは,サイコロのclassを定義します. roll_keep_top()はC,Dで使います.

# ITP1_11
class Dice:
    def __init__(self,a,b,c,d,e,f):
        self.top = a
        self.bottom = f
        self.S = b
        self.N = e
        self.W = d
        self.E = c
    def roll_S(self):
        t = self.top
        s = self.S
        b = self.bottom
        n = self.N
        
        self.top = n
        self.S = t
        self.bottom = s
        self.N = b
        
    def roll_N(self):
        t = self.top
        s = self.S
        b = self.bottom
        n = self.N
        
        self.top = s
        self.S = b
        self.bottom = n
        self.N = t
        
    def roll_E(self):
        t = self.top
        e = self.E
        b = self.bottom
        w = self.W
        
        self.top = w
        self.E = t
        self.bottom = e
        self.W = b
        
    def roll_W(self):
        t = self.top
        e = self.E
        b = self.bottom
        w = self.W
        
        self.top = e
        self.E = b
        self.bottom = w
        self.W = t
        
    def roll_keep_top(self):
        s = self.S
        w = self.W
        n = self.N
        e = self.E
        
        self.S = e
        self.W = s
        self.N = w
        self.E = n
        
指示通りにサイコロを転がします.

# ITP1_11_A
a,b,c,d,e,f = map(int,input().split(" "))
commands = input()
dice = Dice(a,b,c,d,e,f)

for command in commands:
    if command == "N":
        dice.roll_N()
    elif command == "S":
        dice.roll_S()
    elif command == "W":
        dice.roll_W()
    elif command == "E":
        dice.roll_E()
        
print(dice.top)

ITP1_11_B: Dice II

上面を揃えてから側面を揃えます.

# ITP1_11_B
a,b,c,d,e,f = map(int,input().split(" "))
n = int(input())
dice = Dice(a,b,c,d,e,f)

for _ in range(n):
    t,s = map(int,input().split(" "))
    # 上面を揃える
    for i in range(4):
        dice.roll_N()
        if dice.top == t:
            break
    
    if dice.top != t:
        for i in range(4):
            dice.roll_W()
            if dice.top == t:
                break
            
    # 側面を揃える
    for _ in range(4):
        dice.roll_keep_top()
        if dice.S == s:
            print(dice.E)
            break
        

ITP1_11_C: Dice III

Bを少し改良しただけです.

# ITP1_11_C
import random
a,b,c,d,e,f = map(int,input().split(" "))
t,s,E,w,n,B = map(int,input().split(" "))
dice = Dice(a,b,c,d,e,f)

diff = True
# 上面を揃える
for i in range(4):
    dice.roll_N()
    if dice.top == t:
        break

if dice.top != t:
    for i in range(4):
        dice.roll_W()
        if dice.top == t:
            break

# 側面を揃える
for _ in range(4):
    dice.roll_keep_top()
    if dice.S == s and dice.E == E and dice.N == n and dice.W == w and dice.bottom == B:
        print("Yes")
        diff = False
        break
            
if diff:
    print("No")

ITP1_11_D: Distance II

Cをループで回します.

# ITP1_11_D
n = int(input())
a,b,c,d,e,f = map(int,input().split(" "))
same = False
for _ in range(n-1):
    t,s,E,w,n,B = map(int,input().split(" "))
    dice = Dice(a,b,c,d,e,f)

    diff = True
    # 上面を揃える
    for i in range(4):
        dice.roll_N()
        if dice.top == t:
            break

    if dice.top != t:
        for i in range(4):
            dice.roll_W()
            if dice.top == t:
                break

    # 側面を揃える
    for _ in range(4):
        dice.roll_keep_top()
        if dice.S == s and dice.E == E and dice.N == n and dice.W == w and dice.bottom == B and dice.top == t:
            same = True

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

<- 前へ戻る 【目次に戻る】