day53 第十一章:图论part04


title: 代码随想录 day53 第11章 图论part04

date: 2024-12-22 23:48:48

modificationDate: 2024 十二月 22日 星期日 23:48:48

categories:

  • carl

tags: \[\]

sticky: \[\]

hide: false

category_bar: true


**第十一章:图论part04**

经过上面的练习,大家可能会感觉 广搜不过如此,都刷出自信了,本题让大家初步感受一下,广搜难不在广搜本身,而是如何应用广搜。

https://www.programmercarl.com/kamacoder/0110.字符串接龙.html

```go

// ladderLength 实现 BFS 查找最短路径长度

func ladderLength(beginWord, endWord string, wordList \[\]string) int {

wordSet := make(mapstringstruct{})

for _, word := range wordList {

wordSetword = struct{}{}

}

if _, exists := wordSetendWord; !exists {

return 0

}

queue := \[\]string{beginWord}

visitMap := make(mapstringint)

visitMapbeginWord = 1

for len(queue) > 0 {

currentWord := queue0

queue = queue1:

path := visitMapcurrentWord

for i := 0; i < len(currentWord); i++ {

chars := \[\]rune(currentWord)

for ch := 'a'; ch <= 'z'; ch++ {

charsi = ch

newWord := string(chars)

if newWord == endWord {

return path + 1

}

if _, exists := wordSetnewWord; exists {

if _, visited := visitMapnewWord; !visited {

visitMapnewWord = path + 1

queue = append(queue, newWord)

}

}

}

}

}

return 0

}

```

深搜有细节,同样是深搜两种写法的区别,以及什么时候需要回溯操作呢?

https://www.programmercarl.com/kamacoder/0105.有向图的完全可达性.html

```python

import collections

path = set() # 纪录 BFS 所经过之节点

def bfs(root, graph):

global path

que = collections.deque(root)

while que:

cur = que.popleft()

path.add(cur)

for nei in graphcur:

que.append(nei)

graphcur = \[\]

return

def main():

N, K = map(int, input().strip().split())

graph = collections.defaultdict(list)

for _ in range(K):

src, dest = map(int, input().strip().split())

graphsrc.append(dest)

bfs(1, graph)

if path == {i for i in range(1, N + 1)}:

return 1

return -1

if name == "main":

print(main())

```

简单题,避免大家惯性思维,建议大家先独立做题。

https://www.programmercarl.com/kamacoder/0106.岛屿的周长.html

```python

def main():

import sys

input = sys.stdin.read

data = input().split()

读取 n 和 m

n = int(data0)

m = int(data1)

初始化 grid

grid = \[\]

index = 2

for i in range(n):

grid.append(int(data\[index + j) for j in range(m)])

index += m

sum_land = 0 # 陆地数量

cover = 0 # 相邻数量

for i in range(n):

for j in range(m):

if gridij == 1:

sum_land += 1

统计上边相邻陆地

if i - 1 >= 0 and gridi - 1j == 1:

cover += 1

统计左边相邻陆地

if j - 1 >= 0 and gridij - 1 == 1:

cover += 1

不统计下边和右边,避免重复计算

result = sum_land * 4 - cover * 2

print(result)

if name == "main":

main()

```

相关推荐
右耳朵猫AI4 小时前
Python周刊2026W22 | Django 6.1 Alpha 1发布、Nuitka 4.1发布、PEP 831终稿、PEP 808已接受
开发语言·python·django
半个烧饼不加肉4 小时前
JS 底层探究-- 普通函数和构造函数
开发语言·javascript·原型模式
小白不白1114 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
程序猿乐锅4 小时前
【JAVASE | 第十七篇】Java 网络通信
java·开发语言
飞舞哲4 小时前
三维点云最小二乘拟合MATLAB程序
开发语言·算法·matlab
有点。4 小时前
C++(贪心算法二)
开发语言·c++·贪心算法
meilindehuzi_a4 小时前
透视 V8 底部:从物理内存到函数式哲学,重新解构 JavaScript 数组
开发语言·javascript·ecmascript
jllllyuz4 小时前
HVDC 高压直流输电系统 MATLAB/Simulink 仿真全集
开发语言·matlab
我命由我123454 小时前
Windows 操作系统 - Windows 查看防火墙是否开启、Windows 查看防火墙放行端口
java·运维·开发语言·windows·java-ee·操作系统·运维开发
天天进步20154 小时前
Python全栈项目--基于Python的数据库管理工具
开发语言·数据库·python