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

```

相关推荐
唐青枫14 分钟前
C#.NET dapper 详解
c#·.net
爱装代码的小瓶子1 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
死也不注释3 小时前
【鸡零狗碎记录】
unity·c#
Maybe_ch3 小时前
.NET-键控服务依赖注入
开发语言·c#·.net
超浪的晨3 小时前
Java UDP 通信详解:从基础到实战,彻底掌握无连接网络编程
java·开发语言·后端·学习·个人开发
终焉暴龙王3 小时前
CTFHub web进阶 php Bypass disable_function通关攻略
开发语言·安全·web安全·php
Edingbrugh.南空4 小时前
Aerospike与Redis深度对比:从架构到性能的全方位解析
java·开发语言·spring
CodeCraft Studio4 小时前
借助Aspose.HTML控件,在 Python 中将 HTML 转换为 Markdown
开发语言·python·html·markdown·aspose·html转markdown·asposel.html
QQ_4376643144 小时前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
aramae4 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法