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;
}
相关推荐
yaoh.wang7 小时前
力扣(LeetCode) 13: 罗马数字转整数 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
T1ssy7 小时前
布隆过滤器:用概率换空间的奇妙数据结构
算法·哈希算法
hetao17338377 小时前
2025-12-12~14 hetao1733837的刷题笔记
数据结构·c++·笔记·算法
椰子今天很可爱8 小时前
五种I/O模型与多路转接
linux·c语言·c++
程序员zgh8 小时前
C++ 互斥锁、读写锁、原子操作、条件变量
c语言·开发语言·jvm·c++
鲨莎分不晴8 小时前
强化学习第五课 —— A2C & A3C:并行化是如何杀死经验回放
网络·算法·机器学习
搞科研的小刘选手9 小时前
【ISSN/ISBN双刊号】第三届电力电子与人工智能国际学术会议(PEAI 2026)
图像处理·人工智能·算法·电力电子·学术会议
拉姆哥的小屋9 小时前
从混沌到秩序:条件扩散模型在图像转换中的哲学与技术革命
人工智能·算法·机器学习
Sammyyyyy9 小时前
DeepSeek v3.2 正式发布,对标 GPT-5
开发语言·人工智能·gpt·算法·servbay
sin_hielo10 小时前
leetcode 2110
数据结构·算法·leetcode