SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <iostream> | |
| 2 | #include <string> | |
| 3 | #include <cmath> | |
| 4 | ||
| 5 | using namespace std; | |
| 6 | ||
| 7 | int Size; | |
| 8 | ||
| 9 | float getMin(float a, float b, float c, float d){
| |
| 10 | float tmp = a; | |
| 11 | if(tmp > b) | |
| 12 | tmp = b; | |
| 13 | if(tmp > c) | |
| 14 | tmp = c; | |
| 15 | if(tmp > d) | |
| 16 | tmp = d; | |
| 17 | return tmp; | |
| 18 | } | |
| 19 | ||
| 20 | float getMax(float a, float b, float c, float d){
| |
| 21 | float tmp = a; | |
| 22 | if(tmp < b) | |
| 23 | tmp = b; | |
| 24 | if(tmp < c) | |
| 25 | tmp = c; | |
| 26 | if(tmp < d) | |
| 27 | tmp = d; | |
| 28 | return tmp; | |
| 29 | } | |
| 30 | ||
| 31 | struct STree{
| |
| 32 | float Value; | |
| 33 | float XL, XR; | |
| 34 | float YD, YU; | |
| 35 | int MyNumber; | |
| 36 | int Children; | |
| 37 | STree* Root; | |
| 38 | STree* Child1; | |
| 39 | STree* Child2; | |
| 40 | STree* Child3; | |
| 41 | STree* Child4; | |
| 42 | bool Parent(){
| |
| 43 | if(Children == 0) | |
| 44 | return false; | |
| 45 | else | |
| 46 | return true; | |
| 47 | } | |
| 48 | void SetSize(int size = 0){
| |
| 49 | if(Root == NULL){
| |
| 50 | XL = YD = 0; | |
| 51 | XR = YU = size; | |
| 52 | } | |
| 53 | else{
| |
| 54 | if(MyNumber == 1){
| |
| 55 | XL = Root->XL; | |
| 56 | XR = Root->XR/2; | |
| 57 | YU = Root->YU; | |
| 58 | YD = Root->YU/2; | |
| 59 | } | |
| 60 | else if(MyNumber == 2){
| |
| 61 | XL = Root->XR/2; | |
| 62 | XR = Root->XR; | |
| 63 | YU = Root->YU; | |
| 64 | YD = Root->YU/2; | |
| 65 | } | |
| 66 | else if(MyNumber == 3){
| |
| 67 | XL = Root->XL; | |
| 68 | XR = Root->XR/2; | |
| 69 | YU = Root->YU/2; | |
| 70 | YD = Root->YD; | |
| 71 | } | |
| 72 | else if(MyNumber == 4){
| |
| 73 | XL = Root->XR/2; | |
| 74 | XR = Root->XR; | |
| 75 | YU = Root->YU/2; | |
| 76 | YD = Root->YD; | |
| 77 | } | |
| 78 | } | |
| 79 | } | |
| 80 | void AddChild(STree* root){
| |
| 81 | if(Child1 == NULL){
| |
| 82 | root->Children++; | |
| 83 | STree* child = new STree; | |
| 84 | child->Root = root; | |
| 85 | child->MyNumber = 1; | |
| 86 | child->SetSize(); | |
| 87 | root->Child1 = child; | |
| 88 | } | |
| 89 | else if(Child2 == NULL){
| |
| 90 | root->Children++; | |
| 91 | STree* child = new STree; | |
| 92 | child->Root = root; | |
| 93 | child->MyNumber = 2; | |
| 94 | child->SetSize(); | |
| 95 | root->Child2 = child; | |
| 96 | } | |
| 97 | else if(Child3 == NULL){
| |
| 98 | root->Children++; | |
| 99 | STree* child = new STree; | |
| 100 | child->Root = root; | |
| 101 | child->MyNumber = 3; | |
| 102 | child->SetSize(); | |
| 103 | root->Child3 = child; | |
| 104 | } | |
| 105 | else if(Child4 == NULL){
| |
| 106 | root->Children++; | |
| 107 | STree* child = new STree; | |
| 108 | child->Root = root; | |
| 109 | child->MyNumber = 4; | |
| 110 | child->SetSize(); | |
| 111 | root->Child4 = child; | |
| 112 | } | |
| 113 | } | |
| 114 | void SetValue(int x, int y, float val, STree* node){
| |
| 115 | if(XR - XL == 1 && YU - YD == 1){
| |
| 116 | Value = val; | |
| 117 | } | |
| 118 | else{
| |
| 119 | while(Children < 4) | |
| 120 | AddChild(node); | |
| 121 | if(x < XR/2 && y < YU/2){
| |
| 122 | Child3->SetValue(x,y,val,Child3); | |
| 123 | } | |
| 124 | else if(x >= XR/2 && y < YU/2){
| |
| 125 | Child4->SetValue(x,y,val,Child4); | |
| 126 | } | |
| 127 | else if(x < XR/2 && y >= YU/2){
| |
| 128 | Child1->SetValue(x,y,val,Child1); | |
| 129 | } | |
| 130 | else if(x >= XR/2 && y >= YU/2){
| |
| 131 | Child2->SetValue(x,y,val,Child2); | |
| 132 | } | |
| 133 | Meld(node); | |
| 134 | } | |
| 135 | } | |
| 136 | void Meld(STree* node){
| |
| 137 | if(!Child1->Parent() && !Child2->Parent() && !Child3->Parent() && !Child4->Parent()){
| |
| 138 | if(getMax(Child1->Value, Child2->Value, Child3->Value, Child4->Value) - getMin(Child1->Value, Child2->Value, Child3->Value, Child4->Value) < 2){
| |
| 139 | Value = (Child1->Value+Child2->Value+Child3->Value+Child4->Value)/4; | |
| 140 | delete Child1; | |
| 141 | delete Child2; | |
| 142 | delete Child3; | |
| 143 | delete Child4; | |
| 144 | Children = 0; | |
| 145 | } | |
| 146 | } | |
| 147 | } | |
| 148 | void Print(){
| |
| 149 | if(Children == 0) | |
| 150 | cout << XL << "," << XR << "," << YU << "," << YD << ":" << Value << endl; | |
| 151 | else{
| |
| 152 | if(Child1 != NULL){
| |
| 153 | Child1->Print(); | |
| 154 | } | |
| 155 | if(Child2 != NULL){
| |
| 156 | Child2->Print(); | |
| 157 | } | |
| 158 | if(Child3 != NULL){
| |
| 159 | Child3->Print(); | |
| 160 | } | |
| 161 | if(Child4 != NULL){
| |
| 162 | Child4->Print(); | |
| 163 | } | |
| 164 | } | |
| 165 | } | |
| 166 | STree(){
| |
| 167 | Root = NULL; | |
| 168 | Child1 = Child2 = Child3 = Child4 = NULL; | |
| 169 | Value = 0; | |
| 170 | MyNumber = 0; | |
| 171 | XL = XR = YU = YD = 0; | |
| 172 | Children = 0; | |
| 173 | } | |
| 174 | }; | |
| 175 | ||
| 176 | STree* First = new STree; | |
| 177 | ||
| 178 | int main(){
| |
| 179 | cout << "Podaj wymiar kwadratu" << endl; | |
| 180 | cin >> Size; | |
| 181 | First->SetSize(Size); | |
| 182 | string input; | |
| 183 | while(input != "exit"){
| |
| 184 | input.clear(); | |
| 185 | cin >> input; | |
| 186 | if(input == "set"){
| |
| 187 | int x, y; float v; | |
| 188 | cin >> x >> y >> v; | |
| 189 | First->SetValue(x,y,v,First); | |
| 190 | } | |
| 191 | else if(input == "print"){
| |
| 192 | First->Print(); | |
| 193 | } | |
| 194 | } | |
| 195 | return 0; | |
| 196 | } |