
def solution(maze):
    # 상우하좌
    dr = (-1, 0, 1, 0)
    dc = (0, 1, 0, -1)

    N = len(maze)
    r, c, d = 0, 0, 1
    cnt = 0
    while True:
        if (r, c) == (N-1, N-1):    # 목적지 도착
            break
        # 처음 왼쪽 안되면 시계방향 확인
        d = (d + 3) % 4
        for _ in range(4):
            nr = r + dr[d]
            nc = c + dc[d]
            if not (0 <= nr < N and 0 <= nc < N) or maze[nr][nc] == 1:
                d = (d+1) % 4
                continue
            cnt += 1
            r, c = nr, nc
            break


    return cnt

print(solution([[0, 1, 0, 1], [0, 1, 0, 0], [0, 0, 0, 0], [1, 0, 1, 0]]))
# print(solution([[0, 1, 0, 0, 0, 0], [0, 1, 0, 1, 1, 0], [0, 1, 0, 0, 1, 0], [0, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0]]))
# print(solution([[0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0]]))
# print(solution([[0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 1, 1], [0, 0, 0, 0, 0, 0], [1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0], [1, 1, 0, 1, 1, 0]]))
dac16d8a11f95f8ec91a22d2fd9baca5d611a8a1
<mask token>

def solution(maze):
    dr = -1, 0, 1, 0
    dc = 0, 1, 0, -1
    N = len(maze)
    r, c, d = 0, 0, 1
    cnt = 0
    while True:
        if (r, c) == (N - 1, N - 1):
            break
        d = (d + 3) % 4
        for _ in range(4):
            nr = r + dr[d]
            nc = c + dc[d]
            if not (0 <= nr < N and 0 <= nc < N) or maze[nr][nc] == 1:
                d = (d + 1) % 4
                continue
            cnt += 1
            r, c = nr, nc
            break
    return cnt


<mask token>

def solution(maze):
    dr = -1, 0, 1, 0
    dc = 0, 1, 0, -1
    N = len(maze)
    r, c, d = 0, 0, 1
    cnt = 0
    while True:
        if (r, c) == (N - 1, N - 1):
            break
        d = (d + 3) % 4
        for _ in range(4):
            nr = r + dr[d]
            nc = c + dc[d]
            if not (0 <= nr < N and 0 <= nc < N) or maze[nr][nc] == 1:
                d = (d + 1) % 4
                continue
            cnt += 1
            r, c = nr, nc
            break
    return cnt


print(solution([[0, 1, 0, 1], [0, 1, 0, 0], [0, 0, 0, 0], [1, 0, 1, 0]]))


def solution(maze):
    # 상우하좌
    dr = (-1, 0, 1, 0)
    dc = (0, 1, 0, -1)

    N = len(maze)
    r, c, d = 0, 0, 1
    cnt = 0
    while True:
        if (r, c) == (N-1, N-1):    # 목적지 도착
            break
        # 처음 왼쪽 안되면 시계방향 확인
        d = (d + 3) % 4
        for _ in range(4):
            nr = r + dr[d]
            nc = c + dc[d]
            if not (0 <= nr < N and 0 <= nc < N) or maze[nr][nc] == 1:
                d = (d+1) % 4
                continue
            cnt += 1
            r, c = nr, nc
            break


    return cnt

print(solution([[0, 1, 0, 1], [0, 1, 0, 0], [0, 0, 0, 0], [1, 0, 1, 0]]))
# print(solution([[0, 1, 0, 0, 0, 0], [0, 1, 0, 1, 1, 0], [0, 1, 0, 0, 1, 0], [0, 1, 1, 1, 1, 0], [0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0]]))
# print(solution([[0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0]]))
# print(solution([[0, 0, 0, 0, 0, 0], [1, 1, 1, 0, 1, 1], [0, 0, 0, 0, 0, 0], [1, 0, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0], [1, 1, 0, 1, 1, 0]]))