Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- using namespace std;
- int Size;
- float getMin(float a, float b, float c, float d){
- float tmp = a;
- if(tmp > b)
- tmp = b;
- if(tmp > c)
- tmp = c;
- if(tmp > d)
- tmp = d;
- return tmp;
- }
- float getMax(float a, float b, float c, float d){
- float tmp = a;
- if(tmp < b)
- tmp = b;
- if(tmp < c)
- tmp = c;
- if(tmp < d)
- tmp = d;
- return tmp;
- }
- struct STree{
- float Value;
- float XL, XR;
- float YD, YU;
- int MyNumber;
- int Children;
- STree* Root;
- STree* Child1;
- STree* Child2;
- STree* Child3;
- STree* Child4;
- bool Parent(){
- if(Children == 0)
- return false;
- else
- return true;
- }
- void SetSize(int size = 0){
- if(Root == NULL){
- XL = YD = 0;
- XR = YU = size;
- }
- else{
- if(MyNumber == 1){
- XL = Root->XL;
- XR = Root->XR/2;
- YU = Root->YU;
- YD = Root->YU/2;
- }
- else if(MyNumber == 2){
- XL = Root->XR/2;
- XR = Root->XR;
- YU = Root->YU;
- YD = Root->YU/2;
- }
- else if(MyNumber == 3){
- XL = Root->XL;
- XR = Root->XR/2;
- YU = Root->YU/2;
- YD = Root->YD;
- }
- else if(MyNumber == 4){
- XL = Root->XR/2;
- XR = Root->XR;
- YU = Root->YU/2;
- YD = Root->YD;
- }
- }
- }
- void AddChild(STree* root){
- if(Child1 == NULL){
- root->Children++;
- STree* child = new STree;
- child->Root = root;
- child->MyNumber = 1;
- child->SetSize();
- root->Child1 = child;
- }
- else if(Child2 == NULL){
- root->Children++;
- STree* child = new STree;
- child->Root = root;
- child->MyNumber = 2;
- child->SetSize();
- root->Child2 = child;
- }
- else if(Child3 == NULL){
- root->Children++;
- STree* child = new STree;
- child->Root = root;
- child->MyNumber = 3;
- child->SetSize();
- root->Child3 = child;
- }
- else if(Child4 == NULL){
- root->Children++;
- STree* child = new STree;
- child->Root = root;
- child->MyNumber = 4;
- child->SetSize();
- root->Child4 = child;
- }
- }
- void SetValue(int x, int y, float val, STree* node){
- if(XR - XL == 1 && YU - YD == 1){
- Value = val;
- }
- else{
- while(Children < 4)
- AddChild(node);
- if(x < XR/2 && y < YU/2){
- Child3->SetValue(x,y,val,Child3);
- }
- else if(x >= XR/2 && y < YU/2){
- Child4->SetValue(x,y,val,Child4);
- }
- else if(x < XR/2 && y >= YU/2){
- Child1->SetValue(x,y,val,Child1);
- }
- else if(x >= XR/2 && y >= YU/2){
- Child2->SetValue(x,y,val,Child2);
- }
- Meld(node);
- }
- }
- void Meld(STree* node){
- if(!Child1->Parent() && !Child2->Parent() && !Child3->Parent() && !Child4->Parent()){
- if(getMax(Child1->Value, Child2->Value, Child3->Value, Child4->Value) - getMin(Child1->Value, Child2->Value, Child3->Value, Child4->Value) < 2){
- Value = (Child1->Value+Child2->Value+Child3->Value+Child4->Value)/4;
- delete Child1;
- delete Child2;
- delete Child3;
- delete Child4;
- Children = 0;
- }
- }
- }
- void Print(){
- if(Children == 0)
- cout << XL << "," << XR << "," << YU << "," << YD << ":" << Value << endl;
- else{
- if(Child1 != NULL){
- Child1->Print();
- }
- if(Child2 != NULL){
- Child2->Print();
- }
- if(Child3 != NULL){
- Child3->Print();
- }
- if(Child4 != NULL){
- Child4->Print();
- }
- }
- }
- STree(){
- Root = NULL;
- Child1 = Child2 = Child3 = Child4 = NULL;
- Value = 0;
- MyNumber = 0;
- XL = XR = YU = YD = 0;
- Children = 0;
- }
- };
- STree* First = new STree;
- int main(){
- cout << "Podaj wymiar kwadratu" << endl;
- cin >> Size;
- First->SetSize(Size);
- string input;
- while(input != "exit"){
- input.clear();
- cin >> input;
- if(input == "set"){
- int x, y; float v;
- cin >> x >> y >> v;
- First->SetValue(x,y,v,First);
- }
- else if(input == "print"){
- First->Print();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment