Advertisement
Guest User

Untitled

a guest
Feb 16th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import random
  2.  
  3.  
  4. with open(input("Podaj plik bez rozszerzenia\n") +'.txt', 'r') as file:
  5.         MACIERZ = [[int(float(number)) for number in line.split(' ')] for line in file.readlines()]
  6.  
  7.  
  8. def losowa_sciezka(obecny):
  9.     lista_nastepnikow =[]
  10.     for i in range(len(MACIERZ)):
  11.         if MACIERZ[obecny][i]>0:
  12.             lista_nastepnikow.append(i)
  13.    
  14.     if not lista_nastepnikow:
  15.         return None
  16.     else:
  17.         return random.choice(lista_nastepnikow)
  18.  
  19.  
  20. if __name__ == '__main__':
  21.     mrowki = 5
  22.     rozwiazania = []
  23.  
  24.     for i in range(mrowki):
  25.         obecny = random.randint(0, len(MACIERZ)-1)
  26.         koszt = 0
  27.         sciezka = []
  28.         sciezka.append(obecny)
  29.         nastepny = losowa_sciezka(obecny)
  30.         while len(set(sciezka)) != len(MACIERZ): # liczba wierzcholkow
  31.             sciezka.append(nastepny)
  32.             koszt += MACIERZ[obecny][nastepny]
  33.             obecny = nastepny
  34.             nastepny = losowa_sciezka(obecny)
  35.         rozwiazania.append((koszt, sciezka))
  36.  
  37.     print(f'Ilosc wierzchołków: {len(MACIERZ)}')
  38.     for wynik in rozwiazania:
  39.         print(f'Koszt: {wynik[0]}, Długosc sciezki: {len(wynik[1])}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement