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
Branches
No related tags found
No related merge requests found
No preview for this file type
......@@ -15,3 +15,8 @@ route_between_points
1 0
1 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 #
# 2021.10.08 #
# 2021.10.11 #
#########################################
#import sys
import math
##### Global variables #####
......@@ -15,73 +14,63 @@ route_between_points = []
route = []
calculated_lengths = []
distance = []
notseenpoints = []
verticles = []
##### Global functions #####
def dijkstra(a, b):
if(a == b): # Ekkor elertuk a keresett pontot
calculated_lengths.append(distance[b])
return
##### Classes #####
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(route_between_points[i], route_between_points[i+1])
## Ebben fogjuk majd tarolni a graf pontjaihoz tartozo adatokat
class vertice:
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
distance[route_between_points[i+1]] = length + distance[a]
##### Global functions #####
elif(route_between_points[i+1] == a):
length = calc_route_length(route_between_points[i], route_between_points[i+1])
## Neve megteveszto, mert ez nem az egesz dijkstra algoritmus, csak a szomszedokat kiszamolo resze.
## 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]]):
distance[route_between_points[i]] = length + distance[a]
for i in a.neighbours:
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():
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 i in range(map_config[1]):
if(i == route_to_calc[x]):
distance.append(0)
verticles[i].distance = 0
else:
distance.append(math.inf)
notseenpoints.append(i)
verticles[i].distance = math.inf
notseenpoints.append(verticles[i])
while(len(notseenpoints) != 0):
minimum_distance = math.inf
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])
notseenpoints = sorted(notseenpoints, key=lambda verticle: verticle.distance)
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
......@@ -111,6 +100,8 @@ def read_from_input():
point_coordinates.append(int(arr[0], 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()
......@@ -120,6 +111,8 @@ def read_from_input():
route_between_points.append(int(arr[0], 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():
output = ""
......@@ -135,12 +128,7 @@ def main():
read_from_input()
route_search()
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__":
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment