# Perfect Rectangle(完美矩形)

rectangles = [
[1,1,3,3],
[3,1,4,2],
[3,2,4,4],
[1,3,2,4],
[2,3,3,4]
]



rectangles = [
[1,1,2,3],
[1,3,2,4],
[3,1,4,2],
[3,2,4,4]
]



rectangles = [
[1,1,3,3],
[3,1,4,2],
[1,3,2,4],
[3,2,4,4]
]



rectangles = [
[1,1,3,3],
[3,1,4,2],
[1,3,2,4],
[2,2,4,4]
]

class Solution {
public:
bool Cover(vector<int>& a, vector<int>&b)
{
if (min(a[0], a[2]) >= max(b[0], b[2]) || max(a[0], a[2]) <= min(b[0], b[2])
|| min(a[1], a[3]) >= max(b[1], b[3]) || max(a[1], a[3]) <= min(b[1], b[3]))
{
return false;
}
return true;
}
bool isRectangleCover(vector<vector<int>>& rectangles) {
int minx, miny, maxx, maxy;
minx = miny = INT_MAX;
maxx = maxy = INT_MIN;
long long int sum = 0;
for (int i = 0; i < rectangles.size();i++)
{
for (int r = 0; r < i;r++)
{
if (Cover(rectangles[i], rectangles[r]))
{
return false;
}
}
minx = min(minx, min(rectangles[i][0], rectangles[i][2]));
miny = min(miny, min(rectangles[i][1], rectangles[i][3]));
maxx = max(maxx, max(rectangles[i][0], rectangles[i][2]));
maxy = max(maxy, max(rectangles[i][1], rectangles[i][3]));
sum += abs((rectangles[i][0] - rectangles[i][2])*(rectangles[i][1] - rectangles[i][3]));
}
return sum == (maxx - minx)*(maxy - miny);
}
};

class QuadNode
{
public:
enum{
};
vector<vector<int>>* data;
typedef std::list<int> RecList;
typedef std::list<int>::iterator RecListIter;
RecList rectlist;//携带的参数  实体列表
int deep;//自己所在的层索引
int minx,miny;
int maxx,maxy;
QuadNode(vector<vector<int>>* data, int x1, int x2, int y1, int y2, int dp, int qd)
{
minx = x1;
maxx = x2;
miny = y1;
maxy = y2;
deep = dp;
Parent = NULL;
this->data = data;
memset(Children, 0, sizeof(Children));
}
{
for (int i = 0; i < quad_count; i++)
{
if (Children[i])
{
delete Children[i];
Children[i] = NULL;
}
}
rectlist.clear();
}
{
if (deep > 0)
{
//4个孩子都要创建
{
if (!Children[r])
{

QuadNode *node = new QuadNode(data, ix, ax, iy, ay, deep - 1, r);
node->Parent = this;
Children[r] = node;
}
if (Children[r]->CheckInRange(index))
{
return Children[r]->GetDeepest(index);
}
}
}
return this;
}
bool CheckInRange(int index)
{
if ((*data)[index][0] >= minx && (*data)[index][2] <= maxx && (*data)[index][1] >= miny && (*data)[index][3] <= maxy)
{
return true;
}
return false;
}
bool CheckCover(int index)
{
while (parent)
{
if (parent->CheckWithTrianglelist(index))
{
return true;
}
parent = parent->Parent;
}

if (n->CollisionCheck(index))
return true;
n->rectlist.push_back(index);
return false;
}
bool CollisionCheck(int index)
{
return CheckWithTrianglelist(index) || CheckWithSubSpace(index);
}
bool CheckWithTrianglelist(int index)
{
RecListIter itr = rectlist.begin();
while (itr != rectlist.end())
{
int id = *itr;
if (Cover((*data)[id], (*data)[index]))
{
return true;
}
itr++;
}
return false;
}

bool CheckWithSubSpace(int index)
{
bool collision = false;
for (int i = 0; i < quad_count && Children[i]; i++)
{
int vec[] = { minx, miny, maxx, maxy };
vector<int> para(vec, vec + 4);
if (Cover((*data)[index], para))
{
collision |= Children[i]->CollisionCheck(index);
}
if (collision)
{
return true;
}
}
return false;
}

bool Cover(vector<int>& a, vector<int>&b)
{
if (min(a[0], a[2]) >= max(b[0], b[2]) || max(a[0], a[2]) <= min(b[0], b[2])
|| min(a[1], a[3]) >= max(b[1], b[3]) || max(a[1], a[3]) <= min(b[1], b[3]))
{
return false;
}
return true;
}
};
class Solution {
public:
int GetMax2Power(int xmax, int ymax, int& lg)
{
int max = xmax;
if (ymax > max)
max = ymax;
if ((max & (max - 1)) == 0)
{
double L = log(max*1.0) / log(2.0);
lg = (int)L + 1;
return max;
}
else
{
double L = log(max*1.0) / log(2.0);
lg = (int)L + 2;
return (int)pow(2 * 1.0, lg - 1.0);
}
}
bool isRectangleCover(vector<vector<int>>& rectangles) {
int minx, miny, maxx, maxy;
minx = miny = INT_MAX;
maxx = maxy = INT_MIN;
long long int sum = 0;
for (int i = 0; i < rectangles.size(); i++)
{
minx = min(minx, min(rectangles[i][0], rectangles[i][2]));
miny = min(miny, min(rectangles[i][1], rectangles[i][3]));
maxx = max(maxx, max(rectangles[i][0], rectangles[i][2]));
maxy = max(maxy, max(rectangles[i][1], rectangles[i][3]));
}
int mx = max(abs(maxx), abs(minx));
int my = max(abs(maxy), abs(miny));
int range, lg;
range = GetMax2Power(mx, my, lg);
//四叉树

for (int i = 0; i < rectangles.size();i++)
{
if (root->CheckCover(i))
{
return false;
}
sum += abs((rectangles[i][0] - rectangles[i][2])*(rectangles[i][1] - rectangles[i][3]));
}
delete root;
return sum == (maxx - minx)*(maxy - miny);
}
};

