Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. struct cur
  8. {
  9.     bool f;
  10.     int max = 0;
  11.     int val;
  12.  
  13. };
  14.  
  15. void curr(cur* a, cur* b)
  16. {
  17.     if (a->f == true)
  18.     {
  19.         if (b->val > a->max)
  20.         {
  21.             a->val = a->val - a->max;
  22.             a->max = b->val;
  23.             a->val = a->val + b->val;
  24.         }
  25.     }
  26.     else
  27.     {
  28.         a->max = b->val;
  29.         a->val = a->val + b->val;
  30.         a->f = true;
  31.     }
  32. }
  33.  
  34.  
  35. int main()
  36. {
  37.  
  38.     //freopen("input.txt", "r", stdin);
  39.     //freopen("output.txt", "w", stdout);
  40.  
  41.     int n, m;
  42.     cin >> n >> m;
  43.     cur arr[10][10];
  44.     for (int i = 0; i < n; i++)
  45.     {
  46.         for (int j = 0; j < m; j++)
  47.         {
  48.             cin >> arr[i][j].val;
  49.             arr[i][j].f = false;
  50.         }
  51.     }
  52.  
  53.     if (m == 0 || n == 0)
  54.     {
  55.         cout << "-";
  56.     }
  57.     else
  58.     {
  59.         arr[0][0].f = true;
  60.         for (int i = 0; i < m; i++)
  61.         {
  62.             int t = i;
  63.             int j = 0;
  64.             while (t >= 0 && j < n)
  65.             {
  66.                 if (j - 2 >= 0 && t - 1 >= 0 && arr[j - 2][t - 1].f)
  67.                 {
  68.                     curr(&arr[j][t], &arr[j - 2][t - 1]);
  69.                 }
  70.                 if (j - 2 >= 0 && t + 1 < m && arr[j - 2][t + 1].f)
  71.                 {
  72.                     curr(&arr[j][t], &arr[j - 2][t + 1]);
  73.                 }
  74.                 if (j - 1 >= 0 && t - 2 >= 0 && arr[j - 1][t - 2].f)
  75.                 {
  76.                     curr(&arr[j][t], &arr[j - 1][t - 2]);
  77.                 }
  78.                 if (j + 1 < n && t - 2 >= 0 && (arr[(j + 1)][(t - 2)].f))
  79.                 {
  80.                     curr(&arr[j][t], &arr[j + 1][t - 2]);
  81.                 }
  82.                 j++;
  83.                 t--;
  84.             }
  85.         }
  86.  
  87.         for (int i = 1; i < n; i++)
  88.         {
  89.             int t = m - 1;
  90.             int j = i;
  91.             while (t >= 0 && j < n)
  92.             {
  93.                 if (j - 2 >= 0 && t - 1 >= 0 && arr[j - 2][t - 1].f)
  94.                 {
  95.                     curr(&arr[j][t], &arr[j - 2][t - 1]);
  96.                 }
  97.                 if (j - 2 >= 0 && t + 1 < m && arr[j - 2][t + 1].f)
  98.                 {
  99.                     curr(&arr[j][t], &arr[j - 2][t + 1]);
  100.                 }
  101.                 if (j - 1 >= 0 && t - 2 >= 0 && arr[j - 1][t - 2].f)
  102.                 {
  103.                     curr(&arr[j][t], &arr[j - 1][t - 2]);
  104.                 }
  105.                 if (j + 1 < n && t - 2 >= 0 && (arr[(j + 1)][(t - 2)].f))
  106.                 {
  107.                     curr(&arr[j][t], &arr[j + 1][t - 2]);
  108.                 }
  109.                 j++;
  110.                 t--;
  111.             }
  112.         }
  113.         if (arr[n - 1][m - 1].f == true)
  114.         {
  115.             cout << arr[n - 1][m - 1].val;
  116.         }
  117.         else
  118.         {
  119.             cout << "-";
  120.         }
  121.     }
  122.     cin >> m;
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement