Untitled

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

using namespace std;

class DungeonNode {
private:
    int data;
    vector<DungeonNode*> children;
public:
    DungeonNode(int data) : data(data) {}
    int getdata() { return data; }
    void setdata(int data) { this->data = 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, int position);
    DungeonNode* gethead() { return head; }
    void traverse(DungeonNode* node);
    vector<vector<int>> getnodelist(DungeonNode* node, vector<vector<int>>& nodesByDepth, int depth);
    void editnode(vector<int> path, int value);
};

void Dungeon::insertcell(int data, int depth, int position) {
    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;
        }
        if (position >= current->children.size()) {
            position = 0;
        }
        current = current->children[position];
    }
    current->addchild(new DungeonNode(data));
}

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

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

    if (nodesByDepth.size() <= depth) {
        nodesByDepth.push_back(vector<int>());
    }

    nodesByDepth[depth].push_back(node->getdata());

    for (int i = 0; i < node->children.size(); i++) {
        getnodelist(node->children[i], nodesByDepth, depth + 1);
    }

    return nodesByDepth;
}

void Dungeon::editnode(vector<int> path, int value) {
    DungeonNode* current = head;
    for (int i = 0; i < path.size(); i++) {
        if (current->children.empty() || path[i] >= current->children.size()) {
            return;
        }
        current = current->children[path[i]];
    }
    current->setdata(value);
}

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

    // Insert cells
    for (int i = 0; i < nodes; i++) {
        int depth = i / (int)pow(4, L - 1);
        int position = i % (int)pow(4, L - 1);
        dungeon.insertcell(data, depth, position);
        data++;
    }

    // Edit node
    vector<int> path = {0, 0, 0, 3};
    dungeon.editnode(path, 4);

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

    return 0;
}
Leave a Comment