SzybciorSmartTraktor/bfs.py

52 lines
1.8 KiB
Python
Raw Normal View History

import settings
import math
def generate_neighbours_list():
width = settings.Field.horizontal_count()
height = settings.Field.vertical_count()
keys = [width for width in range(height * width)]
neighbours_list = {key: [] for key in keys}
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:
neighbours_list[point].append(point_left)
if point_right_column > 0:
neighbours_list[point].append(point_right)
if point_up_row >= 0:
neighbours_list[point].append(point_up)
if point_down_row < height:
neighbours_list[point].append(point_down)
return neighbours_list
def generate_fields_lists(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
def generate_all(fields):
neighbours_list = generate_neighbours_list()
fields_to_visit = generate_fields_lists(fields)
print(neighbours_list)
print(fields_to_visit)