dfs: refine more code

clean code with pointer rather than index