#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;
}
Объяснение: