def snail_in2out_cc(board):
n = len(board) # 보드 크기 가정
dx = (0, 1, 0, -1) # 상, 우, 하, 좌 방향 벡터
dy = (-1, 0, 1, 0)
x, y = n//2, n//2 # 중앙에서 시작
step = 1
cnt = 0
d = 0
while check((x, y)):
for i in range(step): # 'in' 키워드 추가
x += dx[d]
y += dy[d]
if not check((x, y)):
return
do_something()
d = (d + 1) % 4 # 매번 방향 전환
cnt += 1
if cnt % 2 == 0: # 두 방향 이동 후 (짝수번째 방향 전환)
step += 1 # 스텝 증가
def snail_out2in_c(board):
n = len(board)
dx = (0, 1, 0, -1) # 우, 하, 좌, 상 (시계방향)
dy = (1, 0, -1, 0)
x, y = 0, -1 # 시작 위치 조정
step = n -1 # 초기 스텝 (실제 이동은 n-1)
cnt = 0
d = 0
while True:
for _ in range(step):
x += dx[d]
y += dy[d]
do_something()
# 방향 전환 및 스텝 조정
cnt += 1
d = (d + 1) % 4
if cnt % 2 == 0: # 2번 방향 전환마다
step -= 1
if step == 0: # 종료 조건
return
# 연쇄는 단일인 경우 그 위치의 갯수를 표현하여서 이동후 거기 여러개인지 while [][]==2 로 한다
# 여러개가 영향 받을 수 있는 경우에는 bfs로 가능한 놈들 다 찾아야함
def gravity(board):
n = len(board)
for j in range(n): # 각 열에 대해
for i in range(n-2, -1, -1): # 아래에서 두 번째 행부터 위로
idx = i
while idx < n-1 and board[idx][j] > 0 and board[idx+1][j] == 0:
board[idx][j], board[idx+1][j] = board[idx+1][j], board[idx][j]
idx += 1
# 일부분만 떼 와서 쓴다
def rotateR(board):
nBoard = [row[:] for row in board]
for i in range(len(board)):
for j in range(len(board)):
nBoard[i][j] = nBoard[n-1-j][i]
return nBoard
def rotateL(board):
nBoard = [row[:] for row in board]
for i in range(len(board)):
for j in range(len(board)):
nBoard[i][j] = nBoard[j][n-1-i]
return nBoard
# 주사위는 외우지 말고 그냥 한번씩 이동시키면서 인덱스의 변화를 관찰하고 이를 한번만 구현하면 된다
def diceRotate(dice, d):
nDice = [row[:] for row in dice]
if d == 2:
for i in range(4):
if i == 3:
nDice[i][1] = dice[0][1]
else:
nDice[i][1] = dice[i+1][1]
elif d == 0:
for i in range(4):
if i == 0:
nDice[i][1] = dice[3][1]
else:
nDice[i][1] = dice[i-1][1]
elif d == 1:
nDice[1][0] = dice[1][1]
nDice[3][1] = dice[1][0]
nDice[1][2] = dice[3][1]
nDice[1][1] = dice[1][2]
elif d == 3:
nDice[1][0] = dice[3][1]
nDice[3][1] = dice[1][2]
nDice[1][2] = dice[1][1]
nDice[1][1] = dice[1][0]
return nDice
# DFS 활용한 순열 조
a = [1, 2, 3, 4]
def combinations(comb, depth):
if len(comb) == 2:
print(comb)
answer.append(comb[:])
return
if depth == len(a):
return
comb.append(a[depth])
combinations(comb, depth+1)
comb.pop()
combinations(comb, depth+1)
visited = [0 for _ in range(len(a))]
def permutation(comb):
if len(comb) == 3:
print(comb)
answer.append(comb)
return
for idx, val in enumerate(a):
if visited[idx]:
continue
comb.append(val)
visited[idx] = 1
permutation(comb)
visited[idx] = 0
comb.pop()
combinations([], 0)
permutation([])
def permutation_with_repetition(comb):
if len(comb) == 3: # 3개의 원소 선택
print(comb)
answer.append(comb[:]) # 복사본 저장
return
for val in a: # 모든 원소를 중복 허용하여 선택
comb.append(val)
permutation_with_repetition(comb)
comb.pop()
permutation_with_repetition([])
def combination_with_repetition(comb, start):
if len(comb) == 2: # 2개의 원소 선택
print(comb)
answer.append(comb[:]) # 복사본 저장
return
for i in range(start, len(a)): # start부터 시작해 중복 허용
comb.append(a[i])
combination_with_repetition(comb, i) # i를 그대로 전달
comb.pop()
combination_with_repetition([], 0)