Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct Node {
- int field;
- Node* bottom;
- Node* right;
- };
- int getMax(Node* node, bool Max, int level) {
- if (node->bottom != NULL && node->right != NULL) {
- int bottom = getMax(node->bottom, !Max, level+1);
- int right = getMax(node->right, !Max, level + 1);
- cout << string(level, '-') << (Max && bottom > right || bottom < right ? bottom : right) + node->field << (Max ? " max" : " min") << '\n';
- return ((Max && (bottom > right)) || (bottom < right) ? bottom : right) + node->field;
- }
- else {
- if (node->bottom != NULL) {
- int bottom = getMax(node->bottom, !Max, level + 1);
- cout << string(level, '-') << bottom + node->field << ' ' << (Max ? " max" : " min") << '\n';
- return bottom + node->field;
- }
- else if (node->right != NULL) {
- int right = getMax(node->right, !Max, level + 1);
- cout << string(level, '-') << right + node->field << (Max ? " max" : " min") << '\n';
- return right + node->field;
- }
- }
- cout << string(level, '-') << node->field << (Max ? " max" : " min") << '\n';
- return node->field;
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- int N=3, M=4;
- Node*** nodeMass = new Node**[N];
- for (int i = 0; i < N; i++) {
- nodeMass[i] = new Node * [M];
- for (int j = 0; j < M; j++) {
- nodeMass[i][j] = new Node;
- cin >> nodeMass[i][j]->field;
- }
- }
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- nodeMass[i][j]->bottom = i < N - 1 ? nodeMass[i + 1][j] : NULL;
- nodeMass[i][j]->right = j < M - 1 ? nodeMass[i][j + 1] : NULL;
- }
- }
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++) {
- cout << nodeMass[i][j]->field << ' ';
- }
- cout << '\n';
- }
- cout << getMax(nodeMass[0][0], true, 0) << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement