#include <iostream>
#include <vector>
using namespace std;
int data[20][20], x, y, minValue = -1;
void calc(int px, int py, int value){
value += data[px][py];
int temp = data[px][py];
if(px == x - 1 && py == y - 1) {
if(value < minValue || minValue == -1)
minValue = value;
return;
}
data[px][py] = -1;
if(px + 1 < x && data[px + 1][py] != -1) calc(px + 1, py, value);
if(py + 1 < y && data[px][py + 1] != -1) calc(px, py + 1, value);
data[px][py] = temp;
int main(){
cin >> x >> y;
for (int i = 0; i < x; ++i)
for (int j = 0; j < y; ++j)
cin >> data[i][j];
calc(0, 0, 0);
cout << minValue;
return 0;
Объяснение:
#include <algorithm>
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;
s.push_back(arr[i][j]);
s.push_back(' ');
if(arr[i][j] == 'R'){
j -= 1;
continue;
if(arr[i][j] == 'D'){
i -= 1;
cout << endl;
#include <iostream>
#include <vector>
using namespace std;
int data[20][20], x, y, minValue = -1;
void calc(int px, int py, int value){
value += data[px][py];
int temp = data[px][py];
if(px == x - 1 && py == y - 1) {
if(value < minValue || minValue == -1)
minValue = value;
return;
}
data[px][py] = -1;
if(px + 1 < x && data[px + 1][py] != -1) calc(px + 1, py, value);
if(py + 1 < y && data[px][py + 1] != -1) calc(px, py + 1, value);
data[px][py] = temp;
}
int main(){
cin >> x >> y;
for (int i = 0; i < x; ++i)
for (int j = 0; j < y; ++j)
cin >> data[i][j];
calc(0, 0, 0);
cout << minValue;
return 0;
}
Объяснение:
#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;
}
Объяснение: