add new solutions

This commit is contained in:
kanna5 2026-01-05 16:48:03 +09:00
parent d798d5e8c9
commit 886b5e0a8e
Signed by: kkyy
GPG key ID: 06332F3965E9B0CF
34 changed files with 1164 additions and 0 deletions

View file

@ -0,0 +1,44 @@
package q427
type Node struct {
Val bool
IsLeaf bool
TopLeft *Node
TopRight *Node
BottomLeft *Node
BottomRight *Node
}
func mkTree(grid [][]int, x, y, w int) *Node {
if w == 1 {
return &Node{
Val: grid[y][x] == 1,
IsLeaf: true,
}
}
subtreeW := w / 2
node := &Node{
TopLeft: mkTree(grid, x, y, subtreeW),
TopRight: mkTree(grid, x+subtreeW, y, subtreeW),
BottomLeft: mkTree(grid, x, y+subtreeW, subtreeW),
BottomRight: mkTree(grid, x+subtreeW, y+subtreeW, subtreeW),
}
if !node.TopLeft.IsLeaf || !node.TopRight.IsLeaf || !node.BottomLeft.IsLeaf || !node.BottomRight.IsLeaf {
return node
}
if node.TopLeft.Val && node.TopRight.Val && node.BottomLeft.Val && node.BottomRight.Val ||
!node.TopLeft.Val && !node.TopRight.Val && !node.BottomLeft.Val && !node.BottomRight.Val {
return &Node{
Val: node.TopLeft.Val,
IsLeaf: true,
}
}
return node
}
func construct(grid [][]int) *Node {
return mkTree(grid, 0, 0, len(grid))
}
var _ = construct