Advertisement
Josif_tepe

Untitled

Dec 8th, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int dp[100][100];
  5. int matrica[100][100];
  6. int x, y;
  7. pair<int, int> path[100][100];
  8. int rec(int i, int j){
  9.     if((i==x-1)and(j==y-1)){
  10.         path[i][j] = make_pair(-1, -1);
  11.         return(matrica[i][j]);
  12.     }
  13.  
  14.     if(dp[i][j]!=-1){
  15.         return(dp[i][j]);
  16.     }
  17.  
  18.     int result=0;
  19.     int down = 0, right = 0;
  20.     if(i+1<x){
  21.        down  = rec(i+1, j)+matrica[i][j];
  22.     }
  23.     if(j+1<y){
  24.         right = rec(i, j+1)+matrica[i][j];
  25.     }
  26.     result = max(down, right);
  27.     if(down > right) {
  28.         path[i][j] = make_pair(i + 1, j);
  29.     }
  30.     else {
  31.         path[i][j] = make_pair(i, j + 1);
  32.     }
  33.     dp[i][j]=result;
  34.     return(result);
  35. }
  36. int main()
  37. {
  38.  
  39. cin>>x>>y;
  40. for(int i=0; i<x; i++){
  41.     for(int j=0; j<y; j++){
  42.         cin>>matrica[i][j];
  43.     }
  44. }
  45.  
  46. for(int i=0; i<x; i++){
  47.     for(int j=0; j<y; j++){
  48.         dp[i][j]=-1;
  49.     }
  50. }
  51.     cout<<rec(0, 0) << endl;
  52.     for(pair<int, int> i = make_pair(0, 0); i != make_pair(-1, -1); i = make_pair(path[i.first][i.second].first, path[i.first][i.second].second)) {
  53.         cout << i.first + 1 << " " << i.second + 1 << endl;
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement