【今日三题】ISBN号码(模拟) / kotori和迷宫(BFS最短路) / 矩阵最长递增路径(dfs)

⭐️个人主页:@小羊 ⭐️所属专栏:每日两三题 很荣幸您能阅读我的文章,诚请评论指点,欢迎欢迎 ~

目录


ISBN号码(模拟)

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

string s;
int sum, cnt = 1, n;

int main()
{
    cin >> s;
    n = s.size();
    for (int i = 0; i < n - 1; i++)
    {
        if (s[i] != '-')
        {
            sum += (s[i] - '0') * cnt;
            cnt++;
        }
    }
    sum %= 11;
    if (sum == (s[n - 1] - '0') || (sum == 10 && s[n - 1] == 'X'))
    {
        cout << "Right" << endl;
    }
    else {
        s[n - 1] = (sum == 10 ? 'X' : (sum + '0'));
        cout << s << endl;
    }
    return 0;
}

kotori和迷宫(BFS最短路)

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

int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
const int N = 31;
bool used[N][N];
char arr[N][N];
using pii = pair<int, int>;
queue<pii> q;
int n, m, cnt, dist, step;

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> arr[i][j];
            if (arr[i][j] == 'k')
            {
                q.push({i, j});
                used[i][j] = true;
            }
        }
    }
    while (q.size())
    {
        step++;
        int sz = q.size();
        while (sz--)
        {
            auto [a, b] = q.front();
            q.pop();
            for (int i = 0; i < 4; i++)
            {
                int x = a + dx[i], y = b + dy[i];
                if (x >= 0 && x < n && y >= 0 && y < m && !used[x][y] && arr[x][y] != '*')
                {
                    if (arr[x][y] == 'e')
                    {
                        if (cnt == 0) dist = step;
                        cnt++;
                    }
                    else if (arr[x][y] == '.')
                    {
                        q.push({x, y});
                    }
                    used[x][y] = true;
                }
            }
        }
    }
    if (cnt > 0) cout << cnt << " " << dist << endl;
    else cout << -1 << endl;
    return 0;
}

矩阵最长递增路径(dfs)

cpp 复制代码
class Solution {
    int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
    bool used[1001][1001] = {};
    int n, m;
public:
    int solve(vector<vector<int> >& matrix) {
        n = matrix.size(), m = matrix[0].size();
        int len = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                len = max(len, dfs(matrix, i, j));
            }
        }
        return len;
    }
    int dfs(const vector<vector<int>> &matrix, int i, int j)
    {
        int len = 1;
        for (int k = 0; k < 4; k++)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < n && y >= 0 && y < m && !used[x][y] && matrix[x][y] > matrix[i][j])
            {
                used[x][y] = true;
                len = max(len, dfs(matrix, x, y) + 1);
                used[x][y] = false;
            }
        }
        return len;
    }
};

本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~

相关推荐
Keying,,,,10 小时前
力扣hot100 | 矩阵 | 73. 矩阵置零、54. 螺旋矩阵、48. 旋转图像、240. 搜索二维矩阵 II
python·算法·leetcode·矩阵
花开富贵ii13 小时前
代码随想录算法训练营四十三天|图论part01
java·数据结构·算法·深度优先·图论
易木木木响叮当1 天前
有限元方法中的数值技术:行列式、求逆、矩阵方程
线性代数·矩阵
亮亮爱刷题2 天前
算法提升之树上问题-(LCA)
数据结构·算法·leetcode·深度优先
东方佑2 天前
UniVoc:基于二维矩阵映射的多语言词汇表系统
人工智能·算法·矩阵
KarrySmile2 天前
Day53--图论--106. 岛屿的周长(卡码网),110. 字符串接龙(卡码网),105. 有向图的完全联通(卡码网)
深度优先·图论·广度优先·广搜·岛屿的周长·字符串接龙·有向图的完全联通
火车叨位去19493 天前
力扣top100(day01-05)--矩阵
算法·leetcode·矩阵
厦门辰迈智慧科技有限公司3 天前
现代化水库运行管理矩阵建设的要点
运维·网络·物联网·线性代数·安全·矩阵·监测
文弱_书生4 天前
为什么神经网络的权重矩阵具有低秩特性?如何理解和解释?
人工智能·神经网络·矩阵
岁忧6 天前
(LeetCode 面试经典 150 题) 104. 二叉树的最大深度 (深度优先搜索dfs)
java·c++·leetcode·面试·go·深度优先