Created
May 12, 2016 07:23
-
-
Save ovidijusr/347563dbdc1bf1858f777ec1e71cbf83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import codecs | |
grafas = { | |
'1': {'2': float('Inf'), '6': float('Inf')}, | |
'2': {'1': float('Inf'), '3': float('Inf'), '7': 1}, | |
'3': {'2': float('Inf'), '8': float('Inf'), '4': float('Inf')}, | |
'4': {'3': float('Inf'), '5': float('Inf'), '9': float('Inf')}, | |
'5': {'4': float('Inf'), '10': float('Inf')}, | |
'6': {'7': 1, '11': 1, '1': float('Inf')}, | |
'7': {'2': 1, '6': 1, '8': 1, '12': float('Inf')}, | |
'8': {'3': float('Inf'), '13': 1, '7': 1, '9': 1}, | |
'9': {'4': float('Inf'), '8': 1, '10': 1}, | |
'10': {'5': float('Inf'), '9': 1, '15': 1}, | |
'11': {'6': 1, '12': float('Inf'), '16': float('Inf')}, | |
'12': {'11': float('Inf'), '7': float('Inf'), '17': float('Inf'), '13': float('Inf')}, | |
'13': {'12': float('Inf'), '18': 1, '14': float('Inf'), '8': 1}, | |
'14': {'13': float('Inf'), '19': float('Inf'), '9': float('Inf'), '15': float('Inf')}, | |
'15': {'10': 1, '14': float('Inf'), '20': 1}, | |
'16': {'11': 1, '17': 1, '21': float('Inf')}, | |
'17': {'12': float('Inf'), '22': 1, '16': 1, '18': 1}, | |
'18': {'17': 1, '13': 1, '23': float('Inf'), '19': 1}, | |
'19': {'14': float('Inf'), '18': 1, '24': 1, '20': 1}, | |
'20': {'15': 1, '19': 1, '25': float('Inf')}, | |
'21': {'16': float('Inf'), '22': float('Inf')}, | |
'22': {'17': 1, '21': float('Inf'), '23': float('Inf')}, | |
'23': {'22': float('Inf'), '18': float('Inf'), '24': float('Inf')}, | |
'24': {'23': float('Inf'), '19': 1, '25': float('Inf')}, | |
'25': {'20': float('Inf'), '24': float('Inf')}, | |
} | |
lastSpin=[0,0] | |
usedID = [] | |
def dijkstra(graph,pradz,pab,lankyt=[],grafas={},tev={}): | |
if pradz == pab: | |
kelias=[] | |
while pab != None: | |
kelias.append(pab) | |
pab=tev.get(pab,None) | |
return grafas[pradz], kelias[::-1] | |
if not lankyt: grafas[pradz]=0 | |
for virs in graph[pradz]: | |
if virs not in lankyt: | |
virsilgis = grafas.get(virs,float('Inf')) | |
grafoilgis = grafas[pradz] + graph[pradz][virs] | |
if grafoilgis < virsilgis: | |
grafas[virs] = grafoilgis | |
tev[virs]=pradz | |
lankyt.append(pradz) | |
nelankyt = dict((k, grafas.get(k,float('Inf'))) for k in graph if k not in lankyt) | |
artim = min(nelankyt, key=nelankyt.get) | |
return dijkstra(graph,artim,pab,lankyt,grafas,tev) | |
def rinkimas(pirm,antr, position): | |
if position == "up": | |
if antr == (pirm - 5): | |
print("0: tiesiai") | |
if antr == (pirm + 5): | |
position = "down" | |
print("apsisuka + tiesiai") | |
if antr == (pirm + 1): | |
position = "right" | |
print("3: desinen + tiesiai") | |
if antr == (pirm - 1): | |
position = "left" | |
print("1: kairen + tiesiai") | |
elif position == "down": | |
if antr == (pirm + 5): | |
print("0: tiesiai") | |
if antr == (pirm - 5): | |
position = "up" | |
print("apsisuka + tiesiai") | |
if antr == (pirm + 1): | |
position = "right" | |
print("1: kairen + tiesiai") | |
if antr == (pirm - 1): | |
position = "left" | |
print("3: desinen + tiesiai") | |
elif position == "left": | |
if antr == (pirm - 1): | |
print("0: tiesiai") | |
if antr == (pirm + 5): | |
position = "down" | |
print("1: kairen + tiesiai") | |
if antr == (pirm - 5): | |
position = "up" | |
print("3: desinen + tiesiai") | |
if antr == (pirm + 1): | |
position = "right" | |
print("apsisuka + tiesiai") | |
elif position == "right": | |
if antr == (pirm + 1): | |
print("0: tiesiai") | |
if antr == (pirm + 5): | |
position = "down" | |
print("3: desinen + tiesiai") | |
if antr == (pirm - 5): | |
position = "up" | |
print("1: kairen + tiesiai") | |
if antr == (pirm - 1): | |
position = "left" | |
print("apsisuka + tiesiai") | |
return position | |
def rinkimas2(pirm,antr, indent): | |
iv = 0 | |
if not(pirm in usedID): | |
usedID.append(pirm) | |
failas.write(str(pirm) + ',') | |
if pirm < antr and indent < antr: | |
failas.write('važiuoti žemyn' + ' ') | |
failas.write("\n") | |
if pirm > antr and abs(pirm-antr) == 1: | |
iv = True | |
failas.write('pasisukti į kairę' + ' ') | |
if pirm > antr and abs(pirm-antr) == 1 and iv == True: | |
iv = False | |
failas.write('Pavaziuoti' + ' ') | |
failas.write("\n") | |
if pirm > antr and indent < pirm: | |
failas.write('važiuoti aukstyn' + ' ') | |
failas.write("\n") | |
if pirm < antr and abs(pirm-antr) == 1: | |
iv = True | |
failas.write('pasisukti į desine' + ' ') | |
if pirm < antr and abs(pirm-antr) == 1 and iv == True: | |
iv = False | |
failas.write('Pavaziuoti' + ' ') | |
failas.write("\n") | |
def sorting(mas, mas2): | |
failas = open("lentele.txt", "r") | |
eil=failas.read() | |
sar=eil.split("\n") | |
daiktas = sar[2].split(",") | |
for i in range (len(sar)-1): | |
daiktas = sar[i].split(",") | |
temp=[] | |
for j in range(len(daiktas)): | |
if j % 2 == 0: | |
temp.append(int(daiktas[j].strip(' '))) | |
else: | |
temp.append(daiktas[j].strip(' ')) | |
mas.append(temp) | |
print(mas) | |
mas2 = sorted(mas, key=lambda x:x[0]) | |
print(mas2) | |
mas3=[] | |
for i in range(len(mas2)): | |
for j in range (len(mas2[i])): | |
if mas2[i][0] == mas2[i-1][0]: | |
mas3.append(mas2[i][0]) | |
mas3.append(mas2[i][1]) | |
else: | |
mas3.append(mas2[i][j]) | |
print(mas3) | |
rez = [] | |
rez = dijkstra(grafas,'2','15') | |
rez[1].insert(0,-3) | |
mas = [] | |
mas2=[] | |
position = "down" | |
for i in range(len(rez[1])): | |
# if int(rez[1][i]) == 2: | |
if i == 0: | |
print('---> Pradžia <---') | |
if int(rez[1][i]) == 15: | |
break; | |
pirm = int(rez[1][i]) | |
antr = int(rez[1][i + 1]) | |
position = rinkimas(pirm,antr,position) | |
failas = codecs.open("lentele.txt", "w","utf-8") | |
print('----------LENTELE-------------------') | |
for u in grafas: | |
for v in grafas[u]: | |
if grafas[u][v] == 1: | |
if int(u) < int(v): | |
ind = int(u) | |
if int(u) > int(v): | |
ind = int(v) | |
if 1 <= ind <= 5: | |
rinkimas2(int(u),int(v), 5) | |
if 6 <= ind <= 10: | |
rinkimas2(int(u),int(v), 10) | |
if 11 <= ind <= 15: | |
rinkimas2(int(u),int(v), 15) | |
if 16 <= ind <= 20: | |
rinkimas2(int(u),int(v), 20) | |
if 21 <= ind <= 25: | |
rinkimas2(int(u),int(v), 25) | |
failas.close() | |
print(usedID) | |
# sorting(mas, mas2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment