day53 第十一章:图论part04

news/2025/2/21 13:36:40/

110.字符串接龙

bfs: 求最短路径长度

# bfs
from collections import deque
from collections import defaultdict
def bfs(beginStr, endStr, strDict):que = deque()que.append(beginStr)strDict[beginStr] = 1while que:curStr = que.popleft()curList = list(curStr)for i in range(len(curStr)):for j in range(26):newChar = chr(j + ord('a'))newStr = "".join(curList[:i]) + newChar + "".join(curList[i+1:])if newStr == endStr:return strDict[curStr] + 1if newStr in strDict and strDict[newStr] == 0:que.append(newStr)strDict[newStr] = strDict[curStr] + 1# print(f"{newStr} {strDict[newStr]}")return 0if __name__ == "__main__":# inputn = int(input())beginStr, endStr = input().split()strDict = defaultdict(int)for _ in range(n):strDict[input()] = 0result = bfs(beginStr, endStr, strDict)print(result)

105.有向图的完全可达性

dfs

#include <iostream>
#include <vector>
#include <list>
using namespace std;void dfs(vector<list<int>>& graph, int key, vector<bool>& visited) {//deal with this keyif (visited[key]) {return;}visited[key] = true;list<int> keys = graph[key];for (int key : keys) {dfs(graph, key, visited);}}int main()
{int n, k, s, t;cin >> n >> k;vector<list<int>> graph(n+1);while (k--) {cin >> s >> t;graph[s].push_back(t);}vector<bool> visited(n + 1, false);dfs(graph, 1, visited);for (int i = 1; i <= n; i++) {if (visited[i] == false) {cout << -1 << endl;return 0;}}cout << 1 << endl;
}
#include <iostream>
#include <vector>
#include <list>
#include <queue>
using namespace std;void bfs(vector<list<int>>& graph, int key, vector<bool>& visited) {queue<int> que;que.push(key);visited[key] = true;while (!que.empty()) {int cur = que.front();que.pop();list<int> keys = graph[cur];for (int key : keys) {if (!visited[key]) {//注意这里,不然会进入死循环que.push(key);visited[key] = true;}}}
}int main()
{int n, k, s, t;cin >> n >> k;vector<list<int>> graph(n+1);while (k--) {cin >> s >> t;graph[s].push_back(t);}vector<bool> visited(n + 1, false);bfs(graph, 1, visited);for (int i = 1; i <= n; i++) {if (visited[i] == false) {cout << -1 << endl;return 0;}}cout << 1 << endl;
}

106.岛屿的周长

dfs练手

#include <iostream>
#include <vector>
#include <list>
using namespace std;
int directions[4][2] = { 1,0,0,1,-1,0,0,-1 };
int result = 0;void dfs(vector<vector<int>>& graph, vector<vector<bool>>& visited, int x, int y) {if (visited[x][y] || graph[x][y] == 0) {return;}visited[x][y] = true;// get circlefor (int i = 0; i < 4; i++) {int next_x = x + directions[i][0];int next_y = y + directions[i][1];if (next_x < 0 || next_x >= graph.size()) {result++;// cout << result << endl;}if (next_y < 0 || next_y >= graph[0].size()) {result++;}if (next_x < 0 || next_x >= graph.size() || next_y < 0 || next_y >= graph[0].size()) {continue;}if (graph[next_x][next_y] == 0) {result++;//cout << result << endl;}dfs(graph, visited, next_x, next_y);}
}int main() {int n, m;cin >> n >> m;vector<vector<int>> graph(n, vector<int>(m, 0));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {cin >> graph[i][j];}}// int n = 5;// int m = 5;// vector<vector<int>> graph(n, vector<int>(n, 0));// graph = {//         {0,0,0,0,0},//         {0,1,0,1,0},//         {0,1,1,1,0},//         {0,1,1,1,0},//         {0,0,0,0,0}// };vector<vector<bool>> visited(n, vector<bool>(m, false));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (graph[i][j] == 1) {dfs(graph, visited, i, j);}}}cout << result << endl;return 0;}

也可以不用dfs

#include <iostream>
#include <vector>
#include <list>
using namespace std;
int directions[4][2] = { 1,0,0,1,-1,0,0,-1 };int main() {int n, m;cin >> n >> m;vector<vector<int>> graph(n, vector<int>(m, 0));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {cin >> graph[i][j];}}// int n = 5;// int m = 5;// vector<vector<int>> graph(n, vector<int>(m, 0));// graph = {//         {0,0,0,0,0},//         {0,1,0,1,0},//         {0,1,1,1,0},//         {0,1,1,1,0},//         {0,0,0,0,0}// };int result = 0;vector<vector<bool>> visited(n, vector<bool>(m, false));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {// cout << i << j << endl;if (graph[i][j] == 1) {for (int k = 0; k < 4; k++) {int next_x = i + directions[k][0];int next_y = j + directions[k][1];if (next_x < 0 || next_x >= n || next_y < 0 || next_y >= m || graph[next_x][next_y] == 0) {result++;}}}}}cout << result << endl;return 0;
}


http://www.ppmy.cn/news/1573539.html

相关文章

@Autowired注解(springboot)

Autowired 注解的说明 Autowired 是 Spring 框架中的一个重要注解&#xff0c;用于实现依赖注入&#xff08;Dependency Injection, DI&#xff09;。它可以让 Spring 容器自动将所需的依赖项注入到类中&#xff0c;而无需手动创建或查找这些依赖项。这有助于减少代码耦合&…

【Docker】容器被停止/删除的方式及命令:全面解析与实践指南

文章目录 引言一、容器的生命周期二、停止容器的命令及方式1. docker stop 命令2. docker kill 命令3. docker pause 和 docker unpause 命令4. docker restart 命令 三、删除容器的命令及方式1. docker rm 命令2. docker container prune 命令3. docker rm 与 docker rmi 的区…

NetCDF数据处理

NetCDF 文件格式在气象数据工程领域占据着举足轻重的地位&#xff0c;其结构灵活、强兼容性等优势使其成为该领域的一个标准。无论是从事学术研究还是工程实践&#xff0c;掌握这种数据格式变得越发重要。其次&#xff0c;我注意到气象编程大多数课程都聚焦于某个特定库的使用方…

基于SpringBoot+vue+uniapp的投票小程序+LW示例参考

系列文章目录 1.基于SSM的洗衣房管理系统原生微信小程序LW参考示例 2.基于SpringBoot的宠物摄影网站管理系统LW参考示例 3.基于SpringBootVue的企业人事管理系统LW参考示例 4.基于SSM的高校实验室管理系统LW参考示例 5.基于SpringBoot的二手数码回收系统原生微信小程序LW参考示…

单片机原理与运用

个人主页&#xff1a;java之路-CSDN博客(期待您的关注) 目录 一、走进单片机的世界 二、单片机是什么 &#xff08;一&#xff09;定义与本质 &#xff08;二&#xff09;与普通计算机的区别 三、单片机的工作原理深度剖析 &#xff08;一&#xff09;硬件组成及功能 &am…

filebeat抓取nginx日志

目录 一、抓取普通的应用输出日志到elasticsearch 二、抓取nginx日志输出到ElasticSearch 2.1、nginx.conf设定日志输出为JSON格式 2.2、nginx.conf设定日志按天输出文件 2.3、抓取Nginx JSON到ElasticSearch配置 一、抓取普通的应用输出日志到elasticsearch - type: log…

MySQL 面试系列:MySQL 事务的面试题总结

其它MySQL 面试系列&#xff1a; MySQL 面试系列&#xff1a;MySQL查询如何进行优化? MySQL 面试系列&#xff1a;一条select语句在MySQL是这样执行的? MySQL 面试系列&#xff1a;MySQL 常见的开放性问题 MySQL 面试系列&#xff1a;MySQL 性能优化 & 分布式 MySQL 面试…

DeepSeek、Kimi、文心一言、通义千问:AI 大语言模型的对比分析

在人工智能领域&#xff0c;DeepSeek、Kimi、文心一言和通义千问作为国内领先的 AI 大语言模型&#xff0c;各自展现出了独特的特点和优势。本文将从技术基础、应用场景、用户体验和价格与性价比等方面对这四个模型进行对比分析&#xff0c;帮助您更好地了解它们的特点和优势。…