DFS和BFS,以及多源BFS

深度优先遍历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.
思路&#

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值