#đź”’ Recursive Backtracking
84 messages · Page 1 of 1 (latest)
@split cradle
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Do you understand recursion at all. Like have you ever used it before @split cradle
yeah they don't, you asked if i've ever used recursion
I know what I asked I was just talking to myself
oh oh okay
Backtracking is essentially just using recursion to go “all-in” on a solution, and then trying different solutions from the point where the old solution goes bad or could have failed
I’m guessing you need backtracking because your algorithm may generate an invalid maze or something and you need to correct that?
i think its when the cell has no "unvisited neighbours"
How does the algorithm work, like how are you generating a maze
choose a point, mark it as visited, choose a unvisited neigbiour and break the wall between them
yes
Yeah alright I see where the backtracking is
Once you find no unvisited neighbors, you will return from the most recent recursive call
Which will put you in the second to last recursive call
Where you now continue on a different path as if nothing happened
How are you representing this grid and walls
And what questions do you have
1 being walls, and 0 being the empty places, and it's represented with a 2d array
first of all the grid layout, do i just start with all walls(1's) or do i need the empty places there as well (0's)
I’d start with a single 0 in like the top left
so that corner would look like this?
1 1
1 0```
Something important if you do that setup: you won’t be checking cells that are directly next to each other
yeah would have to check 2 cells away
Yes if you want 1s surrounding the whole thing
Yup exactly
yeah gonna have to
how do you mean
One of these is a thin walled maze
Where the walls don’t take up a tile
Your maze will be like the green
ohhh, okay, yeah that's not a problem
im going to represent it on turtle anyways, so i can change the width
But you can’t
oh
The walls have to be as think as a tile
alrightm either way, not a problem
maze = [
[1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,0,1,0,1],
[1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,0,1,0,1],
[1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,0,1,0,1],
[1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,0,1,0,1],
[1,1,1,1,1,1,1,1,1]
]
visited = [
[1,1,1,1,1,1,1,1,1],
[1,False,1,False,1,False,1,False,1],
[1,1,1,1,1,1,1,1,1],
[1,False,1,False,1,False,1,False,1],
[1,1,1,1,1,1,1,1,1],
[1,False,1,False,1,False,1,False,1],
[1,1,1,1,1,1,1,1,1],
[1,False,1,False,1,False,1,False,1],
[1,1,1,1,1,1,1,1,1]
]```
this was my original setup
I would start with a single 0
And if a cell is a 0 it’s visited
You only need 1 2d array
ohh right
See how your visited array matches your other array
and for a 4x4 maze, the array would need to be 9x9 right
Yeah that’s why having thick walls can make your arrays quite big
But it’s alright
so how can i make it with thin walls
Imagine your maze starts as a grid
With thin walls
A 4x4 maze starts as a 4x4 array
0s can represent cells completely surrounded with walls
1s can mean left side is open
2s can mean top is open
6s can mean top and bottom is open etc
More complicated, and I think thick wall is fine
would thick walls get more complicated with user defined width and height
okay
alright, appreciate it
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.