상세 컨텐츠

본문 제목

파이썬으로 풀어보는 [2020 KAKAO BLIND RECRUITMENT] 블록 이동하기

Python/문제풀이

by 코딩하는 낙타 2020. 3. 31. 01:50

본문

https://programmers.co.kr/learn/courses/30/lessons/60063

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

내 풀이:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# board = [[0, 0, 0, 1, 1],[0, 0, 0, 1, 0],[0, 1, 0, 1, 1],[1, 1, 0, 0, 1],[0, 0, 0, 0, 0]]
 
def issafe(coordinate):
    global N
    if 0 <= coordinate <= N-1:
        return True
    else:
        return False
 
 
def solution(board):
    global N
    dy = [-1010]
    dx = [010-1]
 
    answer = 0
    N = len(board[0])
    visited = [[[False, False] for _ in range(N)] for _ in range(N)]
    visited[0][0][1= True
    q = [(00011)]
    while q:
        answer += 1
        for _ in range(len(q)):
            y1, x1, y2, x2, dir = q.pop(0)
 
            for d in range(4):
                ny1 = y1 + dy[d]
                nx1 = x1 + dx[d]
                ny2 = y2 + dy[d]
                nx2 = x2 + dx[d]
 
                if issafe(ny1) and issafe(nx1) and issafe(ny2) and issafe(nx2) and board[ny1][nx1] == 0 and board[ny2][nx2] == 0:
                    if not visited[ny1][nx1][dir]:
                        visited[ny1][nx1][dir] = True
                        q.append((ny1, nx1, ny2, nx2, dir))
                        if ny2 == N-1 and nx2 == N-1:
                            return answer
 
                    if abs(dir - d)%2 == 1:
                        new_dir = (dir + 1) % 2
                        nny1 = y1
                        nnx1 = x1
                        nny2 = ny1
                        nnx2 = nx1
                        if nny1+nnx1 > nny2+nnx2:
                            nny1, nnx1, nny2, nnx2 = nny2, nnx2, nny1, nnx1
                        if not visited[nny1][nnx1][new_dir]:
                            visited[nny1][nnx1][new_dir] = True
                            q.append((nny1, nnx1, nny2, nnx2, new_dir))
 
                        nny1 = ny2
                        nnx1 = nx2
                        nny2 = y2
                        nnx2 = x2
                        if nny1+nnx1 > nny2+nnx2:
                            nny1, nnx1, nny2, nnx2 = nny2, nnx2, nny1, nnx1
                        if not visited[nny1][nnx1][new_dir]:
                            visited[nny1][nnx1][new_dir] = True
                            q.append((nny1, nnx1, nny2, nnx2, new_dir))
 
# print(solution(board))
 

관련글 더보기

댓글 영역