Skip to content

Instantly share code, notes, and snippets.

@ovidijusr
Created April 27, 2016 07:11
Show Gist options
  • Save ovidijusr/1fd1a69e082cda2666d815b0deae9068 to your computer and use it in GitHub Desktop.
Save ovidijusr/1fd1a69e082cda2666d815b0deae9068 to your computer and use it in GitHub Desktop.
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]
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, indent):
if pirm < antr and indent < antr:
print('važiuoti žemyn')
if pirm < antr and indent >= antr:
if lastSpin[1] == indent and lastSpin[0]+1 == antr:
print('važiuoti tiesiai')
else:
print('pasuka į kairę')
print('vaziuoti tiesiai')
if pirm > antr and indent < pirm:
print('važiuoti aukstyn')
if pirm > antr and indent > pirm:
if lastSpin[1] == indent and lastSpin[0] - 1 == antr:
print('važiuoti tiesiai')
else:
print('pasisukti į desine')
print('vaziuoti tiesiai')
lastSpin[0] = antr
lastSpin[1] = indent
rez = []
rez = dijkstra(grafas,'2','15')
print (rez)
for i in range(len(rez[1])):
if int(rez[1][i]) == 2:
print('---> Pradžia <---')
if int(rez[1][i]) == 15:
break;
pirm = int(rez[1][i])
antr = int(rez[1][i+1])
if 1 <= int(rez[1][i]) <= 5:
rinkimas(pirm,antr, 5)
if 6 <= int(rez[1][i]) <= 10:
rinkimas(pirm,antr, 10)
if 11 <= int(rez[1][i]) <= 15:
rinkimas(pirm,antr, 15)
if 16 <= int(rez[1][i]) <= 20:
rinkimas(pirm,antr, 20)
if 21 <= int(rez[1][i]) <= 25:
rinkimas(pirm,antr, 25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment