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(map[string]struct{})

for _, word := range wordList {

wordSet[word] = struct{}{}

}

if _, exists := wordSet[endWord]; !exists {

return 0

}

queue := []string{beginWord}

visitMap := make(map[string]int)

visitMap[beginWord] = 1

for len(queue) > 0 {

currentWord := queue[0]

queue = queue[1:]

path := visitMap[currentWord]

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

chars := []rune(currentWord)

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

chars[i] = ch

newWord := string(chars)

if newWord == endWord {

return path + 1

}

if _, exists := wordSet[newWord]; exists {

if _, visited := visitMap[newWord]; !visited {

visitMap[newWord] = 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 graph[cur]:

que.append(nei)

graph[cur] = []

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())

graph[src].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(data[0])

m = int(data[1])

初始化 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 grid[i][j] == 1:

sum_land += 1

统计上边相邻陆地

if i - 1 >= 0 and grid[i - 1][j] == 1:

cover += 1

统计左边相邻陆地

if j - 1 >= 0 and grid[i][j - 1] == 1:

cover += 1

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

result = sum_land * 4 - cover * 2

print(result)

if name == "main":

main()

```

相关推荐
CodeCraft Studio1 分钟前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建气泡图
信息可视化·c#·excel·aspose·excel api库·excel气泡图·excel组件库
CodeCraft Studio6 分钟前
国产化Excel开发组件Spire.XLS教程:使用Python批量删除Excel分页符
开发语言·python·excel·python开发·spire.xls·excel api库·excel开发组件
木子欢儿9 分钟前
在 Debian 13 上搭建一个 NTP (Network Time Protocol) 服务器
运维·服务器·开发语言·debian·php
凯子坚持 c18 分钟前
Qt 信号与槽机制深度解析
开发语言·qt
bybitq20 分钟前
Go-Package-Module-functions
开发语言·后端·golang
廋到被风吹走24 分钟前
【Java】【JVM】OOM 原因、定位与解决方案
java·开发语言·jvm
MSTcheng.27 分钟前
【C++STL】map / multimap 保姆级教程:从底层原理到实战应用!
开发语言·c++·stl·map·红黑树
csbysj202029 分钟前
Bootstrap5 按钮组
开发语言
kaikaile199530 分钟前
使用纯MATLAB M函数实现的无刷直流电机控制系统仿真
开发语言·matlab
崇山峻岭之间33 分钟前
Matlab学习记录09
开发语言·学习·matlab