[笔试训练](三十四)100:[NOIP2008]ISBN号码101:kotori和迷宫102:矩阵最长递增路径

目录

100:[NOIP2008]ISBN号码

101:kotori和迷宫

102:矩阵最长递增路径


100:[NOIP2008]ISBN号码

题目链接:[NOIP2008]ISBN号码_牛客题霸_牛客网 (nowcoder.com)

题目:

题解:

简单模拟

cpp 复制代码
#include <iostream>
#include<string>
using namespace std;
string s;
int main() 
{
    cin>>s;
    int n=s.size(),sum=0,count=1;
    for(int i=0;i<n-1;i++)
    {
        if(s[i]>='0' && s[i]<='9')
        {
            sum+=(s[i]-'0')*count;
            count++;
        }
    }
    sum%=11;
    if(s[n-1]==sum+'0' || (s[n-1]=='X' && sum==10))
    {
        cout<<"Right"<<endl;
    }
    else
    {
        s[n-1]=sum==10?'X':sum+'0';
        cout<<s<<endl;
    }
    return 0;
}

101:kotori和迷宫

题目链接:kotori和迷宫 (nowcoder.com)

题目:

题解:

bfs迷宫问题

建一个大小与迷宫arr[N][N]相同大小的dist[N][N]用来存储走到该地点的步数,走不到的地方

为-1(可先初始化为-1)。

dist 数组(或指向的内存区域)中的所有字节都设置为 -1

cpp 复制代码
memset(dist, -1, sizeof dist);//全设置为-1
cpp 复制代码
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N=35;
int n,m;
int x1,y1;//标记起点位置
char arr[N][N];
int dist[N][N];//记录到达arr[i][j]位置的步数
queue<pair<int, int>> q;

int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

void bfs()
{
    memset(dist, -1, sizeof dist);//全设置为-1
    dist[x1][y1]=0;//起点
    q.push({x1,y1});//起点入队列
    while(q.size())
    {
        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>=1 && x<=n && y>=1 && y<=m && arr[x][y]!='*' && dist[x][y]==-1)
            {
                dist[x][y]=dist[a][b]+1;//步数+1
                if(arr[x][y]!='e')//可走的道路
                {
                    q.push({x,y});//入队列
                }     
            }
        }
    }  
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>arr[i][j];
            if(arr[i][j]=='k')
            {
                x1=i,y1=j;
            }
        }
    }
    bfs();//广度搜索
    //打扫战场
    int count=0,ret=0x3f3f3f3f;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(arr[i][j]=='e' && dist[i][j]!=-1)
            {
                count++;//统计出口个数
                ret=min(ret,dist[i][j]);//找最近的出口
            }
        }
    }
    if(count==0) cout<<-1<<endl;
    else cout<<count<<" "<<ret<<endl;
    return 0;
}

102:矩阵最长递增路径

题目链接:矩阵最长递增路径_牛客题霸_牛客网 (nowcoder.com)

题目:

题解:

记忆化搜索+递归

遍历原矩阵,以原矩阵每个元素作为起始点,找最长路径。

同时新建一个二维数组,保存好每个点作为起始点的最长路径的长度,避免后面再次经过这个点,减少时间复杂度

相关推荐
sali-tec1 天前
C# 基于halcon的视觉工作流-章66 四目匹配
开发语言·人工智能·数码相机·算法·计算机视觉·c#
小明说Java1 天前
常见排序算法的实现
数据结构·算法·排序算法
行云流水20191 天前
编程竞赛算法选择:理解时间复杂度提升解题效率
算法
smj2302_796826521 天前
解决leetcode第3768题.固定长度子数组中的最小逆序对数目
python·算法·leetcode
cynicme1 天前
力扣3531——统计被覆盖的建筑
算法·leetcode
core5121 天前
深度解析DeepSeek-R1中GRPO强化学习算法
人工智能·算法·机器学习·deepseek·grpo
mit6.8241 天前
计数if|
算法
a伊雪1 天前
c++ 引用参数
c++·算法
Data_agent1 天前
1688获得1688店铺列表API,python请求示例
开发语言·python·算法
2301_764441331 天前
使用python构建的应急物资代储博弈模型
开发语言·python·算法