[笔试训练](三十四)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)

题目:

题解:

记忆化搜索+递归

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

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

相关推荐
前端炒粉3 小时前
18.矩阵置零(原地算法)
javascript·线性代数·算法·矩阵
暴风鱼划水3 小时前
三维重建【0-D】3D Gaussian Splatting:相机标定原理与步骤
算法·3d
mount_myj6 小时前
敏感信息屏蔽(一)【java】
java·算法·极课堂
先做个垃圾出来………6 小时前
偏移量解释
数据结构·算法
FanXing_zl6 小时前
基于整数MCU的FOC控制定标策略深度解析
单片机·嵌入式硬件·mcu·算法·定点运算·q15
立志成为大牛的小牛7 小时前
数据结构——三十三、Dijkstra算法(王道408)
数据结构·笔记·学习·考研·算法·图论
地平线开发者8 小时前
mul 与 reduce_sum 的优化实例
算法·自动驾驶
坚持编程的菜鸟8 小时前
LeetCode每日一题——Pow(x, n)
c语言·算法·leetcode
csdn_aspnet8 小时前
分享MATLAB在数据分析与科学计算中的高效算法案例
算法·matlab·数据分析