C++ 蓝桥云课代码练习

代码一 ,小明的背包1,代码见下

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

#define maxn 110
#define maxm 1001
#define inf -1

int w[maxn], v[maxn];
int dp[maxn][maxm];


int main()
{
  memset(dp, inf, sizeof(dp));
  dp[0][0] = 0;
  int n, V;
  cin >> n >> V;
  for(int i=1; i<=n; ++i){
    cin >> w[i] >> v[i];
  }
  for(int i=1; i<=n; ++i){
    for(int j=0; j<=V; ++j){
      dp[i][j] = dp[i-1][j];
      if(j - w[i] >= 0){
        int x=dp[i-1][j-w[i]] + v[i];
        if(x > dp[i][j]){
          dp[i][j] = x;
        }
      }
    }
  }
  int x = 0;
  for(int i=0; i<=V; ++i){
    x = max(x, dp[n][i]);
  }
  cout << x << endl;
  // 请在此输入您的代码
  return 0;
}

代码二,对应蓝桥云课,小明的彩灯,代码见下

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

#define maxn 500005
long long a[maxn];
long long s[maxn];

int main()
{
  int n, q;
  cin >> n >> q;
  for(int i=1; i<=n; ++i){
    cin >> a[i];
  }
  s[0] = 0;
  while(q--){
    int l, r, x;
    cin >> l >> r >> x;
    s[l] += x;
    s[r+1] -= x;
  }
  for(int i=1; i<=n; ++i){
    s[i] = s[i] + s[i-1];
    a[i] = a[i] + s[i];
    if(i != 1){
      cout << ' ';
    }
    if(a[i] < 0){
      cout << 0;
    }else{
      cout << a[i];
    }
  }
  cout << endl;
  // 请在此输入您的代码
  return 0;
}

代码三,对应蓝桥云课 走迷宫,代码见下

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

#define maxn 110
#define base 101

int mat[maxn][maxn];

struct p{
  int x, y;
};

int dir[4][2] = {
  {0, 1}, {1, 0}, {0, -1}, {-1, 0}
};

int main()
{
  int hash[maxn][maxn];
  memset(hash, -1, sizeof(hash));
  int n,m;
  queue<int> q;
  cin >> n >> m;
  for(int i=1; i<=n; ++i){
    for(int j=1; j<=m; ++j){
      cin >> mat[i][j];
    }
  }
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  q.push(x1*base + y1);
  hash[x1][y1] = 0;
  while(!q.empty()){
    int v = q.front();
    q.pop();
    int x = v / base;
    int y = v % base;
    if(x == x2 && y == y2){
      break;
    }
    for(int i=0; i<4; ++i){
      int tx = x + dir[i][0];
      int ty = y + dir[i][1];
      if(tx < 1 || tx > n || ty < 1 || ty > m){
        continue;
      }
      if(mat[tx][ty] == 0){
        continue;
      }
      if(hash[tx][ty] == -1){
        hash[tx][ty] = hash[x][y] + 1;
        q.push(tx*base + ty);
      }
    }

  }
  cout << hash[x2][y2] << endl;
  // 请在此输入您的代码
  return 0;
}
相关推荐
大鱼>4 分钟前
AI+快递分拣:视觉识别+自动分拣+异常检测
人工智能·深度学习·算法·机器学习
想要成为糕糕手6 分钟前
238. 除了自身以外数组的乘积 — 面试向深度解析
javascript·算法·面试
2601_9498163513 分钟前
C++内存对齐与结构体填充深度精讲:底层规则、内存裁剪、适配优化与工程避坑
开发语言·arm开发·c++
浩瀚地学18 分钟前
【面试算法笔记】0105-数组-螺旋矩阵
java·开发语言·笔记·算法·面试
王老师青少年编程37 分钟前
2026年全国青少年信息素养大赛复赛真题(算法应用主题赛C++初中组:带解析)(7月11日试卷一)【文末附答案和解析】
c++·真题·答案·复赛·2026年·青少年信息素养大赛·算法应用主题赛
Hesionberger1 小时前
动态规划与二分法破解最长递增子序列
java·数据结构·python·算法·leetcode
imuliuliang1 小时前
关于Kruskal 算法在图优化问题中的扩展应用7
算法
大阿明1 小时前
C++智能指针与RAII机制精讲:彻底根治内存泄漏与野指针
java·jvm·c++
一生了无挂1 小时前
C++面向对象核心精讲:类、对象、封装、权限与生命周期全解析
java·jvm·c++
aaPIXa6221 小时前
C++ 用 Claude Code 的 defending-code-reference-harness:C/C++ 内存漏洞自动发现与修复 Pipeline
java·c语言·c++