#--- priorityQueu ---#
# PriorityQueue는 (우선순위, 데이터) 형태로 넣으면, 우선순위가 작은 값(높은 데이터)부터 자동 정렬되어 꺼내집니다.
# pQueue.get()은 큐에서 우선순위가 가장 낮은 튜플을 꺼냅니다.(예: (89, '울릉(도동항)', 23) 이런 형태)
from queue import PriorityQueue
pq = PriorityQueue()
# (우선순위, 데이터) 형태로 넣음
pq.put((3, 'apple'))
pq.put((1, 'banana'))
pq.put((2, 'cherry'))
# 우선순위가 낮은 숫자가 먼저 꺼내짐
while not pq.empty():
print(pq.get())
# 출력 (1, 'banana'), (2, 'cherry'), (3, 'apple')
import queue #정보를 저장하기 위해 큐를 사용
#지역의 위치를 저장
def Pos():
city = {
'포항고': [100,257], '포항 여객선터미널': [150,232], '포항 영일만항': [205,300],
'울릉(사동항)':[310, 165], '울릉(도동항)':[319,215], '울릉(저동항)':[332,252], '독도':[400,223]
}
return city
#지역 간의 연결 관계와 이동 시간(분)을 저장
def mTime():
graph = {
'포항고':[['포항 여객선터미널',16],['포항 영일만항',65]],
'포항 여객선터미널':[['포항고',16],['울릉(도동항)',170]],
'포항 영일만항':[['포항고',65],['울릉(사동항)',390]],
'울릉(도동항)':[['포항 여객선터미널',170],['울릉(사동항)',23],['울릉(저동항)',17],['독도',95]],
'울릉(사동항)':[['포항 영일만항',390],['울릉(도동항)',23],['독도',105]],
'울릉(저동항)':[['울릉(도동항)',17],['독도',90]],
'독도':[['울릉(사동항)',105],['울릉(도동항)',95],['울릉(저동항)',90]]
}
return graph
# 독도까지의 직선거리(km)
def H():
hn = {
'포항고':258.9,'포항 여객선터미널':258.6, '포항 영일만항':251.6,
'울릉(사동항)':90.9, '울릉(도동항)':89, '울릉(저동항)':89.4, '독도':0
}
return hn
#최상우선탐색
def bestFS(hn, graph, start ='포항고', goal='독도'):
pQueue = queue.PriorityQueue()
pQueue.put((hn[start],start,0))
path = []
best_time = 0
while not pQueue.empty():
priority, current, c_time = pQueue.get()
path.append(current)
best_time += c_time
if current == goal:
break
pQueue = queue.PriorityQueue()
for next, t in graph[current]:
if next not in path:
pQueue.put((hn[next],next,t))
return path, best_time