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

```

相关推荐
hez20102 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉8 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫9 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫10 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m62510 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户917215619021110 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠11 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫13 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech13 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf15 天前
C#摸鱼实录——IoC与DI案例详解
c#