diff --git a/mi_GG2DP5_hf1.py b/mi_GG2DP5_hf1.py index eab263a4ebce55fa6a37df0eab11dde27a7115f6..d43e5f5a7db761b7da5fec546c3a01f3886e1f66 100644 --- a/mi_GG2DP5_hf1.py +++ b/mi_GG2DP5_hf1.py @@ -15,92 +15,106 @@ route_between_points = [] route = [] calculated_lengths = [] +distance = [] #Ebbe mennek a pontok eleresi hosszai +seen = [] + ##### Global functions ##### # Start search route from point, and gives back a route. Works recursive # Start from point a, and not stopping until point b reached -def iter_from_point(a, b, seen): - if(a == b): +def dijkstra(a, b): + seen[a] = 1 # jeloljuk, hogy mar jartunk itt, ne kezdjuk el + + if(a == b): # Ekkor elertuk a keresett pontot + calculated_lengths.append(distance[b]) return - for i in range(0, len(route_between_points), 2): - if(route_between_points[i] == a and route_between_points[i+1] == b): - route.append(int(i/2)) - return - if(route_between_points[i+1] == a and route_between_points[i] == b): - route.append(int(i/2)) - return + for i in range(0, len(route_between_points), 2): # iteralni a route_between_points-on, hogy az == e a meghivottal es kiszamolni a szomszedokhoz tartozo utat, ami esetleg legrovidebb lehet + if(route_between_points[i] == a ): + length = calc_route_length(a, route_between_points[i+1]) - for i in range(0, len(route_between_points), 2): - if(route_between_points[i] == route_between_points[i+1]): - continue + if(distance[route_between_points[i+1]] == math.inf): + print("*** debug: " + str(distance[a]), file=sys.stderr) + distance[route_between_points[i+1]] == length + distance[a] + continue - if(route_between_points[i] == a and seen[int(i/2)] == 0): - route.append(int(i/2)) - seen[int(i/2)] = 1 - iter_from_point(route_between_points[i+1], b, seen) + if(length < distance[a] + distance[route_between_points[i+1]]): # ha kisebb az eddig tudott utvonalnal a most talalt, akkor cserelje ki arra + distance[route_between_points[i+1]] = length + distance[a] - if(route_between_points[i+1] == a and seen[int(i/2)]==0): - route.append(int(i/2)) - seen[int(i/2)] = 1 - iter_from_point(route_between_points[i], b, seen) + if(route_between_points[i+1] == a ): + length = calc_route_length(a, route_between_points[i]) + if(distance[route_between_points[i]] == math.inf): + distance[route_between_points[i]] == length + distance[a] + continue + if( length + distance[a] < distance[route_between_points[i]]): + distance[route_between_points[i]] == length + distance[a] -def route_search(): - seen = [] - asd = len(route_between_points) + minimum_distance = math.inf + corresponding_point = None + cntr = 0 + for i in range(len(seen)): + if(seen[i] == 0 and distance[i] != math.inf): #Meghivni ra a dijkstra algoritmust rekurzivan, ha mar szamoltuk, de ne jartunk meg benne + cntr += 1 + print("*** debug: talalt pontot amiben meg nem volt", file=sys.stderr) + if(distance[i] < minimum_distance): # Es legkisebb a tavolsaga eddig + minimum_distance = distance[i] + corresponding_point = i + + if(cntr > 0): + dijkstra(corresponding_point, b) + + + + +def route_search(): + asd = len(point_coordinates) asd = asd/2 asd = int(asd) - for i in range(asd): - seen.append(0) - for x in range(0,len(route_to_calc),2): - iter_from_point(route_to_calc[x], route_to_calc[x+1], seen) # TODO melyik ponttol iteraljunk? - calc_route_length() - route.clear() + for i in range(asd): + if(i == route_to_calc[x]): + distance.append(0) + else: + distance.append(math.inf) + + for i in range(asd): + seen.append(0) + + dijkstra(route_to_calc[x], route_to_calc[x+1]) + + distance.clear() + seen.clear() #print("**** debug ***", file=sys.stderr) #print( route , file=sys.stderr) #print("**** \debug ***", file=sys.stderr) -def calc_route_length(): - route_length = 0 - - for i in range(len(route)): - # X koordinata lekerese es szamolasa a pitagorasz tetelhez - tempx1 = int(route[i]*2) - tempx2 = int(route_between_points[tempx1]) - x1 = int(point_coordinates[tempx2*2]) - - tempx1 = int(route[i]*2+1) - tempx2 = int(route_between_points[tempx1]) - x2 = int(point_coordinates[tempx2*2]) +def calc_route_length(a, b): + + # X koordinata lekerese es szamolasa a pitagorasz tetelhez + x1 = int(point_coordinates[a*2]) - x = abs(x1 - x2) + x2 = int(point_coordinates[b*2]) - # Y koordinata lekerese es szamolasa a pitagorasz tetelhez + x = abs(x1 - x2) - tempy1 = int(route[i]*2) - tempy2 = int(route_between_points[tempy1]) - y1 = int(point_coordinates[tempy2*2+1]) + # Y koordinata lekerese es szamolasa a pitagorasz tetelhez - tempy1 = int(route[i]*2+1) - tempy2 = int(route_between_points[tempy1]) - y2 = int(point_coordinates[tempy2*2+1]) - y = abs(y1 - y2) + y1 = int(point_coordinates[a*2+1]) + y2 = int(point_coordinates[b*2+1]) - # Pitagorasz tetel + y = abs(y1 - y2) - route_length += math.sqrt(x*x + y*y) - calculated_lengths.append(route_length) - route_length = 0 + # Pitagorasz tetel + return math.sqrt(x*x + y*y) def read_from_input():