　　 long long int getHash(int x, int y)
{
long long int t = 2 << 16;
return x*t + y;
}
bool isRectangleCover(vector<vector<int>>& rectangles) {
int minx, miny, maxx, maxy;
minx = miny = INT_MAX;
maxx = maxy = INT_MIN;
long long int sum = 0;
unordered_set<long long int> st;
for (int i = 0; i < rectangles.size(); i++)
{
minx = min(minx, min(rectangles[i][0], rectangles[i][2]));
miny = min(miny, min(rectangles[i][1], rectangles[i][3]));
maxx = max(maxx, max(rectangles[i][0], rectangles[i][2]));
maxy = max(maxy, max(rectangles[i][1], rectangles[i][3]));
sum += abs((rectangles[i][0] - rectangles[i][2])*(rectangles[i][1] - rectangles[i][3]));
long long int lu = getHash(rectangles[i][0], rectangles[i][3]);
long long int ld = getHash(rectangles[i][0], rectangles[i][1]);
long long int ru = getHash(rectangles[i][2], rectangles[i][3]);
long long int rd = getHash(rectangles[i][2], rectangles[i][1]);
if (st.count(lu) == 0) st.insert(lu);
else st.erase(lu);
if (st.count(ld) == 0) st.insert(ld);
else st.erase(ld);
if (st.count(ru) == 0) st.insert(ru);
else st.erase(ru);
if (st.count(rd) == 0) st.insert(rd);
else st.erase(rd);
}

return sum == (maxx - minx)*(maxy - miny) && st.size() == 4
&& st.count(getHash(minx, miny)) == 1
&& st.count(getHash(minx, maxy)) == 1
&& st.count(getHash(maxx, miny)) == 1
&& st.count(getHash(maxx, maxy)) == 1;
}

posted @ 2018-09-29 18:59  newShit  阅读(633)  评论(0编辑  收藏