-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path558.cpp
More file actions
130 lines (123 loc) · 3.74 KB
/
558.cpp
File metadata and controls
130 lines (123 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
__________________________________________________________________________________________________
sample 300 ms submission
/*
// Definition for a QuadTree node.
class Node {
public:
bool val;
bool isLeaf;
Node* topLeft;
Node* topRight;
Node* bottomLeft;
Node* bottomRight;
Node() {}
Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
val = _val;
isLeaf = _isLeaf;
topLeft = _topLeft;
topRight = _topRight;
bottomLeft = _bottomLeft;
bottomRight = _bottomRight;
}
};
*/
class Solution {
public:
Node* intersect(Node* T1, Node* T2) {
if(T1->isLeaf && T1->val || T2->isLeaf && T2->val == 0) return T1;
if(T2->isLeaf && T2->val || T1->isLeaf && T1->val == 0) return T2;
Node *tl = intersect(T1->topLeft, T2->topLeft);
Node *tr = intersect(T1->topRight, T2->topRight);
Node *bl = intersect(T1->bottomLeft, T2->bottomLeft);
Node *br = intersect(T1->bottomRight, T2->bottomRight);
if(tl->isLeaf && tr->isLeaf && bl->isLeaf && br->isLeaf &&
tl->val == tr->val && tl->val == bl->val && tl->val == br->val)
{
bool val = tl->val;
delete tl, tr, bl, br;
return new Node(val, 1, 0, NULL, NULL, NULL);
}
else return new Node(0, 0, tl, tr, bl, br);
}
};
static int x = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return NULL;
}();
__________________________________________________________________________________________________
sample 49064 kb submission
/*
// Definition for a QuadTree node.
class Node {
public:
bool val;
bool isLeaf;
Node* topLeft;
Node* topRight;
Node* bottomLeft;
Node* bottomRight;
Node() {}
Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
val = _val;
isLeaf = _isLeaf;
topLeft = _topLeft;
topRight = _topRight;
bottomLeft = _bottomLeft;
bottomRight = _bottomRight;
}
};
*/
class Solution {
public:
Node* intersect(Node* quadTree1, Node* quadTree2) {
if((!quadTree1)&&(!quadTree2))return NULL;
if(!quadTree2){
quadTree2 = quadTree1;
quadTree1=NULL;
}
Node* newRoot;
if(!quadTree1){
newRoot = quadTree2;
}
else{
if(quadTree1->isLeaf&&quadTree1->val){
newRoot = quadTree1;
}
else if(quadTree2->isLeaf&&quadTree2->val){
newRoot = quadTree2;
}
else if(quadTree2->isLeaf&&quadTree1->isLeaf){
newRoot = quadTree2;
}
else{
newRoot = quadTree1;
newRoot->isLeaf = false;
newRoot->val = false;
newRoot->topLeft = intersect(quadTree1->topLeft,quadTree2->topLeft);
newRoot->topRight = intersect(quadTree1->topRight,quadTree2->topRight);
newRoot->bottomLeft = intersect(quadTree1->bottomLeft,quadTree2->bottomLeft);
newRoot->bottomRight = intersect(quadTree1->bottomRight,quadTree2->bottomRight);
if(newRoot->topLeft->isLeaf&&
newRoot->topRight->isLeaf&&
newRoot->bottomLeft->isLeaf&&
newRoot->bottomRight->isLeaf){
bool result = newRoot->topLeft->val;
if(result==newRoot->topRight->val&&
result==newRoot->bottomLeft->val&&
result==newRoot->bottomRight->val){
newRoot->val = result;
newRoot->isLeaf = true;
newRoot->topLeft = NULL;
newRoot->topRight = NULL;
newRoot->bottomLeft = NULL;
newRoot->bottomRight = NULL;
}
}
}
}
return newRoot;
}
};
__________________________________________________________________________________________________