2019-05-08 02:31:29 +02:00
|
|
|
class Graph:
|
2019-05-07 15:37:27 +02:00
|
|
|
def __init__(self):
|
2019-05-08 02:31:29 +02:00
|
|
|
self.nodes = {}
|
2019-05-07 15:37:27 +02:00
|
|
|
|
|
|
|
def neighbors(self, id):
|
2019-05-27 07:40:42 +02:00
|
|
|
return self.nodes[id][0]
|
2019-05-08 02:31:29 +02:00
|
|
|
|
|
|
|
def direction2point(self, point, direction):
|
|
|
|
if direction == 'N':
|
|
|
|
return (point[0] - 1, point[1])
|
|
|
|
elif direction == 'S':
|
|
|
|
return (point[0] + 1, point[1])
|
|
|
|
elif direction == 'W':
|
|
|
|
return (point[0], point[1] - 1)
|
|
|
|
else:
|
|
|
|
return (point[0], point[1] + 1)
|
|
|
|
|
|
|
|
def add_neighbor(self, node, direction):
|
|
|
|
neighbor = self.direction2point(node, direction)
|
|
|
|
if node in self.nodes:
|
2019-05-27 07:40:42 +02:00
|
|
|
self.nodes[node][0].add(neighbor)
|
2019-05-08 02:31:29 +02:00
|
|
|
else:
|
2019-05-27 07:40:42 +02:00
|
|
|
self.nodes[node] = (set([neighbor]), False)
|
2019-05-08 02:31:29 +02:00
|
|
|
if neighbor in self.nodes:
|
2019-05-27 07:40:42 +02:00
|
|
|
self.nodes[neighbor][0].add(node)
|
2019-05-08 02:31:29 +02:00
|
|
|
else:
|
2019-05-27 07:40:42 +02:00
|
|
|
self.nodes[neighbor] = (set([node]), False)
|
2019-05-07 15:37:27 +02:00
|
|
|
|