59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
import settings
|
|
import math
|
|
from collections import defaultdict
|
|
|
|
|
|
class Instance:
|
|
def __init__(self):
|
|
self.graph = defaultdict(list)
|
|
|
|
def config(self, fields):
|
|
adjacency_list = self.generate_graph()
|
|
fields_to_visit = self.generate_target_fields(fields)
|
|
print("Adjacency list: " + str(dict(adjacency_list)))
|
|
print("List of targets: " + str(fields_to_visit))
|
|
|
|
def add_edge(self, u, v):
|
|
self.graph[u].append(v)
|
|
|
|
def generate_graph(self):
|
|
width = settings.Field.horizontal_count()
|
|
height = settings.Field.vertical_count()
|
|
|
|
for i in range(height):
|
|
for j in range(width):
|
|
point = i * width + j
|
|
# point_row = math.floor(point/width)
|
|
# point_column = point % width
|
|
point_left = i * width + j - 1
|
|
point_left_column = point_left - width * i
|
|
point_right = i * width + j + 1
|
|
point_right_column = point_right % width
|
|
point_up = (i - 1) * width + j
|
|
point_up_row = math.floor(point_up / width)
|
|
point_down = (i + 1) * width + j
|
|
point_down_row = math.floor(point_down / width)
|
|
|
|
if point_left_column >= 0:
|
|
self.add_edge(point, point_left)
|
|
if point_right_column > 0:
|
|
self.add_edge(point, point_right)
|
|
if point_up_row >= 0:
|
|
self.add_edge(point, point_up)
|
|
if point_down_row < height:
|
|
self.add_edge(point, point_down)
|
|
|
|
return self.graph
|
|
|
|
@staticmethod
|
|
def generate_target_fields(fields):
|
|
width = settings.Field.horizontal_count()
|
|
height = settings.Field.vertical_count()
|
|
fields_to_visit = []
|
|
for i in range(height):
|
|
for j in range(width):
|
|
point = i * width + j
|
|
if fields[j][i] == 'dirt':
|
|
fields_to_visit.append(point)
|
|
return fields_to_visit
|