深度优先遍历DFS
使用回溯法以及栈结构实现,用于寻找全部解中的最优解等任务。
搜索时需要将走过的位置标记出来,当回溯时,可能需要取消一部分节点标记。
广度优先遍历BFS
使用队列结构实现,常用来求最短路径,最近距离点等任务。
已经探索过的节点需要进行标记,且标记之后不会取消,即不会被重复探索。
DFS与BFS的对比
DFS浪费时间,节省空间
BFS节省时间,浪费空间
多源BFS
来自于一道leetcode的算法题:1162.地图分析(个人理解为求max in all mins)
Given an N x N grid containing only values 0 and 1, where 0 represents water and 1 represents land, find a water cell such that its distance to the nearest land cell is maximized and return the distance.
The distance used in this problem is the Manhattan distance: the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|.
If no land or water exists in the grid, return -1.
思路&#