Task:
You need to get out of the labyrinth. You don't know how many rooms it has or what its map is. You can move freely in both directions along all corridors, all rooms and corridors look the same (rooms may differ only in the number of corridors). Let k be the number of corridors between rooms.
Propose an algorithm that finds a way out of the labyrinth, or proves that there is no way out, for O(k) transitions between rooms. You have an unlimited number of coins at your disposal, which you can leave in the rooms.
My thoughts:
I have had two attempts at a solution, one using BFS and one using DFS, but in both of them I encounter difficulties and need to introduce some modifications.
1. BFS: Starting from the starting room we visit one room on each corridor. Put a coin in the visited room and return to the starting room. Then from the starting room go first to the rooms of the first level and in each of them also visit all rooms at a distance of $1$ on each of the new corridors of this room. We put a coin if there was no coin in the room. Then take two steps back, returning to the starting room. Until we find the exit, repeat these steps, increasing the range of each next move by $1$. However, there are two problems that confuse me here: 1.1) Will the fact that I walk back to the starting room each time preserve the linear asymptotics of the solution? It seems to me that it doesn't anymore. 1.2) In this solution, how do I resolve the situation where there are no solutions? There must be some way to check the fact of visiting all rooms. The possible existence of cycles is especially complicated here.
2. DFS: Do DFS, starting from the starting room, with corridors bypassed, always starting from the rightmost one (if there are several corridors in the room). We leave $3$ groups of coins in the room: 1st group of coins - opening time, 2nd group of coins - corridor counter, 3rd group of coins - closing time. We do it this way: fix a discrete counter of opening time (starting from $1$ in the starting room), increasing it by $1$ at each step (opening or closing). (i.e. in the second room in 1st pile of coins we will already put $2$ coins, etc.), and also in the second pile of coins we will put the counter on the number of corridors (before we are going to go from the room along the corridor, which has not yet walked, add a coin to the second group. If the number of coins in the second group becomes equal to the number of corridors in this room, then consider that we have passed all the corridors from this room and then add to the third pile as many coins as now on the time counter (the condition must also be fulfilled here that all descendant vertices are already closed)). If we go this way to the starting top and close it, it means that there was no exit. The problem in this solution: if there are cyclic paths in the graph, then we can pass some paths without closing vertices in them and return to the parent. Then the parent cannot be closed, because the descendants are still not closed. But in order to close a descendant it is necessary to pass this path again. But there is no guarantee that in case of a labyrinth with a lot of branching, we have the right to close vertices at the second pass through them.
I have absolutely no idea how to upgrade counters with coins in order to solve problematic situations with cycles. I will be grateful for any ideas or other solution.