Tic-tac-toe is played by two players A and B on a 3 x 3 grid.
Here are the rules of Tic-Tac-Toe:
Given an array moves where each element is another array of size 2 corresponding to the row and column of the grid where they mark their respective character in the order in which A and B play.
Return the winner of the game if it exists (A or B), in case the game ends in a draw return "Draw", if there are still movements to play return "Pending".
You can assume that moves is valid (It follows the rules of Tic-Tac-Toe), the grid is initially empty and A will play first.
class Solution:
def tictactoe(self, moves: List[List[int]]) -> str:
AB = 0 # A == 0, B == 1
# row, column, diagonal
rcd = [[[0,0], [0,1], [0,2]], [[1,0], [1,1], [1,2]], [[2,0], [2,1], [2,2]],
[[0,0], [1,0], [2,0]], [[0,1], [1,1], [2,1]], [[0,2], [1,2], [2,2]],
[[0,0], [1,1], [2,2]], [[0,2], [1,1], [2,0]]]
A = []
B = []
for i in range(len(moves)):
if AB == 0:
A.append(moves[i])
else:
B.append(moves[i])
AB = not AB
for i in range(len(rcd)):
cntA = 0
cntB = 0
for xy in rcd[i]:
if xy in A:
cntA += 1
if xy in B:
cntB += 1
if cntA == 3:
return "A"
if cntB == 3:
return "B"
if len(moves) == 9:
return "Draw"
return "Pending"
파이썬 문자열은 인덱스 값으로 바로 수정할 수 없기 때문에... (str[i] = "a"
X)
["X", "O", "X"]
식으로 리스트에 저장한 후
가로, 세로, 대각선 모두 같은지 확인하려 했지만 너무 코드가 길어져서...
3 * 3 이니까 그냥 좌표들을 rcd
에 미리 저장해서 확인함
누가 빙고 되는지 확인해서 winner return
winner 가 없을 경우, moves
가 9 번 꽉 차면 return Draw
/ 아니면 return Pending
Given two numbers, hour and minutes. Return the smaller angle (in degrees) formed between the hour and the minute hand.
class Solution:
def angleClock(self, hour: int, minutes: int) -> float:
# 시침은 1 분마다 0.5 도씩 / 분침은 1 분마다 6 도씩
h = ((hour % 12) * 30) + (minutes * 0.5)
m = minutes * 6
return abs(m - h) if 360 - abs(m - h) > abs(m - h) else 360 - abs(m - h)
12 시를 기준으로 시침과 분침의 각도를 구해서 서로 빼주기
더 작은 각도를 return 해야하므로 360 도에서 뺀 값과 비교해서 작은 값 return