Advertisement
allia

конь

Nov 26th, 2020
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int horse_coin (int n, int m, int **chess)
  7. {
  8.   int **result = new int*[n];
  9.   int tek_sum;
  10.  
  11.   for (int i = 0; i < n; i++)
  12.      result[i] = new int[m];
  13.  
  14.   for (int i = 0; i < n; i++)
  15.    for (int j = 0; j < m; j++)
  16.     result[i][j] = 0;
  17.  
  18.   result[1][2] = chess[1][2] + chess[0][0];
  19.   result[2][1] = chess[2][1] + chess[0][0];
  20.  
  21.   for (int i = 2; i < n; i++)
  22.    for (int j = 2; j < m; j++)
  23.    {
  24.      if (result[i-2][j-1] >= result[i-1][j-2] && result[i-2][j-1] != 0)
  25.       result[i][j] = chess[i][j] + result[i-2][j-1];
  26.      else if (result[i-2][j-1] < result[i-1][j-2] )
  27.       result[i][j] = chess[i][j] + result[i-1][j-2];
  28.    }
  29.     return result[n-1][m-1];
  30. }
  31. int main()
  32. {
  33.   int n, m;
  34.   cin >> n >> m;
  35.  
  36.   int **chess = new int*[n];
  37.  
  38.   for (int i = 0; i < n; i++)
  39.     chess[i] = new int[m];
  40.  
  41.  
  42.   for (int i = 0; i<n; i++)
  43.    for ( int j = 0; j < m; j++)
  44.     cin >> chess[i][j];
  45.  
  46.    int znach = horse_coin (n, m, chess);
  47.    
  48.    if (znach == 0)
  49.     cout << "-";
  50.   else cout << znach;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement