Search

67259. ๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค

ํ”Œ๋žซํผ
BOJ
๋ถ„๋ฅ˜
์•Œ๊ณ ๋ฆฌ์ฆ˜
์ค‘์š”๋„
ํ‹ฐ์–ด
๋งํฌ
๋‹ค์‹œ ํ’€์–ด๋„ ํ‹€๋ฆด ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ
Created At
2023/04/15 14:18
Updated At
2023/04/15 14:42
Writer
์ƒํƒœ

Problem

Code

ย #1

Idea

bfs ๋กœ ํƒ์ƒ‰์„ ํ•˜๋Š”๋ฐ ํ์— (x์ขŒํ‘œ, y์ขŒํ‘œ, ๋ˆ„์ ๋น„์šฉ, ๋ฐฉํ–ฅ) ์„ ์ €์žฅํ•˜๋ฉฐ ํƒ์ƒ‰์„ ํ•œ๋‹ค.
1.
์ง์„ /์ฝ”๋„ˆ ํŒ๋‹จ
ํ์— pop ํ•œ ๋ฐฉํ–ฅ ๊ฐ’๊ณผ ๊ฐ™์œผ๋ฉด ์ง์„ ์ด๊ณ , ๋‹ค๋ฅด๋ฉด ์ฝ”๋„ˆ๋‹ค.
2.
์ตœ์†Œ๋น„์šฉ
ํ์— ๋ˆ„์ ๋น„์šฉ์„ ๋‹ด์Œ์œผ๋กœ์จ ๋ฐฉ๋ฌธํ•  ๋…ธ๋“œ์˜ ์ตœ์†Œ๋น„์šฉ์„ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.

ย Code

from collections import deque def solution(board): dx = [-1,0,1,0] dy = [0,1,0,-1] n = len(board) MAX = int(1e9) def bfs(start): visited = [[MAX]*n for _ in range(n)] visited[0][0] = 0 q = deque([start]) # x, y, cost, direction while q: x, y, c, d = q.popleft() for i in range(4): nx = x + dx[i] ny = y + dy[i] if 0 <= nx < n and 0 <= ny < n and board[nx][ny] == 0: # dp if i == d: # ๋ฐฉํ–ฅ ๊ทธ๋Œ€๋กœ์ด๋ฉด ์ง์„  ๊ธˆ์•ก ๊ณ„์‚ฐ nc = c + 100 else: # ๋ฐฉํ–ฅ ๋‹ฌ๋ผ์ง€๋ฉด ์ฝ”๋„ˆ ๊ธˆ์•ก ๊ณ„์‚ฐ nc = c + 600 # new cost ๊ฐ€ ์ž‘์„ ๋•Œ๋งŒ ๊ฐฑ์‹ ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ, ํ append if nc < visited[nx][ny]: visited[nx][ny] = nc q.append([nx, ny, nc, i]) return visited[n-1][n-1] return min([bfs((0, 0, 0, 1)), bfs((0, 0, 0, 2))])
Python
๋ณต์‚ฌ

Solution

Commentary

#2

Idea

ย Code

Python
๋ณต์‚ฌ

Solution

Commentary