代码随想录算法训练营第五十三天 | 图论part04

110. 字符串接龙

思路是要将字符串之间用线连起来,每个相邻的字符串只有一个字符不同。通过bfs来找到最短路径。要注意已经走过的路径要记录下来,包括走过的步数。

但是这一题并没有建图,而是将这个过程简化了,只是记录下了path。

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <queue>

using namespace std;

int main() {
    int n;
    string beginStr, endStr, str;
    unordered_set<string> strSet;
    unordered_map<string, int> visitMap;
    queue<string> que;
    
    cin >> n;
    cin >> beginStr >> endStr;
    while (n--) {
        cin >> str;
        strSet.insert(str);
    }
    
    que.push(beginStr);
    visitMap.insert({beginStr, 1});
    
    while (!que.empty()) {
        string word = que.front();
        que.pop();
        int path = visitMap[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 << endl;
                    return 0;
                }
                if (strSet.count(newWord) == 0) continue;
                if (visitMap.find(newWord) == visitMap.end()) {
                    visitMap.insert({newWord, path + 1});
                    que.push(newWord);
                }
            }
        }
    }
    cout << 0 << endl;
    return 0;
}

105. 有向图的完全可达性

cpp 复制代码
#include <iostream>
#include <vector>
#include <unordered_set>
#include <list>

using namespace std;

void dfs(const vector<list<int>> &graph, unordered_set<int> &visited, int node) {
    if (visited.count(node) != 0) return;
    
    visited.insert(node);
    for (int n : graph[node]) {
        dfs(graph, visited, n);
    }
}

int main() {
    int n, m, node1, node2;
    cin >> n >> m;
    
    vector<list<int>> graph(n + 1);
    while (m--) {
        cin >> node1 >> node2;
        graph[node1].push_back(node2);
    }
    
    unordered_set<int> visited;
    
    dfs(graph, visited, 1);
    
    if (visited.size() == n) cout << 1;
    else cout << -1;
    return 0;
}

06. 岛屿的周长

确实陷入惯性思维了

cpp 复制代码
#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 sum = 0, cover = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (grid[i][j] == 1) {
                sum++;
                if (i-1 >=0 && grid[i-1][j] == 1) cover++;
                if (j-1 >=0 && grid[i][j-1] == 1) cover++;
            }
        }
    }
    cout << sum*4-cover*2;
}
相关推荐
Kaltistss7 分钟前
98.验证二叉搜索树
算法·leetcode·职场和发展
知己如祭11 分钟前
图论基础(DFS、BFS、拓扑排序)
算法
mit6.82420 分钟前
[Cyclone] 哈希算法 | SIMD优化哈希计算 | 大数运算 (Int类)
算法·哈希算法
c++bug23 分钟前
动态规划VS记忆化搜索(2)
算法·动态规划
哪 吒25 分钟前
2025B卷 - 华为OD机试七日集训第5期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
军训猫猫头1 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
success1 小时前
【爆刷力扣-数组】二分查找 及 衍生题型
算法
Orlando cron2 小时前
数据结构入门:链表
数据结构·算法·链表
牛客企业服务3 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
糖葫芦君3 小时前
Policy Gradient【强化学习的数学原理】
算法