代码随想录算法训练营Day-53 图论01 | 110.字符串接龙、105.有向图的完全可达性、106.岛屿的周长

110.字符串接龙

cpp 复制代码
#include<iostream>
using namespace std;
#include<vector>
#include<unordered_map>
#include<unordered_set>
#include<queue>
#include<string>

int main(){
    int n;
    cin>>n;

    string beginStr,endStr;
    cin>>beginStr>>endStr;

    unordered_map<string, int> strmap;//用来记录起点到该字符串最短路径,并用来判断某字符串是否被访问过,如果map中找不到对应的str就说明没访问过
    unordered_set<string> strset;//用来存放若干个需要出现过的字符串

    //把六个字符串加入集合
    for(int i=0;i<n;i++){
        string appearStr;
        cin>>appearStr;
        strset.insert(appearStr);
    }

    queue<string> que;//用来把"待寻找相邻节点的字符串"入队 
    //把起点加入队列
    que.push(beginStr);
    //同时要更新strmap,标记起点作为key的话,最短路径为1
    strmap[beginStr] = 1;
    
    //开始循环入队直到队空(需要注意,访问过的节点不能重复访问,否则会死循环,因为访问节点之前一定要判断节点是否访问过),执行广搜
    while(!que.empty()){
        string curStr = que.front();que.pop();
        for(int i=0;i<curStr.size();i++){
            string newStr = curStr;//以原字符串为基础定义新字符串,新字符串是原字符串用26个字母中某个字母替代某个字母位得到的
            for(int j=0;j<26;j++){
                newStr[i] = j+'a';//把第i个字母替换为了a后面第j个字母
                if(newStr == endStr){
                    cout<<strmap[curStr]+1<<endl;
                    return 0;
                }
                if(strset.find(newStr)!=strset.end() && strmap.find(newStr) == strmap.end()){
                    que.push(newStr);
                    strmap[newStr] = strmap[curStr]+1;
                }
            }
        }
    }
    cout<<0<<endl;
}

105.有向图的完全可达性

cpp 复制代码
#include<iostream>
#include<list>
#include<vector>
using namespace std;

void dfs(vector<list<int>>& graph, int node, vector<bool>& visited){
    if(visited[node] == true) return;
    visited[node] =true;
    list<int> nodes = graph[node];
    for(int newnode:nodes){
        dfs(graph, newnode, 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;

}

106.岛屿的周长

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 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,y
                    if (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;

}

相关推荐
IronMurphy3 小时前
【算法四十六】300. 最长递增子序列
算法
碧海银沙音频科技研究院3 小时前
高通QCC3084-QCC518X蓝牙耳机项目
人工智能·深度学习·算法
兩尛3 小时前
compare_exchange_weak 的用法
算法
数智工坊3 小时前
面向具身操作的视觉-语言-动作模型:让机器人真正理解并执行人类指令
论文阅读·人工智能·算法·机器人
代码不停3 小时前
记忆化搜索题目练习
java·算法
闻缺陷则喜何志丹3 小时前
【C++动态规划】B3734 [信息与未来 2017] 加强版密码锁|普及+
c++·算法·动态规划·洛谷
是娇娇公主~3 小时前
力扣——105. 从前序与中序遍历序列构造二叉树详解
算法·leetcode·哈希算法
承渊政道3 小时前
【贪心算法】(经典实战应用解析(三):K次取反后最⼤化的数组和、按⾝⾼排序、优势洗牌、最⻓回⽂串、增减字符串匹配)
数据结构·c++·学习·算法·贪心算法·线性回归·哈希算法
凌波粒3 小时前
LeetCode--100.相同的树(二叉树)
算法·leetcode·职场和发展