728x90
๋ฐ์ํ
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ์ฐ์ต
- ํ๋ก๊ทธ๋๋จธ์ค
- ์ฝ๋ฉํ
์คํธ ์ฐ์ต
- ๊น์ด/๋๋น ์ฐ์ ํ์(DFS/BFS)
- ๊ฒ์ ๋งต ์ต๋จ๊ฑฐ๋ฆฌ
- ๊น์ด/๋๋น ์ฐ์ ํ์(DFS/BFS)
- ์ฝ๋ฉํ
์คํธ ์ฐ์ต
๋ฌธ์ ๋ณด๊ธฐ
๋ด๊ฐ ํผ ๋ฐฉ์์...
- ํ์ํ ๋ค์ ์์น๋ค์ ์ฐ์ ์์ ํ์ ์ ์ฅํ๊ณ ,
- ์์น ์ ์ฅ์ ์ํ ๋ฐฐ์ด์ ๋ฌธ์ ์์ ์ ๊ณตํ๋ maps์ ๊ฐ์ ํฌ๊ธฐ๋ก ๋ง๋ค์ด์ inf ๊ฐ์ ๋ฃ์ด๋๊ณ ํ์ํ๋๋ฐ...
from queue import PriorityQueue
def solution(maps):
pq = PriorityQueue()
dist = 1
x, y = 0, 0
sx, sy = 0, 0
ex, ey = len(maps[0]), len(maps)
pq.put([dist,(sx,sy)])
INF = float("inf") #ํ์ด์ฌ 3.5 ์ด์์์ ์ฌ์ฉ ๊ฐ๋ฅ
footmap = [[INF for i in range(ex)] for j in range(ey)]
while pq.empty() == False:
t = pq.get()
dist = t[0]
x = t[1][0]
y = t[1][1]
if dist < footmap[y][x] and maps[y][x] != 0 and sx <= x and x < ex and sy <= y and y < ey:
footmap[y][x] = dist
dist += 1
if sx < x and maps[y][x-1] != 0:
pq.put([dist,(x-1,y)])
if x < ex-1 and maps[y][x+1] != 0:
pq.put([dist,(x+1,y)])
if sy < y and maps[y-1][x] != 0:
pq.put([dist,(x,y-1)])
if y < ey-1 and maps[y+1][x] != 0:
pq.put([dist,(x,y+1)])
if footmap[ey-1][ex-1] == INF:
return -1
else:
return footmap[ey-1][ex-1]
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.20ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.15ms, 10.5MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.29ms, 10.4MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.28ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.36ms, 10.4MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.27ms, 10.4MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.41ms, 10.5MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.28ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.39ms, 10.3MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.36ms, 10.4MB)
ํ
์คํธ 11 ใ ํต๊ณผ (0.18ms, 10.4MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.13ms, 10.3MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.27ms, 10.4MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.21ms, 10.6MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.17ms, 10.3MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.08ms, 10.3MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.65ms, 10.5MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.10ms, 10.5MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.06ms, 10.5MB)
ํ
์คํธ 20 ใ ํต๊ณผ (0.06ms, 10.5MB)
ํ
์คํธ 21 ใ ํต๊ณผ (0.05ms, 10.5MB)
ํจ์จ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (165.44ms, 10.6MB)
ํ
์คํธ 2 ใ ํต๊ณผ (26.76ms, 10.7MB)
ํ
์คํธ 3 ใ ํต๊ณผ (68.42ms, 10.5MB)
ํ
์คํธ 4 ใ ํต๊ณผ (45.74ms, 10.5MB)
๋ค๋ฅธ ์ฌ๋ ํ์ด ๋ณด๋๊น ๋ ๊น๋ํ ์ฝ๋๋ค์ด ๋ง์๋ค.
๋ ๋น ๋ฅด๊ณ ํจ์จ์ ์.
from collections import deque
def solution(maps):
x_move = [1, 0, -1, 0]
y_move = [0, 1, 0, -1]
x_h, y_h = (len(maps[0]), len(maps))
queue = deque([(0, 0, 1)])
while queue:
x, y, d = queue.popleft()
for i in range(4):
nx = x + x_move[i]
ny = y + y_move[i]
if nx > -1 and ny > -1 and nx < x_h and ny < y_h:
if maps[ny][nx] == 1 or maps[ny][nx] > d + 1:
maps[ny][nx] = d + 1
if nx == x_h - 1 and ny == y_h - 1:
return d + 1
queue.append((nx, ny, d + 1))
return -1
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.06ms, 10.3MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.04ms, 10.4MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.07ms, 10.2MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.10ms, 10.1MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.06ms, 10.1MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.05ms, 10.2MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.06ms, 10.1MB)
ํ
์คํธ 11 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.02ms, 10.1MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.06ms, 10.3MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.04ms, 10.1MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.07ms, 10.1MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 20 ใ ํต๊ณผ (0.01ms, 10.2MB)
ํ
์คํธ 21 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํจ์จ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (13.07ms, 10.2MB)
ํ
์คํธ 2 ใ ํต๊ณผ (3.71ms, 10.4MB)
ํ
์คํธ 3 ใ ํต๊ณผ (8.80ms, 10.3MB)
ํ
์คํธ 4 ใ ํต๊ณผ (5.43ms, 10.2MB)
ํ ์์ด ํธ๋ ๋ฐฉ์๋ ์๋๋ฐ, ์ด๊ฒ ๋ ๋น ๋ฅด๋ค.
๋ค ์ฐพ์ง ์๊ณ ์ค๊ฐ์ ๋์ด์ ๊ทธ๋ฐ๊ฑด๊ฐ?
def solution(maps):
answer = 0
n = len(maps)-1
m = len(maps[0])-1
path = [(0,0)]
food = [1]
pos = 0
try:
while(True):
i = path[pos][0]
j = path[pos][1]
if maps[i][j] == 1:
if j<m:
if maps[i][j+1] == 1:
# ์ค๋ฅธ์ชฝ์ ๊ธธ์ด ์๋ค๋ฉด
path.append((i,j+1))
food.append(food[pos]+1)
if i<n:
if maps[i+1][j] == 1:
# ์๋์ชฝ์ ๊ธธ์ด ์๋ค๋ฉด
path.append((i+1, j))
food.append(food[pos]+1)
if j != 0:
if maps[i][j-1] == 1:
# ์ผ์ชฝ์ ๊ธธ์ด ์๋ค๋ฉด
path.append((i,j-1))
food.append(food[pos]+1)
if i != 0:
if maps[i-1][j] == 1 and i != 0:
path.append((i-1,j))
food.append(food[pos]+1)
pos += 1
maps[i][j] = 0
if i == n and j == m:
answer = food[path.index((n,m))]
break
except:
answer = -1
return answer
ํ
์คํธ 1 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.02ms, 10.4MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.03ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.03ms, 10.4MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.04ms, 10.4MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.03ms, 10.4MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.06ms, 10.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (0.03ms, 10.4MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.04ms, 10.3MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.02ms, 10.4MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.05ms, 10.2MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํ
์คํธ 20 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 21 ใ ํต๊ณผ (0.01ms, 10.1MB)
ํจ์จ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (11.73ms, 11.8MB)
ํ
์คํธ 2 ใ ํต๊ณผ (2.45ms, 10.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (6.00ms, 10.9MB)
ํ
์คํธ 4 ใ ํต๊ณผ (3.73ms, 10.6MB)
A* ์๊ณ ๋ฆฌ์ฆ๋ ๋์จ๋ค
import heapq
def a_star(R,C,maps):
# init
check_list = [(1,0),(0,1),(-1,0),(0,-1)]
path = [[100000]*C for r in range(R)]
visited = [[0]*C for r in range(R)]
# a star queue guess, r, c
queue = [(R+C-2,0,0)]
path[0][0] = 0
while(queue):
guess,r,c = heapq.heappop(queue)
visited[r][c] = 1
for check in check_list:
tmp_r = r+check[0]
tmp_c = c+check[1]
# ๊ฐ์์ด
if tmp_r<0 or tmp_r>=R or tmp_c <0 or tmp_c >= C:
continue
if visited[tmp_r][tmp_c] == 1:
continue
# ๋ฒฝ์ด๋ฉด ๋์ด๊ฐ
if maps[tmp_r][tmp_c] == 0:
continue
if path[tmp_r][tmp_c] > path[r][c] + 1:
path[tmp_r][tmp_c] = path[r][c]+1
f = path[tmp_r][tmp_c] + R + C - tmp_r - tmp_c -2
if tmp_r == R-1 and tmp_c == C-1:
return f+1
heapq.heappush(queue,(f,tmp_r,tmp_c))
return -1
def solution(maps):
R = len(maps)
C = len(maps[0])
return a_star(R,C,maps)
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.03ms, 10.3MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.02ms, 10.2MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.05ms, 10.2MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.04ms, 10.2MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.04ms, 10.3MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.08ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.05ms, 10.1MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.11ms, 10.3MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.09ms, 10.3MB)
ํ
์คํธ 11 ใ ํต๊ณผ (0.04ms, 10.3MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.05ms, 10.4MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.05ms, 10.3MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.05ms, 10.1MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.06ms, 10.3MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.08ms, 10.1MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.03ms, 10.1MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 20 ใ ํต๊ณผ (0.01ms, 10.3MB)
ํ
์คํธ 21 ใ ํต๊ณผ (0.02ms, 10.3MB)
ํจ์จ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (17.79ms, 10.5MB)
ํ
์คํธ 2 ใ ํต๊ณผ (2.62ms, 10.3MB)
ํ
์คํธ 3 ใ ํต๊ณผ (5.78ms, 10.4MB)
ํ
์คํธ 4 ใ ํต๊ณผ (4.63ms, 10.5MB)
STACK์ ์ฌ์ฉํ ํ์ด๋ ์๊ณ ...
๋ด ์ฝ๋์์ ๋์ค์ ๋์ฐฉํ๋ฉด ๋๊ธฐ ํ๋๊น ์ข ๋ ๋นจ๋ผ์ง๊ธด ํ์ง๋ง... ์ฌ์ ํ ๋๋ ค. ์ ๋๋ฆด๊น? ๋ฐฐ์ด์ 2๊ฐ ์ฌ์ฉํด์ ๊ทธ๋ฐ๊ฐ?
- ์ค๊ฐ ๋๊ธฐ ์ถ๊ฐ
- ๊ฑฐ๋ฆฌ ์ ๋ ฅํ ๋ ์ขํ ์ฒดํฌ ์ํ๋๋ก
- ์ด๋ฏธ ๊ฐ์ด ์๋๋ฐ ์ ์ ๊ฐ์ด๋ฉด ์ฒดํฌ ์ํ๋๋ก
- ๋ฐฐ์ด์ 1๊ฐ๋ง ์ฌ์ฉํ๊ณ ...
์ด๋ ๊ฒ ๋ฐ๊พธ๋๊น ์๋๊ฐ 1/2 ์ ๋ ๋๋ค.
from queue import PriorityQueue
def solution(maps):
#if maps[0][0] == 0: #์ด์ ๋ณด๋ ์ด ๋ถ๋ถ ํ์์๋ค?
# return -1
pq = PriorityQueue() #๊ทธ๋ฅ ํ๋ฅผ ์จ๋ ๋๋ ๋ฏ... ใ
ก.ใ
ก;
dist = 1
sx, sy = 0, 0
ex, ey = len(maps[0]), len(maps)
INF = 10001 #float์ ์ธ ํ์๊ฐ ์์.
#float("inf") #ํ์ด์ฌ 3.5 ์ด์์์ ์ฌ์ฉ ๊ฐ๋ฅ
for y in range(ey):
for x in range(ex):
if maps[y][x] != 0:
maps[y][x] = INF
pq.put([dist,(sx,sy)])
while pq.empty() == False:
t = pq.get()
dist = t[0]
x = t[1][0]
y = t[1][1]
if y == ey-1 and x == ex-1:
return dist
if dist < maps[y][x]:
maps[y][x] = dist
dist += 1
if sx < x and dist < maps[y][x-1]:
pq.put([dist,(x-1,y)])
if x < ex-1 and dist < maps[y][x+1]:
pq.put([dist,(x+1,y)])
if sy < y and dist < maps[y-1][x]:
pq.put([dist,(x,y-1)])
if y < ey-1 and dist < maps[y+1][x]:
pq.put([dist,(x,y+1)])
return -1
์ ํ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (0.10ms, 10.4MB)
ํ
์คํธ 2 ใ ํต๊ณผ (0.06ms, 10.4MB)
ํ
์คํธ 3 ใ ํต๊ณผ (0.13ms, 10.5MB)
ํ
์คํธ 4 ใ ํต๊ณผ (0.09ms, 10.3MB)
ํ
์คํธ 5 ใ ํต๊ณผ (0.20ms, 10.4MB)
ํ
์คํธ 6 ใ ํต๊ณผ (0.27ms, 10.4MB)
ํ
์คํธ 7 ใ ํต๊ณผ (0.23ms, 10.3MB)
ํ
์คํธ 8 ใ ํต๊ณผ (0.13ms, 10.3MB)
ํ
์คํธ 9 ใ ํต๊ณผ (0.20ms, 10.3MB)
ํ
์คํธ 10 ใ ํต๊ณผ (0.21ms, 10.4MB)
ํ
์คํธ 11 ใ ํต๊ณผ (0.10ms, 10.4MB)
ํ
์คํธ 12 ใ ํต๊ณผ (0.08ms, 10.3MB)
ํ
์คํธ 13 ใ ํต๊ณผ (0.12ms, 10.4MB)
ํ
์คํธ 14 ใ ํต๊ณผ (0.11ms, 10.3MB)
ํ
์คํธ 15 ใ ํต๊ณผ (0.11ms, 10.4MB)
ํ
์คํธ 16 ใ ํต๊ณผ (0.06ms, 10.3MB)
ํ
์คํธ 17 ใ ํต๊ณผ (0.17ms, 10.3MB)
ํ
์คํธ 18 ใ ํต๊ณผ (0.08ms, 10.3MB)
ํ
์คํธ 19 ใ ํต๊ณผ (0.06ms, 10.1MB)
ํ
์คํธ 20 ใ ํต๊ณผ (0.03ms, 10.3MB)
ํ
์คํธ 21 ใ ํต๊ณผ (0.04ms, 10.3MB)
ํจ์จ์ฑ ํ
์คํธ
ํ
์คํธ 1 ใ ํต๊ณผ (84.16ms, 10.6MB)
ํ
์คํธ 2 ใ ํต๊ณผ (14.37ms, 10.5MB)
ํ
์คํธ 3 ใ ํต๊ณผ (39.36ms, 10.6MB)
ํ
์คํธ 4 ใ ํต๊ณผ (22.83ms, 10.4MB)
728x90
๋ฐ์ํ
'๊ฒ์ ํ๋ก๊ทธ๋๋ฐ > Python ํ๋ก๊ทธ๋๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ๊ณ ๋์ Kit (2) ํด์ (0) | 2023.02.09 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ๊ณ ๋์ Kit (1) (0) | 2023.02.08 |
๊ฒ์์ผ๋ก ๋ฐฐ์ฐ๋ ํ์ด์ฌ (3) (0) | 2023.02.08 |
๊ฒ์์ผ๋ก ๋ฐฐ์ฐ๋ ํ์ด์ฌ (2) (0) | 2023.02.08 |
๊ฒ์์ผ๋ก ๋ฐฐ์ฐ๋ ํ์ด์ฌ (1) (0) | 2023.01.27 |