Skip to content
Snippets Groups Projects
Commit 7264e199 authored by Cseh Viktor's avatar Cseh Viktor
Browse files

Vegre keszvan 12 pontra ez a fos

parent d45db2c0
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -15,3 +15,8 @@ route_between_points ...@@ -15,3 +15,8 @@ route_between_points
1 0 1 0
1 2 1 2
0 2 0 2
input3 rossz 2db, majd jo kimenete:
380.42 84.85 118.79 67.88 106.07
380.42 465.28 543.06 272.94 364.87
316.85 368.61 442.56 237.84 325.31
\ No newline at end of file
######################################### #########################################
# Keszitette Cseh Viktor - GG2DP5 # # Keszitette Cseh Viktor - GG2DP5 #
# 2021.10.08 # # 2021.10.11 #
######################################### #########################################
#import sys
import math import math
##### Global variables ##### ##### Global variables #####
...@@ -15,73 +14,63 @@ route_between_points = [] ...@@ -15,73 +14,63 @@ route_between_points = []
route = [] route = []
calculated_lengths = [] calculated_lengths = []
distance = [] verticles = []
notseenpoints = []
##### Global functions ##### ##### Classes #####
def dijkstra(a, b):
if(a == b): # Ekkor elertuk a keresett pontot
calculated_lengths.append(distance[b])
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 ## Ebben fogjuk majd tarolni a graf pontjaihoz tartozo adatokat
if(route_between_points[i] == a): class vertice:
length = calc_route_length(route_between_points[i], route_between_points[i+1]) def __init__(self, name, x, y, neighbours):
self.name = name
self.x = x
self.y = y
self.neighbours = neighbours
self.distance = math.inf
if(length + distance[a] < distance[route_between_points[i+1]]): # ha kisebb az eddig tudott utvonalnal a most talalt, akkor cserelje ki arra ##### Global functions #####
distance[route_between_points[i+1]] = length + distance[a]
elif(route_between_points[i+1] == a): ## Neve megteveszto, mert ez nem az egesz dijkstra algoritmus, csak a szomszedokat kiszamolo resze.
length = calc_route_length(route_between_points[i], route_between_points[i+1]) ## Amikor az egesz algoritmus itt volt, akkor a csodalatos hf.mit.bme.hu ellenorzo rendszer panaszkodott,
## hogy tul nagy a rekurzio melysege, igy fail-elt a leadas.....
def dijkstra(a, b, notseenpoints):
if(a.name == b):
calculated_lengths.append(a.distance)
return
if(length + distance[a] < distance[route_between_points[i]]): for i in a.neighbours:
distance[route_between_points[i]] = length + distance[a] lenght = calc_route_length(a, verticles[i])
if(lenght + a.distance < verticles[i].distance):
verticles[i].distance = a.distance + lenght
## Ez lenne a dijkstra masodik resze, ami az elozo vegere ment volna kb
def route_search(): def route_search():
notseenpoints = []
# minden kiszamolando ut, minden kovetkezo legkisebb eleresi utu pontjara meghivja a szomszedok tavolsaganak a kiszamolasat
for x in range(0,len(route_to_calc),2): for x in range(0,len(route_to_calc),2):
for i in range(map_config[1]): for i in range(map_config[1]):
if(i == route_to_calc[x]): if(i == route_to_calc[x]):
distance.append(0) verticles[i].distance = 0
else: else:
distance.append(math.inf) verticles[i].distance = math.inf
notseenpoints.append(i) notseenpoints.append(verticles[i])
while(len(notseenpoints) != 0): while(len(notseenpoints) != 0):
minimum_distance = math.inf notseenpoints = sorted(notseenpoints, key=lambda verticle: verticle.distance)
corresponding_point = None
cntr = 0
for i in range(len(notseenpoints)):
if(distance[notseenpoints[i]] != None): #Meghivni ra a dijkstra algoritmust rekurzivan, ha mar szamoltuk, de ne jartunk meg benne
if(distance[notseenpoints[i]] < minimum_distance): # Es legkisebb a tavolsaga eddig
cntr += 1
minimum_distance = distance[notseenpoints[i]]
corresponding_point = notseenpoints[i]
if(cntr > 0):
notseenpoints.remove(corresponding_point)
dijkstra(corresponding_point, route_to_calc[x+1])
distance.clear()
notseenpoints.clear()
def calc_route_length(a, b):
# X koordinata lekerese es szamolasa a pitagorasz tetelhez
x1 = int(point_coordinates[a*2])
x2 = int(point_coordinates[b*2]) dijkstra(notseenpoints[0], route_to_calc[x+1], notseenpoints)
notseenpoints.clear()
x = abs(x1 - x2)
# Y koordinata lekerese es szamolasa a pitagorasz tetelhez def calc_route_length(a, b):
# X tavolsag szamolasa pitagorasz tetelhez
y1 = int(point_coordinates[a*2+1]) x = abs(int(a.x) - int(b.x))
y2 = int(point_coordinates[b*2+1]) # Y tavolsag szamolasa pitagorasz tetelhez
y = abs(y1 - y2) y = abs(a.y - b.y)
# Pitagorasz tetel # Pitagorasz tetel
...@@ -111,6 +100,8 @@ def read_from_input(): ...@@ -111,6 +100,8 @@ def read_from_input():
point_coordinates.append(int(arr[0], 10)) point_coordinates.append(int(arr[0], 10))
point_coordinates.append(int(arr[1], 10)) point_coordinates.append(int(arr[1], 10))
temp = vertice(i, int(arr[0], 10), int(arr[1], 10), neighbours = [])
verticles.append(temp)
x = input() x = input()
...@@ -120,6 +111,8 @@ def read_from_input(): ...@@ -120,6 +111,8 @@ def read_from_input():
route_between_points.append(int(arr[0], 10)) route_between_points.append(int(arr[0], 10))
route_between_points.append(int(arr[1], 10)) route_between_points.append(int(arr[1], 10))
verticles[int(arr[0], 10)].neighbours.append(int(arr[1], 10))
verticles[int(arr[1], 10)].neighbours.append(int(arr[0], 10))
def output_lengths(): def output_lengths():
output = "" output = ""
...@@ -135,12 +128,7 @@ def main(): ...@@ -135,12 +128,7 @@ def main():
read_from_input() read_from_input()
route_search() route_search()
output_lengths() output_lengths()
#print(map_config, file=sys.stderr)
#print(route_to_calc, file=sys.stderr)
#print(point_coordinates, file=sys.stderr)
#print(route_between_points, file=sys.stderr)
#print("almafa")
#print("***" + map_config[0] + "***", file=sys.stderr)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment