Advertisement
GreenMap

Лабораторная работа 4 МАПКС эйлеров цикл

Nov 5th, 2020
2,296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.07 KB | None | 0 0
  1. def find_value(l, value):
  2.     result = []
  3.     for i, val in enumerate(l):
  4.         if value == val:
  5.             result.append(i)
  6.     return result
  7. def edges_counter(edges):
  8.     result = len(edges[0])
  9.     for i in range(0, len(edges[0])):
  10.         start_point = edges[0][i]
  11.         end_point = edges[1][i]
  12.         for j in range(0, len(edges[0])):
  13.             if start_point == edges[1][j] and end_point == edges[0][j]:
  14.                 result -= 0.5
  15.     return int(result)
  16. def incorrect_remove(ways):
  17.     del_ways = []
  18.     for way in ways:
  19.         for i_f in range(0, len(way)-1):
  20.             first_edges = [way[i_f] + way[i_f+1], way[i_f+1] + way[i_f]]
  21.             for i_s in range(0, len(way)-1):
  22.                 if i_f != i_s and way[i_s] + way[i_s+1] in first_edges and way[i_s+1] + way[i_s] in first_edges:
  23.                     del_ways.append(way)
  24.         del_ways = list(set(del_ways))
  25.     for d_way in del_ways:
  26.         ways.remove(d_way)
  27. def printer(ways):
  28.     for way in ways:
  29.         if way[0] == way[-1]:
  30.             way_print = ""
  31.             for i in range(0, len(way)):
  32.                 way_print += str(int(way[i])+1)
  33.             print(way_print)
  34.  
  35.        
  36. start_point = int(input("Начальная точка: ")) - 1
  37. m_size = int(input("Размер матрицы: "))
  38. matric = []
  39. for i in range(0, m_size):
  40.     converted_string = []
  41.     for num in input("Введите " + str(i + 1) + " срочку матрицы: ").split(" "):
  42.         converted_string.append(int(num))
  43.     matric.append(converted_string)
  44. edges = [[], []]
  45. for i_string in range(0, m_size):
  46.     for i_val in range(0, m_size):
  47.         if matric[i_string][i_val] == 1:
  48.             edges[0].append(str(i_string))
  49.             edges[1].append(str(i_val))
  50. ways = [str(start_point)]
  51. while(len(ways[0]) <= edges_counter(edges)):
  52.     update_ways = []
  53.     for i_way in range(0, len(ways)):
  54.         for i_edge in find_value(edges[0], ways[i_way][-1]):
  55.             update_ways.append(ways[i_way] + str(edges[1][i_edge]))
  56.     ways = update_ways[:]
  57.     incorrect_remove(ways)
  58. printer(ways)
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement