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;
}
相关推荐
实心儿儿2 小时前
C++ —— 模板进阶
开发语言·c++
WWZZ20252 小时前
快速上手大模型:深度学习10(卷积神经网络2、模型训练实践、批量归一化)
人工智能·深度学习·神经网络·算法·机器人·大模型·具身智能
go_bai3 小时前
Linux-线程2
linux·c++·经验分享·笔记·学习方法
sali-tec3 小时前
C# 基于halcon的视觉工作流-章62 点云采样
开发语言·图像处理·人工智能·算法·计算机视觉
fashion 道格3 小时前
用 C 语言玩转归并排序:递归实现的深度解析
数据结构·算法·排序算法
j_xxx404_3 小时前
C++:继承(概念及定义|作用域|基类与派生类转换|默认成员函数|与友元、静态成员关系|多继承|组合)
数据结构·c++
九年义务漏网鲨鱼4 小时前
蓝桥杯算法——状态压缩DP
算法·职场和发展·蓝桥杯
欧阳x天4 小时前
C++入门(二)
开发语言·c++
CappuccinoRose4 小时前
MATLAB学习文档(二十八)
开发语言·学习·算法·matlab
Freedom_my5 小时前
插入排序算法
数据结构·算法·排序算法