Https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/practice-problems/algorithm/agitated-chandan/

why does bfs and dfs approach both work for this problem…
??plz explain …

also provide solution for this problem…