Advertisement
Guest User

asdasd

a guest
Mar 25th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. from random import randint
  2.  
  3. class Point:
  4. def __init__(self, x, y, connect):
  5. self.x = x
  6. self.y = y
  7. self.connect = connect
  8.  
  9. def __str__(self):
  10. return '({}, {}, {})'.format(self.x, self.y, self.connect)
  11.  
  12. def __repr__(self):
  13. return '({}, {}, {})'.format(self.x, self.y, self.connect)
  14.  
  15. def __eq__(self, other):
  16. return self.x == other.x and self.y == other.y
  17.  
  18. def __hash__(self):
  19. return self.x ** self.y
  20.  
  21. def bfs(s, t, graph):
  22. if s == t:
  23. return {}
  24. queue = [s]
  25. visits = {}
  26. for i in graph.keys():
  27. visits[i] = False
  28. visits[s] = True
  29. path = { s: -1 }
  30. while queue:
  31. v = queue[0]
  32. queue = queue[1:]
  33. for u in graph[v]:
  34. if (not u.connect or visits[u]):
  35. continue
  36. visits[u] = True
  37. queue.append(u)
  38. path[u] = v
  39. if u == t:
  40. return path
  41. return path
  42.  
  43. def get_path(path, s, t):
  44. arr = [t]
  45. if not t in path:
  46. return []
  47. while t != s:
  48. arr.append(path[t])
  49. t = path[t]
  50. return arr[::-1]
  51.  
  52. def delete_node(graph, v):
  53. for node in graph[v]:
  54. node.connect = False
  55.  
  56. def start(N, M):
  57. all_nodes = N*M - 2*M
  58. delete_nodes = 0
  59. max_x, max_y = [N, M] #or [int(i) for i in input().split()]
  60. points = []
  61. for y in range(max_y):
  62. for x in range(max_x):
  63. points.append(Point(x, y, True))
  64.  
  65. graph = {}
  66.  
  67. for point in points:
  68. graph[point] = []
  69. if point.x > 0:
  70. graph[point].append(Point(point.x-1, point.y, True))
  71. if point.x < max_x - 1:
  72. graph[point].append(Point(point.x+1, point.y, True))
  73. if point.y > 0:
  74. graph[point].append(Point(point.x, point.y-1, True))
  75. if point.y < max_y - 1:
  76. graph[point].append(Point(point.x, point.y+1, True))
  77.  
  78.  
  79. p = bfs(Point(0, 0, True), Point(max_x - 1, 0, True), graph)
  80. path = get_path(p, Point(0, 0, True), Point(max_x - 1, 0, True))
  81. while p:
  82. del_node = Point(randint(1, max_x - 2), randint(0, max_y - 1), True)
  83. check_connectivity = False
  84. for i in graph[del_node]:
  85. if i.connect:
  86. check_connectivity = True
  87. break
  88. if check_connectivity:
  89. delete_nodes += 1
  90. delete_node(graph, del_node)
  91. p = bfs(Point(0, 0, True), Point(max_x - 1, 0, True), graph)
  92. if del_node in path:
  93. path = get_path(p, Point(0, 0, True), Point(max_x - 1, 0, True))
  94. if not path:
  95. #print(del_node)
  96. return delete_nodes/all_nodes
  97. #print(path, del_node)
  98.  
  99. if __name__ == '__main__':
  100. count_experiments = 1
  101. sum_ = 0
  102. for i in range(count_experiments):
  103. sum_ += start(20, 10)
  104. print(sum_/count_experiments)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement