【Day46 LeetCode】图论问题 Ⅳ

news/2025/2/23 23:32:32

一、图论问题 Ⅳ

1、字符串接龙

采用BFS,代码如下:(判断是否在字典中需要遍历每个位置,同时遍历26中可能有点不优雅)

# include<iostream>
# include<string>
# include<vector>
# include<unordered_set>
# include<unordered_map>
# include<queue>

using namespace std;

int main(){
    
    int n;
    cin >> n;
    string src, des, s;
    cin >> src >> des;
    unordered_set<string> dic;
    for(int i=0; i<n; ++i){
        cin >> s;
        dic.insert(s);
    }
    
    queue<string> q;
    q.push(src);
    unordered_map<string, int> mp;
    mp[src] = 1;
    while(!q.empty()){
        string s = q.front(); q.pop();
        int cnt = mp[s];
        // BFS
        for(int i=0; i<s.size(); ++i){
            string ss = s;
            for(int j=0; j<26; ++j){
                ss[i] = 'a' + j;
                if(ss == des){
                    cout << cnt + 1 << endl;
                    return 0;
                }
                if(dic.find(ss) != dic.end() && mp.find(ss)==mp.end()){
                    mp[ss] = cnt+1;
                    q.push(ss);
                }
            }
        }
    }
    cout << 0 << endl;
    return 0;
}

2、有向图的完全可达性

这题和找可达路径的那题不同,因为这题可能存在环,不处理会陷入死循环。所以我们需要标记节点,看是否是已经访问过的,刚好可以复用本来就得用来求解节点是否可达的标记数组。本题需要判断 1节点 是否能到所有节点,那么就没有必要回溯去撤销操作了,只要将遍历过的节点都标记上。而搜索路径就需要回溯,不然路径就会混叠在一起。
DFS代码如下:

# include<iostream>
# include<vector>
# include<list>

using namespace std;

void dfs(vector<list<int>> &graph, vector<bool> &visited, int node){
    for(auto nxt : graph[node]){
        if(visited[nxt]==false){
            visited[nxt] = true;
            dfs(graph, visited, nxt);
        }
    }
}

int main(){
    int n, k;
    cin >> n >> k;
    vector<list<int>> graph(n+1);
    while(k--){
        int s, t;
        cin >> s >> t;
        graph[s].push_back(t);
    }
    vector<bool> visited(n+1, false);
    visited[1] = true;
    dfs(graph, visited, 1);
    for(int i=1; i<=n; ++i){
        if(visited[i]==false){
            cout << -1 << endl;
            return 0;
        }
    }
    cout << 1 << endl;
    return 0;
}

BFS版本的代码如下:

# include<iostream>
# include<vector>
# include<list>
# include<queue>

using namespace std;

void bfs(vector<list<int>> &graph, vector<bool> &visited, int node){
    queue<int> q;
    q.push(node);
    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(auto nxt : graph[cur]){
            if(visited[nxt]==false){
                visited[nxt] = true;
                q.push(nxt);
            }
        }
    }
}

int main(){
    int n, k;
    cin >> n >> k;
    vector<list<int>> graph(n+1);
    while(k--){
        int s, t;
        cin >> s >> t;
        graph[s].push_back(t);
    }
    vector<bool> visited(n+1, false);
    visited[1] = true;
    bfs(graph, visited, 1);
    for(int i=1; i<=n; ++i){
        if(visited[i]==false){
            cout << -1 << endl;
            return 0;
        }
    }
    cout << 1 << endl;
    return 0;
}

3、岛屿周长

周长是由边组成的,什么是能构成周长的边?陆地与水域接壤的部分。所以可以通过查看陆地是否存在水域(超出边界也是水域),每存在一个水域+1,表示一条有效的边,能给周长+1。代码如下:

# include<iostream>
# include<vector>

using namespace std;

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(m));
    for(int i=0; i<n; ++i)
        for(int j=0; j<m; ++j)
            cin >> graph[i][j];
    
    int ans = 0;
    vector<vector<int>> dirs = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
    for(int i=0; i<n; ++i){
        for(int j=0; j<m; ++j){
            if(graph[i][j]==1){
                for(auto xy : dirs){
                    int x = i + xy[0], y = j + xy[1];
                    if(x<0 || x>=n || y<0 || y>=m || graph[x][y]==0)
                        ++ans;
                }   
            }
        }
    }

    cout << ans << endl;
    return 0;
}

http://www.niftyadmin.cn/n/5863865.html

相关文章

HDFS Java 客户端 API

一、基本调用 Configuration 配置对象类&#xff0c;用于加载或设置参数属性 FileSystem 文件系统对象基类。针对不同文件系统有不同具体实现。该类封装了文件系统的相关操作方法。 1. maven依赖pom.xml文件 <dependency><groupId>org.apache.hadoop</groupId&g…

编译部署使用腾讯云cpp-cos-sdk

创建对象存储 在腾讯云上创建对象存储,再创建一个子用户,对他进行授权; 点击我们创建的子账户,在其中新建一个密钥 创建存储桶 然后到控制台上创建一个存储桶用于测试 编译sdk 我们需要去windows上编译sdk源码,在对象存储的控制台中常用工具中有sdk下载:里面有一个c++…

【C】队列与栈的相互转换

栈与队列是两种特点相反的数据结构&#xff0c;一个特点是后进先出&#xff0c;一个特点是先进先出&#xff0c;但是他们之间是可以相互转换的。 目录 1 用队列实现栈 1&#xff09; 题目解析 2&#xff09; 算法解析 &#xff08;1&#xff09; 结构(MyStack) &#xff…

SQLMesh 系列教程8- 详解 seed 模型

在数据分析和建模过程中&#xff0c;外部模型&#xff08;External Models&#xff09;在 SQLMesh 中扮演着重要角色。外部模型允许用户引用外部数据源或现有数据库表&#xff0c;从而实现灵活的数据整合和分析。本文将介绍外部模型的定义、生成方法&#xff08;包括使用 CLI 和…

线性模型 - Softmax 回归(参数学习)

本文&#xff0c;我们来学习Softmax 回归的参数学习&#xff0c;在开始之前&#xff0c;我们先了解一下“损失函数”、“风险函数”和“目标函数”这三个核心概念。 一、损失函数、风险函数、目标函数 1. 损失函数&#xff08;Loss Function&#xff09; 定义&#xff1a; 损…

Linux 内核中的 container_of 宏:以 ipoib_rx_poll_rss 函数为例

在 Linux 内核编程中,container_of 是一个非常实用的宏,主要用于通过结构体的成员指针来获取包含该成员的整个结构体的指针。rx_ring = container_of(napi, struct ipoib_recv_ring, napi); 在代码中就是利用了这个宏,下面我们详细分析它的作用和工作原理。 背景知识 在内…

断开ssh连接程序继续运行

在使用 SSH 远程连接服务器时&#xff0c;我们常希望在断开连接后仍然让程序继续运行&#xff0c;以下是几种常见的方法&#xff1a; 1. 使用 screen 或 tmux screen 和 tmux 是两款非常强大的终端复用工具&#xff0c;它们允许你在后台运行会话&#xff0c;即使断开 SSH 连接…

【Python爬虫(45)】Python爬虫新境界:分布式与大数据框架的融合之旅

【Python爬虫】专栏简介&#xff1a;本专栏是 Python 爬虫领域的集大成之作&#xff0c;共 100 章节。从 Python 基础语法、爬虫入门知识讲起&#xff0c;深入探讨反爬虫、多线程、分布式等进阶技术。以大量实例为支撑&#xff0c;覆盖网页、图片、音频等各类数据爬取&#xff…