I was trying to solve this problem. I solved it using bfs. However, I would like to know how can I generate the whole graph beforehand — before processing it in dfs or any other algorithm- ? Another problem that I would like to employ this technique on is problem A in this contest
You can't generate full graph because it's infinite :)
You can press first button as many times as you want and get as big number as you want. BFS looks only for verticles which are at distance not more than some value and there are finite number of vertciles. When BFS from start verticle reaches end verticle it says: "There is path of some length, there are no shorter pathes, so there is no reason to look to other verticles"