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 = [-1, 0, 1, 0]
dx = [0, 1, 0, -1]
answer = 0
N = len(board[0])
visited = [[[False, False] for _ in range(N)] for _ in range(N)]
visited[0][0][1] = True
q = [(0, 0, 0, 1, 1)]
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))
|
파이썬으로 풀어보는 백준 1991번: 트리 순회 (0) | 2020.04.27 |
---|---|
파이썬으로 풀어보는 백준 1949번: 우수 마을 (0) | 2020.04.12 |
파이썬으로 풀어보는 SWEA 5688. 세제곱근을 찾아라 (0) | 2020.03.20 |
파이썬으로 풀어보는 백준 2573번: 빙산 (0) | 2020.02.27 |
파이썬으로 풀어보는 백준 13302번: 리조트 (0) | 2020.02.27 |
댓글 영역