add: 220429-CN [cpp]
This commit is contained in:
parent
32b1794fe0
commit
87c808a61b
|
|
@ -0,0 +1,49 @@
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
// Definition for a QuadTree node.
|
||||||
|
class Node {
|
||||||
|
public:
|
||||||
|
bool val, isLeaf;
|
||||||
|
Node* topLeft;
|
||||||
|
Node* topRight;
|
||||||
|
Node* bottomLeft;
|
||||||
|
Node* bottomRight;
|
||||||
|
|
||||||
|
explicit Node(bool val = false, bool isLeaf = false, Node* topLeft = nullptr, Node* topRight = nullptr, Node* bottomLeft = nullptr, Node* bottomRight = nullptr) : val(val), isLeaf(isLeaf), topLeft(topLeft), topRight(topRight), bottomLeft(bottomLeft), bottomRight(bottomRight) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 427. Construct Quad Tree
|
||||||
|
* Given a n * n matrix grid of 0's and 1's only. We want to represent the grid with a Quad-Tree.
|
||||||
|
* Return the root of the Quad-Tree representing the grid.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
Node* construct(std::vector<std::vector<int>>& G) {
|
||||||
|
const int n = G.size();
|
||||||
|
|
||||||
|
int* prefixSum = new int[(n + 1) * (1 + n)]{};
|
||||||
|
for (int i = 1; i <= n; ++i)
|
||||||
|
for (int j = 1; j <= n; ++j)
|
||||||
|
prefixSum[i * n + j] = prefixSum[(i - 1) * n + j] + prefixSum[i * n + j - 1] - prefixSum[(i - 1) * n + j - 1] + G[i - 1][j - 1];
|
||||||
|
|
||||||
|
auto sum = [&](int x0, int y0, int x1, int y1) {
|
||||||
|
return prefixSum[x1 * n + y1] - prefixSum[x0 * n + y1] - prefixSum[x1 * n + y0] + prefixSum[x0 * n + y0];
|
||||||
|
};
|
||||||
|
|
||||||
|
std::function<Node*(int, int, int, int)> dfs = [&](int x0, int y0, int x1, int y1) {
|
||||||
|
int s = sum(x0, y0, x1, y1);
|
||||||
|
if (!s)
|
||||||
|
return new Node(false, true);
|
||||||
|
if (s == (y1 - y0) * (x1 - x0))
|
||||||
|
return new Node(true, true);
|
||||||
|
const int mx = (x0 + x1) >> 1, my = (y0 + y1) >> 1;
|
||||||
|
return new Node(std::rand() & 1, false, dfs(x0, y0, mx, my), dfs(x0, my, mx, y1), dfs(mx, y0, x1, my), dfs(mx, my, x1, y1));
|
||||||
|
};
|
||||||
|
|
||||||
|
return dfs(0, 0, n, n);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2204)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2204 220429.cpp)
|
ADD_EXECUTABLE(2204 220429-CN.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue