250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 동적sql
- 다대일
- CHECK OPTION
- exclusive lock
- JPQL
- BOJ
- shared lock
- 이진탐색
- 비관적락
- 일대다
- 즉시로딩
- execute
- 낙관적락
- 백트래킹
- 유니크제약조건
- 힙
- eager
- 연관관계
- fetch
- 지연로딩
- PS
- SQL프로그래밍
- 다대다
- FetchType
- 연결리스트
- 데코레이터
- 스토어드 프로시저
- 스프링 폼
- dfs
- querydsl
Archives
- Today
- Total
흰 스타렉스에서 내가 내리지
[프로그래머스] 경주로 건설 - 필독 본문
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/67259#
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
다익스트라 문제인데,
직전시점에서, 한 지점에 도달하는 경우가 n가지라면, distance 배열을 n차원 배열로 만들어라
# https://school.programmers.co.kr/learn/courses/30/lessons/67259#
import heapq
from collections import defaultdict
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
V = 0
H = 1
def solution(board):
answer = 0
n = len(board)
total_cost = [[[1e9 for _ in range(n)] for _ in range(n)] for _ in range(2)]
q = []
# (지금까지 비용, x, y, 이전 방향)
heapq.heappush(q, (0, 0, 0, 0))
heapq.heappush(q, (0, 0, 0, 1))
total_cost[0][0][0] = 0
total_cost[1][0][0] = 0
while q:
cost, x, y, direction = heapq.heappop(q)
if total_cost[direction][x][y] < cost:
continue
if x == n - 1 and y == n - 1:
continue
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if not (0 <= nx < n and 0 <= ny < n):
continue
if board[nx][ny]:
continue
new_direction = V if y == ny else H
if direction != new_direction:
additional_cost = 600
else:
additional_cost = 100
new_cost = cost + additional_cost
if total_cost[new_direction][nx][ny] >= new_cost:
total_cost[new_direction][nx][ny] = new_cost
heapq.heappush(q, (new_cost, nx, ny, new_direction))
# print(x, y, '/', nx, ny,'/', direction, new_direction, '/', cost, new_cost)
return min(total_cost[0][n - 1][n - 1], total_cost[1][n - 1][n - 1])
'Problem Solving' 카테고리의 다른 글
[프로그래머스] Prim 인듯 아닌듯 Prim 같은 문제 (0) | 2024.04.24 |
---|---|
dfs(백트래킹)가 어려운 나를 위해 (0) | 2024.04.21 |
[프로스래머스] 코딩테스트 연습 > 해시 > 완주하지 못한 선수 (0) | 2024.04.11 |
[Time Complexity 분석] BOJ 9935 문자열 폭발 (0) | 2024.01.20 |
[노트] 최장 부분수열 (LIS) (1) | 2024.01.17 |