아이디어
- 백준의 주사위 굴리기가 도움이 많이 되었다 💡
- 주사위 굴리기 + bfs + 예외 처리
- 그냥 왜 틀렸는지 모르겠으면 처음부터 천천히 다시 짜는 것도 나쁘지 않을듯.... 생각보다 오타 찾기가 쉽지 않다................. 🤯😵🥲
틀렸던 이유
move_dice()
에서 값을 잘못 입력
- 처음부터
dx
, dy
부분을 시계방향을 생각하고 짰는데 반대로 짜서(반시계방향) move_dice()
의 방향인 d
가 제대로 안 됐다.
격자판을 벗어나게 된다면, 반사되어 방향이 반대로 바뀌게 된 뒤 한 칸 움직이게 됩니다.
이 부분에서 처음에는 그냥 d
만 바꿔주고 continue
했는데 그러면 내 생각보다 한번 덜 움직임(m - 1
번)
if nx < 0 or nx >= n or ny < 0 or ny >= n:
d = (d + 2) % 4
nx = sx + dx[d]
ny = sy + dy[d]
코드
from collections import deque
def move_dice(d):
ndice = [0] * 6
if d == 0:
ndice[0] = dice[0]
ndice[1] = dice[4]
ndice[2] = dice[2]
ndice[3] = dice[5]
ndice[4] = dice[3]
ndice[5] = dice[1]
elif d == 3:
ndice[0] = dice[1]
ndice[1] = dice[2]
ndice[2] = dice[3]
ndice[3] = dice[0]
ndice[4] = dice[4]
ndice[5] = dice[5]
elif d == 2:
ndice[0] = dice[0]
ndice[1] = dice[5]
ndice[2] = dice[2]
ndice[3] = dice[4]
ndice[4] = dice[1]
ndice[5] = dice[3]
else:
ndice[0] = dice[3]
ndice[1] = dice[0]
ndice[2] = dice[1]
ndice[3] = dice[2]
ndice[4] = dice[4]
ndice[5] = dice[5]
for i in range(6):
dice[i] = ndice[i]
def cal_point(x, y):
visited = [[0] * n for _ in range(n)]
number = board[x][y]
cnt = 1
q = deque([(x, y)])
visited[x][y] = 1
while q:
x, y = q.popleft()
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny] and board[nx][ny] == number:
q.append((nx, ny))
cnt += 1
visited[nx][ny] = 1
point = cnt * number
return point
n, m = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(n)]
dice = [5, 1, 2, 6, 4, 3]
point = 0
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
d = 0
sx, sy = 0, 0
for _ in range(m):
nx = sx + dx[d]
ny = sy + dy[d]
if nx < 0 or nx >= n or ny < 0 or ny >= n:
d = (d + 2) % 4
nx = sx + dx[d]
ny = sy + dy[d]
move_dice(d)
cur_point = cal_point(nx, ny)
point += cur_point
if board[nx][ny] < dice[3]:
d = (d + 1) % 4
elif board[nx][ny] > dice[3]:
d = (d + 3) % 4
sx, sy = nx, ny
print(point)