Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- #pragma comment(linker, "/STACK:104857600")
- struct Node {
- int field;
- Node* bottom;
- Node* right;
- };
- void print_Tree(Node* p, int level)
- {
- if (p)
- {
- print_Tree(p->bottom, level + 1);
- for (int i = 0; i < level; i++) cout << " ";
- cout << p->field << endl;
- print_Tree(p->right, level + 1);
- }
- }
- int getMax(Node* node, bool Max, int level, char &hod) {
- if (node->bottom != NULL && node->right != NULL) {
- int bottom = getMax(node->bottom, !Max, level+1, hod);
- int right = getMax(node->right, !Max, level + 1, hod);
- hod = (Max && (bottom > right)) || (!Max && (bottom < right)) ? 'D' : 'R';
- //cout << string(level*3, '-') << node->field << ' ' << ((Max && (bottom > right)) || (!Max && (bottom < right)) ? bottom : right) + node->field << (Max ? " max" : " min") << '\n';
- return ((Max && (bottom > right)) || (!Max && (bottom < right)) ? bottom : right) + node->field;
- }
- else {
- if (node->bottom != NULL) {
- int bottom = getMax(node->bottom, !Max, level + 1, hod);
- //cout << string(level * 3, '-') << node->field << ' ' << bottom + node->field << ' ' << (Max ? " max" : " min") << '\n';
- hod = 'D';
- return bottom + node->field;
- }
- else if (node->right != NULL) {
- int right = getMax(node->right, !Max, level + 1, hod);
- hod = 'R';
- //cout << string(level * 3, '-') << node->field << ' ' << right + node->field << (Max ? " max" : " min") << '\n';
- return right + node->field;
- }
- }
- //cout << string(level * 3, '-') << node->field << ' ' << node->field << (Max ? " max" : " min") << '\n';
- return node->field;
- }
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- int N, M;
- cin >> N >> M;
- 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;
- }
- }
- // print_Tree(nodeMass[0][0], 0);
- Node* root = nodeMass[0][0];
- int summ = 0;
- for (int i = 0; i < N + M - 2; i++) {
- char hod = ' ';
- if (i % 2 == 0) {
- int max = getMax(root, true, 0, hod);
- cout << summ + max << ' ' << hod << '\n';
- }
- else {
- cin >> hod;
- }
- summ += root->field;
- switch (hod)
- {
- case 'D':
- root = root->bottom;
- break;
- case 'R':
- root = root->right;
- break;
- default:
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement