◂ 15 décembre 2024 ▸
Warehouse Woes : Déplacer des caisses façon sokoban, mais avec des caisses prenant deux places en largeur.
########## … ###
##.#...OO# … O.#
#O#O.....O … ..#
⋮
#..O.O.O.. … #O#
#.....O.OO … OO#
########## … ###
<v><v><> …
>^><<>vv …
⋮
Toute petite optimisation possible (qui ne fait gagner aucun temps) quand on touche une boîte horizontalement : s'occuper directement de la partie non touchée, qu'on sait être derrière.
- code_part_1_2.py
- code_part1_simple_affichageConsole.py
- code_part2_affichageConsole.py
- diff_adaptationAlgo_2_PourPartie_1.diffy
- diff_optimPushingHorizontally.diffy
- affichage_console_partie1.png
- affichage_console_partie2.png
- Visualisation superbe sur YouTube ⬀ En 3D avec images réalistes et faite par Paul Bonsma
def next_grid(grid, x, y, depl_x, depl_y): def next_grid(grid, x, y, depl_x, depl_y):
### Find what needs to move ### Find what needs to move
pushed = set() pushed = set()
todo = set([(x, y)]) todo = set([(x, y)])
while todo: while todo:
bx, by = todo.pop() bx, by = todo.pop()
pushed.add( (bx,by) ) pushed.add( (bx,by) )
nx, ny = bx + depl_x, by + depl_y nx, ny = bx + depl_x, by + depl_y
if (nx,ny) in pushed or (nx,ny) in todo: if (nx,ny) in pushed or (nx,ny) in todo:
continue continue
next_c = grid[ny][nx] next_c = grid[ny][nx]
if next_c == '#': if next_c == '#':
return None return None
elif next_c in '[]': elif next_c in '[]':
todo.add( (nx, ny) ) # Process touched part <
if not depl_y: # If pushing horizontally if not depl_y: # If pushing horizontally
> pushed.add( (nx,ny) ) # just push touched part later
todo.add( (bx+2*depl_x, by) ) # process other todo.add( (bx+2*depl_x, by) ) # and process only the other part
elif next_c == '[': # If pushing vertic. left | else:
> todo.add( (nx, ny) ) # Process touched part
> if next_c == '[': # If pushing vertically on the left
todo.add( (bx+1,ny) ) # process right todo.add( (bx+1,ny) ) # process the right part of the box
else: # If pushing vertic. right else: # If pushing vertically on the right
todo.add( (bx-1,ny) ) # process left todo.add( (bx-1,ny) ) # process the left part
### And push everything! ### And push everything!
... ...