Advertisement
noctual

Untitled

Oct 10th, 2021
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Node {
  6.     int field;
  7.     Node* bottom;
  8.     Node* right;
  9. };
  10.  
  11. int getMax(Node* node, bool Max, int level) {
  12.     if (node->bottom != NULL && node->right != NULL) {
  13.         int bottom = getMax(node->bottom, !Max, level+1);
  14.         int right = getMax(node->right, !Max, level + 1);
  15.         cout << string(level, '-') << (Max && bottom > right || bottom < right ? bottom : right) + node->field << (Max ? " max" : " min") << '\n';
  16.         return ((Max && (bottom > right)) || (bottom < right) ? bottom : right) + node->field;
  17.     }
  18.     else {
  19.         if (node->bottom != NULL) {
  20.             int bottom = getMax(node->bottom, !Max, level + 1);
  21.             cout << string(level, '-') << bottom + node->field << ' ' << (Max ? " max" : " min") << '\n';
  22.             return bottom + node->field;
  23.         }
  24.         else if (node->right != NULL) {
  25.             int right = getMax(node->right, !Max, level + 1);
  26.             cout << string(level, '-') << right + node->field << (Max ? " max" : " min") << '\n';
  27.             return right + node->field;
  28.         }
  29.     }
  30.     cout << string(level, '-') << node->field << (Max ? " max" : " min") << '\n';
  31.     return node->field;
  32. }
  33.  
  34. int main() {
  35.     ios::sync_with_stdio(0);
  36.     cin.tie(0);
  37.  
  38.     int N=3, M=4;
  39.  
  40.     Node*** nodeMass = new Node**[N];
  41.     for (int i = 0; i < N; i++) {
  42.         nodeMass[i] = new Node * [M];
  43.         for (int j = 0; j < M; j++) {
  44.             nodeMass[i][j] = new Node;
  45.             cin >> nodeMass[i][j]->field;
  46.         }
  47.     }
  48.  
  49.     for (int i = 0; i < N; i++) {
  50.         for (int j = 0; j < M; j++) {
  51.             nodeMass[i][j]->bottom = i < N - 1 ? nodeMass[i + 1][j] : NULL;
  52.             nodeMass[i][j]->right = j < M - 1 ? nodeMass[i][j + 1] : NULL;
  53.         }
  54.     }
  55.  
  56.     for (int i = 0; i < N; i++) {
  57.         for (int j = 0; j < M; j++) {
  58.             cout << nodeMass[i][j]->field << ' ';
  59.         }
  60.         cout << '\n';
  61.     }
  62.  
  63.     cout << getMax(nodeMass[0][0], true, 0) << '\n';
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement