Hi everyone,
I'm trying to solve LeetCode's "Snakes and Ladders" using DFS with memoization (dp[square] for min moves) and a visited_in_path[square] check to prevent infinite loops if square is already in the current recursion stack.
However, for some test cases, my DFS outputs a suboptimal path (e.g., 4 moves when 3 is expected).
My Core Confusion:
I understand visited_in_path stops simple stack overflow cycles like A → B → A. But I'm struggling to see exactly how this check causes my dp values for some squares to become permanently suboptimal, leading to a longer overall path found.
Specifically, can the true shortest path for a subproblem (e.g., from square X) sometimes require taking a snake or ladder to an Ancestor square (where Ancestor is still active higher up in dfs(X)'s current call stack)? If so, my visited_in_path[Ancestor] check would prune this path segment for X. How does this pruning specifically lead to dp[X] (and subsequently dp[Start_Square]) being wrong, rather than just avoiding a simple loop? I'm looking for a clear, minimal example that demonstrates how pruning based on an active ancestor in visited_in_path results in an incorrect dp value for an intermediate square, making the final answer suboptimal. Why isn't the first time dp[square] is fully computed always the true shortest path from that square with this DFS strategy?









Did you actually paid attention to "the cells are labeled from 1 to n2 in a Boustrophedon style"
How does it change anything in the process of solving it through the method he mentioned.
The point is: I am pretty confident his solution is right; That said the implementation of this problem is ultra annoying because they defined the board in the most bullshit way possible
thanks, mate for highlighting that! I've definitely been working with the Boustrophedon labeling for any state calculations that depend on the cell IDs and their corresponding grid positions.
Regarding its direct impact on the DFS itself, I'm curious about the specific aspect you have in mind. Are you thinking, for instance, about how it could influence the order of exploring neighbors if the adjacency logic isn't careful to correctly map Boustrophedon labels to actual grid adjacencies, or perhaps something else specific to the traversal mechanics? I've been primarily focused on ensuring the state definitions and transitions are correct under this labeling, so if there's a particular nuance for the DFS search pattern itself that I might be overlooking, I'm all ears for a hint!
I was afraid you were not careful on the mapping; also why do you do a dfs instead of a bfs, which is more appropriate for single source single target problem?
I have never seen single source single target problem being solved with dfs, as it is not guaranteed to find the minimum distance path
Yeah I solved that using bfs, the minimum distance give me a hint for that, although I was curious on how the dfs will turn out, spend rest of my day figuring that out :'( might need a licor 43 now lol.
How would you have solved this using dfs?
I don't think there is a way to solve this problem using dfs unless the implicit graph of the problem was a DAG (directed acyclic graph)
Sure will avoid dfs & dp on graphs which are not DAG.
Thanks mate!