Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.4 kB
2
Indexable
Never
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class DungeonNode {
private:
    int data;
    vector<DungeonNode*> children;
public:
    DungeonNode(int data) : data(data) {}
    int getdata() { return data; }
    void addchild(DungeonNode* child) { children.push_back(child); }
    friend class Dungeon;
};

class Dungeon {
private:
    DungeonNode* head;
public:
    Dungeon() : head(nullptr) {}
    void insertcell(int data, int depth);
    DungeonNode* gethead() { return head; }
    void traverse(DungeonNode* node);
    vector<int> getnodelist(DungeonNode* node, int depth);
};

void Dungeon::insertcell(int data, int depth) {
    if (head == nullptr) {
        head = new DungeonNode(data);
        return;
    }
    DungeonNode* current = head;
    for (int i = 0; i < depth; ++i) {
        if (current->children.empty()) {
            current->addchild(new DungeonNode(data));
            return;
        }
        current = current->children.back();
    }
    current->addchild(new DungeonNode(data));
}

void Dungeon::traverse(DungeonNode* node) {
    vector<int> nodes = getnodelist(node, 0);
    sort(nodes.begin(), nodes.end());
    for (int i = 0; i < nodes.size(); i++) {
        cout << nodes[i] << "|";
    }
}

vector<int> Dungeon::getnodelist(DungeonNode* node, int depth) {
    vector<int> temp;
    if (node == nullptr) return temp;

    if (depth == 0) {
        temp.push_back(node->getdata());
    }

    for (int i = 0; i < node->children.size(); i++) {
        vector<int> subnodes = getnodelist(node->children[i], depth + 1);
        temp.insert(temp.end(), subnodes.begin(), subnodes.end());
    }

    return temp;
}

int main() {
    Dungeon dungeon;
    int L = 2; // Depth of the dungeon
    int nodes = 15; // Total nodes to insert
    int data = 1;

    // Insert cells
    for (int i = 0; i < nodes; i++) {
        dungeon.insertcell(data++, L);
    }

    // Traverse and display the dungeon
    cout << "Dungeon nodes: ";
    dungeon.traverse(dungeon.gethead());
    cout << endl;

    // Test sorting function
    vector<int> tosort = {9, 8, 7, 6, 5, 2, 5, 5, 1};
    sort(tosort.begin(), tosort.end());
    cout << "Output: ";
    for(int i = 0; i < tosort.size(); i++){
        cout << tosort[i] << "|";
    }
    cout << endl;

    return 0;
}
Leave a Comment