How to use DFS on an array

隐身守侯 提交于 2019-12-01 20:31:13
  • Make a 2D array boolean[][] visited designating the points that you have visited; set all elements to false
  • Go through each point in two nested loops
  • For each point where visited[r][c] is false, go into a DFS which you can implement recursively
  • Inside the recursive DFS call check if the point is your destination; if yes, return true
  • If the point has the correct color, explore its neighbors (up to four) in four directions
  • If a neighbor has the right color, mark it as visited, and make a recursive call
  • If a recursive call returns true, return true
  • Otherwise, continue exploring other neighbors
  • Once you are done exploring neighbors, return false.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!