Advertisement
MathQ_

Untitled

Nov 26th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. class Vertex:
  2.     def __init__(self, x, y):
  3.         self.x = x
  4.         self.y = y
  5.  
  6.     def __eq__(self, other):
  7.         return self.x == other.x and self.y == other.y
  8.  
  9.     def __ne__(self, other):
  10.         return self.x != other.x or self.y != other.y
  11.  
  12.     def __lt__(self, other):
  13.         if self.x == other.x:
  14.             return self.y < other.y
  15.         else:
  16.             return self.x < other.x
  17.  
  18.     def __hash__(self):
  19.         return hash(str(self.x) + str(self.y))
  20.  
  21.     def __str__(self):
  22.         return str(self.x) + " " + str(self.y)
  23.  
  24.  
  25. height, width = 0, 0
  26. field = []
  27. graph, shortest_way = dict(), dict()
  28.  
  29.  
  30. def file_input(filename):
  31.     global field, height, width
  32.  
  33.     with open(filename, "r") as inp:
  34.         lines = inp.readlines()
  35.         for line in lines:
  36.             field.append(line.split())
  37.     height, width = len(field), len(field[0])
  38.  
  39.  
  40. def check(x, y):
  41.     global field, width, height
  42.  
  43.     if (1 <= x <= width) and (1 <= y <= height):
  44.         return field[x][y] == '1'
  45.     else:
  46.         return False
  47.  
  48.  
  49. def create_graph():
  50.     global field, graph, width, height
  51.  
  52.     for i in range(1, height):
  53.         for j in range(1, width):
  54.             if field[i][j] == '1':
  55.                 v = Vertex(i, j)
  56.  
  57.                 graph[v] = []
  58.                 shortest_way[v] = {}
  59.  
  60.                 if check(i + 1, j):
  61.                     graph[v].append(Vertex(i + 1, j))
  62.  
  63.                 if check(i, j + 1):
  64.                     graph[v].append(Vertex(i, j + 1))
  65.  
  66.                 if check(i, j - 1):
  67.                     graph[v].append(Vertex(i, j - 1))
  68.  
  69.                 if check(i - 1, j):
  70.                     graph[v].append(Vertex(i - 1, j))
  71.                 if i == 2 and j == 1:
  72.                     uuu = Vertex(2, 1)
  73.  
  74.  
  75. def bfs(st):
  76.     global graph, shortest_way
  77.  
  78.     queue = []
  79.     used = {st: True}
  80.     for u in graph[st]:
  81.         shortest_way[st][u] = u
  82.         queue.append(u)
  83.         used[u] = True
  84.  
  85.     while len(queue) > 0:
  86.         v = queue[0]
  87.         queue.remove(queue[0])
  88.         for u in graph[v]:
  89.             if not used.get(u, False):
  90.                 queue.append(u)
  91.                 shortest_way[st][u] = shortest_way[st][v]
  92.                 used[u] = True
  93.  
  94.  
  95. def main(filename):
  96.     file_input(filename)
  97.     create_graph()
  98.     for vertex in graph:
  99.         bfs(vertex)
  100.  
  101.  
  102. # main("input.txt")
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement