代码随想录day53 110. 字符串接龙 105.有向图的完全可达性 106. 岛屿的周长

本文主要是介绍代码随想录day53 110. 字符串接龙 105.有向图的完全可达性 106. 岛屿的周长,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

代码随想录day53 110. 字符串接龙 105.有向图的完全可达性 106. 岛屿的周长

110. 字符串接龙

代码随想录

#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <queue>using namespace std;int main() {int N;cin >> N;string beginStr, endStr, str;cin >> beginStr >> endStr;unordered_set<string> strSet;for (int i = 0; i < N; i++) {cin >> str;strSet.insert(str);}unordered_map<string, int> pathMap;pathMap[beginStr] = 1;queue<string> que;que.push(beginStr);while (!que.empty()) {string word = que.front();que.pop();int path = pathMap[word];for (int i = 0; i < word.size(); i++) {string newWord = word;for (int j = 0; j < 26; j++) {newWord[i] = 'a' + j ;if (newWord == endStr) {cout << path + 1;return 0;}if (strSet.count(newWord) != 0 && pathMap.find(newWord) == pathMap.end()) {pathMap[newWord] = path + 1;que.push(newWord);}}}}cout << 0;return 0;
}

105.有向图的完全可达性

代码随想录

#include <vector>
#include <iostream>
#include <list>
using namespace std;void dfs(vector<list<int>> &graph, vector<bool> &visted, int begin) {visted[begin] = true;list<int> destList = graph[begin];for (int ele : destList) {if (!visted[ele]) {dfs(graph, visted, ele);}}
}int main() {int N, K, s, t;cin >> N >> K;vector<list<int>> graph(N + 1, list<int>());for (int i = 0; i < K; i++) {cin >> s >> t;graph[s].push_back(t);}vector<bool> visted(N + 1, false);dfs(graph, visted, 1);for (int i = 1; i <= N; i++) {if (!visted[i]) {cout << -1;return 0;}}cout << 1;return 0;
}

106. 岛屿的周长

代码随想录

#include <vector>
#include <iostream>using namespace std;int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};int dfs(vector<vector<int>> &graph, vector<vector<bool>> &visted, int row, int col) {visted[row][col] = true;int count = 0;for (int i = 0; i < 4; i++) {int nextRow = row + dir[i][0];int nextCol = col + dir[i][1];if (nextRow < 0 || nextRow >= graph.size() || nextCol < 0 || nextCol >= graph[0].size() || graph[nextRow][nextCol] == 0) {count++;continue;}if (!visted[nextRow][nextCol]) {count += dfs(graph, visted, nextRow, nextCol);}}return count;
}int main() {int N, M;cin >> N >> M;vector<vector<int>> graph(N, vector<int>(M));vector<vector<bool>> visted(N, vector<bool>(M, false));for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {cin >> graph[i][j];}}for (int i = 0; i < N; i++) {for (int j = 0; j < M; j++) {if (graph[i][j] == 1) {cout << dfs(graph, visted, i, j);return 0;}}}return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main() {int n, m;cin >> n >> m;vector<vector<int>> grid(n, vector<int>(m, 0));for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {cin >> grid[i][j];}}int direction[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};int result = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (grid[i][j] == 1) {for (int k = 0; k < 4; k++) {       // 上下左右四个方向int x = i + direction[k][0];int y = j + direction[k][1];    // 计算周边坐标x,yif (x < 0                       // x在边界上|| x >= grid.size()     // x在边界上|| y < 0                // y在边界上|| y >= grid[0].size()  // y在边界上|| grid[x][y] == 0) {   // x,y位置是水域result++;}}}}}cout << result << endl;}

这篇关于代码随想录day53 110. 字符串接龙 105.有向图的完全可达性 106. 岛屿的周长的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1103895

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代