树型简单数据操作

时间:2019-04-29 11:33:33

标签: c++ tree octree

我们的任务是创建一个具有蓝色和白色体素的Octree。但是,对于级别1和级别2的输入,我只能得到正确的答案。

详细信息:

  1. p-表示仍需要将大体素划分为八分位数

    • b-体素为蓝色
    • w-体素为白色
  2. 假定未分配任何颜色的体素为白色

  3. 输入:N表示要相互添加多少对

正确的输出:

4096
3072
2048
1184

输入:

4
b
w
pbbbbwwww
pbwbwbwbw
pbpbbwwwwbbwwwwpbbwwwwbbb
ppbbwwwwbbbwwwwbpbbwwwwbb
pppbwwwwwbwwwwwwbwwwwwwbw
pwpwpwbwwwwwbwwwwwbwwwwwb

O树:

#include <iostream>
#include <queue> 
#include <string>
#include <cmath>
using namespace std; 

int N,lvl;
string s1,s2;
int index_a = 0;
int index_b = 0;
int VCount();

struct Node 
{ 
    char data;
    vector<Node *>child; 
}; 

Node *newNode(char data) 
{ 
    Node *temp = new Node; 
    temp->data = data;
    return temp; 
} 

Node *root;
int Voxels(vector<Node *> _root, int level);


int main() 
{ 
    cin>>N;
    for(int i=0;i<N;i++){
        root = newNode('w');
        cin>>s1;
        cin>>s2;
            lvl = 1;
            for(int j = 0; s1[j] != '\0'; j++){
                if(s1[j] == 'p')
                    lvl++;
            }
        cout<<VCount();
        cout<<'\n';
        index_a = 0;
        index_b = 0;
        lvl = 0;
    }     
return 0;   
}  

int VCount(){

    int _vox = 0;

        for(int l = 0; l < 8; l++)                                                                      //  Level 1
            (root->child).push_back(newNode('w'));   
                if (lvl == 1)
                return Voxels(root->child,1);

            if (lvl >= 2){
                 for(int k = 0; k < 8; k++){                                                             //  Level 2
                    for(int j = 0; j < 8; j++){
                        (root->child[k]->child).push_back(newNode('w'));
                    }
                 }
                _vox = Voxels(root->child,2);


            if (lvl >= 3){
                for(int l = 0; l < 8; ++l){
                    for(int k = 0; k < 8; k++){                                                         //  Level 3
                        for(int j = 0; j < 8; j++){
                            (root->child[l]->child[k]->child).push_back(newNode('w')); 
                        }

                    }
                    _vox += Voxels(root->child[l]->child,3);
                }


                if (lvl == 4){
                    for(int l = 0; l < 8; l++){      
                        for(int k = 0; k < 8; k++){                                                     //  Level 4
                            for(int j = 0; j < 8; j++){
                                for(int i = 0; i < 8; i++)
                                    (root->child[l]->child[k]->child[j]->child).push_back(newNode('w'));   
                            }
                        _vox += Voxels(root->child[l]->child[k]->child,4);
                    }
                  }
                }

            }

        }

    return _vox;  
}

int Voxels(vector<Node *> _root, int level){
int _voxels = 0;
int _ind;
int branch = 0;

        if(s1.size() == 1){
                if(s1[index_a] =='b' || s2[index_b] == 'b')
                    return 4096;
                else
                    return 0;
            }

        index_a++;
        index_b++;
        _ind = index_a;


            if(s1[index_a] != 'p' && s2[index_b] != 'p'){
                for(int i = index_a, j = 0; i < s1.size() && s1[i] != 'p'; i++,j++){
                    if(s1[i] == 'b'){
                        _root[branch]->child[j]->data = 'b';
                        _voxels++;
                    }
                    if(j==7){
                        branch++;
                        j=0;
                    }
                    index_a = i;  
                }
                for(int i = index_b, j = 0; i < s2.size() && s2[i] != 'p'; i++, j++){
                    if(s2[i] == 'b' && s1[_ind] != 'b'){
                        _root[branch]->child[j]->data = 'b';
                        _voxels++;
                    }
                    if(j==7){
                        branch++;
                        j=0;
                    }
                   _ind++;
                }
            }

return (4096/pow(8,level-1))*_voxels;
}

0 个答案:

没有答案