SZI2019SmieciarzWmi/Traversal/DFS.py

40 lines
1.4 KiB
Python
Raw Permalink Normal View History

from utilities import movement,check_moves
2019-04-23 20:48:14 +02:00
from DataModels.House import House
2019-05-13 09:45:09 +02:00
from DataModels.Dump import Dump
2019-04-23 21:45:16 +02:00
from DataModels.Container import Container
2019-05-13 09:45:09 +02:00
from config import GRID_WIDTH, GRID_HEIGHT
2019-04-24 07:34:01 +02:00
2019-05-13 09:45:09 +02:00
def DFS(grid, available_movement, gc_moveset, mode,depth=0):
2019-05-13 13:16:36 +02:00
2019-04-23 20:48:14 +02:00
possible_goals = []
a = gc_moveset[-1][0]
b = gc_moveset[-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])
2019-05-13 09:45:09 +02:00
object_in_area = False
2019-04-23 20:48:14 +02:00
for location in possible_goals:
2019-05-13 09:45:09 +02:00
if GRID_WIDTH>location[0]>=0 and GRID_HEIGHT>location[1]>=0:
cell = grid[location[0]][location[1]]
2019-05-13 13:16:36 +02:00
if(type(cell) == mode and cell.unvisited):
cell.unvisited = False
object_in_area = True
break
x,y = gc_moveset[-1]
2019-05-13 09:45:09 +02:00
if(object_in_area):
2019-04-23 21:45:16 +02:00
gc_moveset.append("pick_garbage")
2019-05-14 22:31:30 +02:00
return [cell,[x,y], gc_moveset]
2019-04-23 23:05:34 +02:00
if len(available_movement) == 0 or depth>30:
return
2019-04-23 21:45:16 +02:00
for direction in available_movement:
x_next, y_next = movement(grid,x,y)[0][direction]
2019-04-23 21:45:16 +02:00
available_movement_next = check_moves(grid, x_next,y_next,direction)
gc_moveset_next = gc_moveset.copy()
gc_moveset_next.append([x_next,y_next])
2019-05-13 09:45:09 +02:00
result = DFS(grid, available_movement_next, gc_moveset_next,mode, depth+1)
2019-04-23 23:05:34 +02:00
if result!= None:
return result