29 lines
861 B
Python
29 lines
861 B
Python
class Graph:
|
|
def __init__(self):
|
|
self.nodes = {}
|
|
|
|
def neighbors(self, id):
|
|
return self.nodes[id][0]
|
|
|
|
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:
|
|
self.nodes[node][0].add(neighbor)
|
|
else:
|
|
self.nodes[node] = (set([neighbor]), False)
|
|
if neighbor in self.nodes:
|
|
self.nodes[neighbor][0].add(node)
|
|
else:
|
|
self.nodes[neighbor] = (set([node]), False)
|
|
|