-
[Programmers] ๋ธ๋ก ์ด๋ํ๊ธฐ with PythonPS 2021. 12. 12. 02:28728x90๋ฐ์ํ
๐ Programmers - [๋ธ๋ก ์ด๋ํ๊ธฐ]
๐ก ์กฐ๊ฑด
board
์ ํ ๋ณ์ ๊ธธ์ด๋5 ์ด์ 100 ์ดํ
.board
์ ์์๋0(์ด๋๊ฐ๋ฅ ๋ธ๋ก)
๋๋1(์ด๋๋ถ๊ฐ ๋ฒฝ)
.๋ก๋ด์ด ์ฒ์์ ๋์ฌ ์๋ ์นธ
(1, 1), (1, 2)
๋ ํญ์ 0์ผ๋ก ์ฃผ์ด์ง๋ค.๋ก๋ด์ ํ์ ํ ์ ์๋ค.
BFS, ์๋ฎฌ๋ ์ด์ ์ ๋ฌธ์
(N, N)
์ขํ๊น์ง ๋๋ฌํ๋ ์ต์์๊ฐ์ ๊ตฌํ๋ ๋ฌธ์
๐ฅ ์์ค ์ฝ๋
from collections import deque def get_next_pos(pos, board): next_pos = [] pos = list(pos) pos1_x, pos1_y, pos2_x, pos2_y = pos[0][0], pos[0][1], pos[1][0], pos[1][1] dx = [-1, 1, 0, 0] dy = [0, 0, -1, 1] for i in range(4): pos1_next_x, pos1_next_y, pos2_next_x, pos2_next_y = pos1_x + dx[i], pos1_y + dy[i], pos2_x + dx[i], pos2_y + dy[i] if board[pos1_next_x][pos1_next_y] == 0 and board[pos2_next_x][pos2_next_y] == 0: next_pos.append({(pos1_next_x, pos1_next_y), (pos2_next_x, pos2_next_y)}) if pos1_x == pos2_x: for i in [-1, 1]: if board[pos1_x + i][pos1_y] == 0 and board[pos2_x + i][pos2_y] == 0: next_pos.append({(pos1_x, pos1_y), (pos1_x + i, pos1_y)}) next_pos.append({(pos2_x, pos2_y), (pos2_x + i, pos2_y)}) elif pos1_y == pos2_y: for i in [-1, 1]: if board[pos1_x][pos1_y + i] == 0 and board[pos2_x][pos2_y + i] == 0: next_pos.append({(pos1_x, pos1_y), (pos1_x, pos1_y + i)}) next_pos.append({(pos2_x, pos2_y), (pos2_x, pos2_y + i)}) return next_pos def solution(board): n = len(board) new_board = [[1] * (n + 2) for _ in range(n + 2)] for i in range(n): for j in range(n): new_board[i + 1][j + 1] = board[i][j] q = deque() visited = [] pos = {(1, 1), (1, 2)} q.append((pos, 0)) visited.append(pos) while q: pos, cost = q.popleft() if (n, n) in pos: return cost for next_pos in get_next_pos(pos, new_board): if next_pos not in visited: q.append((next_pos, cost + 1)) visited.append(next_pos) return 0
๐ ์์ ๋ฐ ์คํ๊ฒฐ๊ณผ
์์
print(solution([[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]]))
์คํ๊ฒฐ๊ณผ
7
โจ๏ธ ๋ฌธ์ ํ์ด
๊ฐ์ฅ ๋จผ์ ,
(n * n)
ํฌ๊ธฐ์ ๋งต์(n + 2) * (n + 2)
๋งต์ผ๋ก ์๋ก ๋ง๋ค์ด
๋งต ์ฃผ๋ณ์ 1๋ก ๋๋ฌ์ฃผ์ด ์ด๋ํ์ง ๋ชปํ๋ ๊ณณ์ ํ์คํ ๋ง๋ค์ด์ค๋ค.์์ ์์น๋
{(1, 1), (1, 2)}
๋กpos
์ ์ ์ฅํ ๋ค ํ์ ๋ฃ๋๋ค.
๋ฌผ๋ก ๋ฐฉ๋ฌธํ ๊ธฐ๋ก์ ๋จ๊ธธvisited
๋ฆฌ์คํธ์๋ ์ ์ฅํ๋ค.BFS์ ๋ฐฉ์์ผ๋ก
Queue
๊ฐ ๋น ๋๊น์ง ์ํ๋ฅผ ํ๋ค.
๋จ, ํ์ฌ ์์น์์ ์ด๋์ด ๊ฐ๋ฅํ๊ฑฐ๋ ํ์ ์ด ๊ฐ๋ฅํ ์์น๋ฅผ ํ์ ์ ์ฅํ๋ค.3๋ฒ์์ ๋งํ ์ด๋ ๋ฐ ํ์ ์ด ๊ฐ๋ฅํ ์ขํ๋ ํจ์
get_next_pos()
์์ ๋ฐํํ๋ ์ขํ๊ฐ๋ค์ ๊ธฐ์ค์ผ๋ก ํ๋ค.get_next_pos
์์๋ ํ์ฌ ์์น์์ ์ด๋ ๋ฐ ๊ฐ๋ฅํ ์ขํ๋ฅผ ๋ฐํํ๋๋ฐ, ์๋์ ์กฐ๊ฑด์ ์ ํ์ธํด์ผํ๋ค.- ์ํ์ข์ฐ ๋ค ๋ฐฉํฅ์ผ๋ก ์ด๋์ด ๊ฐ๋ฅํ์ง
- ํ์ฌ ๋ก๋ด์ด ๊ฐ๋ก๋ก ๋์ฌ์ ธ ์๋ ๊ฒฝ์ฐ ํ์ ์ด ๊ฐ๋ฅํ์ง
- ํ์ฌ ๋ก๋ด์ด ์ธ๋ก๋ก ๋์ฌ์ ธ ์๋ ๊ฒฝ์ฐ ํ์ ์ด ๊ฐ๋ฅํ์ง
get_next_pos
์์ ๋ฐํ๋ฐ์ ์ขํ๋ค ์ค, ์ด๋ฏธ ๋ฐฉ๋ฌธํ ์ฅ์๊ฐ ์๋๋ผ๋ฉด ๋ฐฉ๋ฌธ์ฒ๋ฆฌ๋ฅผ ํ๊ณcost(๊ฑธ๋ฆฐ ์๊ฐ)
์ ํ์ ํจ๊ป ๋ฃ์ด์ค๋ค.
๐พ ๋๋์
- ๋ก๋ด์ด 90๋ ํ์ ํ๋ ์ขํ์ ๋ํด์ ์ฒ๋ฆฌํ๊ณ ๋ฐํํ๋ ๊ฒ์ด ์ด๋ ค์ ๋ค.
- ์ขํ ๊ณ์ฐ์ด ํท๊ฐ๋ ค ํ์ด ๋ค์๋ค.
- ์ขํ ๋ฐ ์ธ๋ฑ์ฑ์ ๋ํด ์ฐ์ต์ด ๋ง์ด ํ์ํ ๊ฒ ๊ฐ๋ค.
๋ฐ์ํ'PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 1515 ์ ์ด์ด ์ฐ๊ธฐ with Python (0) 2021.12.12 [๋ฐฑ์ค] 1411 ๋น์ทํ ๋จ์ด with Python (0) 2021.12.12 [Programmers] ์ธ๋ฒฝ ์ ๊ฒ with Python (0) 2021.12.06 [๋ฐฑ์ค] 11497 ํต๋๋ฌด ๊ฑด๋๋ฐ๊ธฐ with Python (0) 2021.12.06 [๋ฐฑ์ค] 6118 ์จ๋ฐ๊ผญ์ง with Python (0) 2021.12.01