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

```

相关推荐
平头哥AI13 分钟前
Day 01 | go run 跑通第一个 Go 程序,go build 留下一个能拷走的 exe
开发语言·后端·golang
葡萄城技术团队1 小时前
工业数据可视化:使用活字格 AIcoding + Three.js 构建轻量化三维设备监控大屏
开发语言·javascript·信息可视化
qiaosaifei1 小时前
JAVA老项目中有大量的类名_编译时报找不到符号错误
java·开发语言
聪明蛋子哟1 小时前
MCP协议深度落地:如何用Python/Java双语言实现统一的工具调用网关?
java·开发语言·python
一晌小贪欢1 小时前
python-第27天:Python面向对象详解
开发语言·python·数据可视化·面向对象·python办公
迪康Defender2 小时前
公用电脑责任追溯难?迪康端点安全一体化管理系统用户模式详解
java·运维·开发语言·网络·其他·安全
16月6日-晴2 小时前
Java面向对象——接口
java·开发语言
wuyk5553 小时前
107.FreeRTOS 链表深度解析:从原理到面试满分答案
c语言·开发语言·数据结构·stm32·单片机·链表·面试
geovindu3 小时前
CSharp: Wordcloud
开发语言·后端·c#·.net·.netcore·词云
Java小白笔记3 小时前
Java 实现 ZIP 压缩包生成方案
java·开发语言·网络·7-zip