SZI2019SmieciarzWmi/Traversal/BFS.py
2019-05-14 22:31:30 +02:00

53 lines
2.0 KiB
Python

from utilities import movement,check_moves
from DataModels.House import House
from DataModels.Dump import Dump
from DataModels.Container import Container
from config import GRID_WIDTH, GRID_HEIGHT
from collections import deque
def BFS(grid, available_movement, gc_moveset, mode):
queue = deque()
visited_nodes=[]
for x in range(GRID_WIDTH):
visited_nodes.append([])
for y in range(GRID_HEIGHT):
visited_nodes[-1].append(0)
visited_nodes[gc_moveset[-1][0]][gc_moveset[-1][1]] = 1
queue.append([available_movement, gc_moveset, visited_nodes])
while queue:
possible_goals = []
state = queue.popleft()
avalible_movement_state = state[0]
gc_moveset_state = state[1]
visited_nodes_state = state[2]
a = gc_moveset_state[-1][0]
b = gc_moveset_state[-1][1]
possible_goals.append([a+1,b])
possible_goals.append([a-1,b])
possible_goals.append([a,b+1])
possible_goals.append([a,b-1])
object_in_area = False
for location in possible_goals:
if GRID_WIDTH>location[0]>=0 and GRID_HEIGHT>location[1]>=0:
cell = grid[location[0]][location[1]]
if(type(cell) == mode and cell.unvisited):
cell.unvisited = False
object_in_area = True
break
x,y = gc_moveset_state[-1]
if(object_in_area):
gc_moveset_state.append("pick_garbage")
return (cell,[x,y], gc_moveset_state)
for direction in avalible_movement_state:
x_next, y_next = movement(grid,x,y)[0][direction]
if visited_nodes_state[x_next][y_next]==0:
available_movement_next = check_moves(grid, x_next,y_next,direction)
gc_moveset_next = gc_moveset_state.copy()
gc_moveset_next.append([x_next,y_next])
visited_nodes_state[x_next][y_next]=1
queue.append([available_movement_next, gc_moveset_next,visited_nodes_state])