Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
352571 王栎州 【C6-5】数塔的行走路径 C++ 解答错误 0 4 MS 260 KB 1896 2025-09-27 14:52:56

Tests(0/3):


#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int N; cin >> N; // 存储数塔,采用1-based索引 vector<vector<int>> tower(N + 1); for (int i = 1; i <= N; ++i) { tower[i].resize(i + 1); // 第i行有i个元素 for (int j = 1; j <= i; ++j) { cin >> tower[i][j]; } } // 贪心策略:从底层开始,每次选择上一层相邻的较大值 vector<pair<int, int>> path; int current_row = N; int current_col = 1; // 找到底层的最大值作为起点 for (int j = 2; j <= N; ++j) { if (tower[N][j] > tower[N][current_col]) { current_col = j; } } path.push_back({current_row, current_col}); // 向上层移动,每次选择相邻的较大值 while (current_row > 1) { int next_row = current_row - 1; // 上层相邻的两个可能位置 int left_col = current_col - 1; int right_col = current_col; // 选择较大的那个(注意边界判断) if (left_col < 1) { current_col = right_col; } else if (right_col > next_row) { current_col = left_col; } else { current_col = (tower[next_row][left_col] > tower[next_row][right_col]) ? left_col : right_col; } current_row = next_row; path.push_back({current_row, current_col}); } // 输出路径 for (int i = 0; i < path.size(); ++i) { if (i > 0) cout << "->"; cout << path[i].first << "," << path[i].second; } cout << endl; // 计算总和 int total = 0; for (auto &p : path) { total += tower[p.first][p.second]; } cout << total << endl; return 0; }


测评信息: