Код:
#include <iostream>
#include <string>
using namespace std;
void printArray(int** arr, size_t X, size_t Y) {
for (size_t i = 0; i < X; ++i) {
for (size_t j = 0; j < Y; ++j) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main() {
size_t X, Y;
cout << "Number of rows in the array: ";
cin >> X;
cout << "Elements in each row of the array: ";
cin >> Y;
int** arr = new int* [X];
for (size_t i = 0; i < X; ++i) {
arr[i] = new int[Y];
cout << "#" << i + 1 << ": ";
for (size_t j = 0; j < Y; ++j)
cin >> arr[i][j];
}
size_t index;
cout << "index to check the row for non-decreasing ordering: ";
cin >> index;
--index; // numbering from 1
bool flag = 1;
for (int i = 0; i < Y - 1; ++i) {
if (!(arr[index][i] <= arr[index][i + 1])) {
cout << "No, " << i + 1 << (i + 1 == 1 ? "st" : (i + 1 == 2 ? "nd" : (i + 1 == 3) ? "rd" : "th")) << " element (" << arr[index][i] << ") violates the non-decreasing ordering (" << arr[index][i] << " > " << arr[index][i + 1] << ").\n";
flag = 0;
break;
}
}
if (flag)
cout << "Yes, the specified row is ordered in non-decreasing order.\n";
flag = 1;
index = 1;
cout << "index to check the column for non-increasing ordering: ";
cin >> index;
--index;
flag = 1;
for (int i = 0; i < X - 1; ++i) {
if (!(arr[i][index] >= arr[i + 1][index])) {
cout << "No, " << i + 1 << (i + 1 == 1 ? "st" : (i + 1 == 2 ? "nd" : (i + 1 == 3) ? "rd" : "th")) << " element (" << arr[i][index] << ") violates the non-increasing ordering (" << arr[i][index] << " < " << arr[i][index + 1] << ").\n";
flag = 0;
break;
}
}
if (flag)
cout << "Yes, the specified row is ordered in non-increasing order.\n";
}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n(0),m(0);
cin >> n >> m;
int matrix[n][m];
char arr[n][m];
arr[0][0] = '0';
for(int i = 0;i<n;++i){
for(int j = 0;j<m;++j){
cin >> matrix[i][j];
if(!i && !j)continue;
if(!i){
matrix[i][j] += matrix[i][j-1];
arr[i][j] = 'R';
}
if(!j){
matrix[i][j] += matrix[i-1][j];
arr[i][j] = 'D';
}
if(i && j){
matrix[i][j] += max(matrix[i-1][j],matrix[i][j-1]);
if(max(matrix[i-1][j],matrix[i][j-1]) == matrix[i-1][j])arr[i][j] = 'D';
else arr[i][j] = 'R';
}
}
}
cout << matrix[n-1][m-1];
cout << "\n";
string s;
for(int i = n-1;;){
for(int j = m-1;;){
if(arr[i][j] == '0'){
reverse(s.begin(),s.end());
s.erase(0,1);
cout << s << endl;
return 0;
}
s.push_back(arr[i][j]);
s.push_back(' ');
if(arr[i][j] == 'R'){
j -= 1;
continue;
}
if(arr[i][j] == 'D'){
i -= 1;
continue;
}
}
}
cout << endl;
return 0;
}
Объяснение: