11 • 1年前
#include <iostream>
#include <vector>
using namespace std;
int maxApples = 0; // 存储能够摘到的最多苹果数
void dfs(const vector<int>& appleHeights, int maxReachWithStool, int index, int count) {
    // 如果遍历到最后一个苹果,更新最大苹果数
    if (index == 10) {
        if (count > maxApples) {
            maxApples = count;
        }
        return;
    }
    // 不摘取当前苹果,继续搜索
    dfs(appleHeights, maxReachWithStool, index + 1, count);
    // 摘取当前苹果,继续搜索
    if (appleHeights[index] <= maxReachWithStool) {
        dfs(appleHeights, maxReachWithStool, index + 1, count + 1);
    }
}
int main() {
    vector<int> appleHeights(10);
    int maxReachHeight;
    // 读取10个苹果的高度
    for (int i = 0; i < 10; i++) {
        cin >> appleHeights[i];
    }
    // 读取陶陶的伸手高度
    cin >> maxReachHeight;
    // 计算能够达到的最大高度
    int maxReachWithStool = maxReachHeight + 30;
    // 进行DFS搜索
    dfs(appleHeights, maxReachWithStool, 0, 0);
    // 输出最终结果
    cout << maxApples << endl;
    return 0;
}
评论:
请先登录,才能进行评论