Smart path finding algorithm suggestion needed.
I'm writing a small program for my leisure. The program will create a 3D dungeon, the size is base on the user input. It has a starting point and an exit point, filled with any number of treasure. The idea is to let user enter a direction, visit all the block before exiting the dungeon. And this is the part I'm struggling, I need a function to check if the dungeon has any valid escape route. I wrote the has_escape_route() function. The idea is to check all adjacent grids against the last move. Since the player can move to 5 different directions in theory (except the starting point, which has 6 possible moves), "path to evaluate" growth exponentially, eventually running forever even the dungeon is shaped 3x4x4. For a small dungeon like 2x3x3, it takes around 10 seconds to finish the calculation on my 12th gen i5 laptop. This is the code I come up with so far. Is there any smarter method instead of brute force? https://sololearn.com/compiler-playground/cF4W4g08p9rl/?ref=app