当前位置:网站首页>August 20, 2021: brick making. There is a binary grid of m x n, where 1 table
August 20, 2021: brick making. There is a binary grid of m x n, where 1 table
2022-06-24 05:11:00 【Fuda scaffold constructor's daily question】
2021-08-20: Brick making . There is one m x n The binary mesh of , among 1 For bricks ,0 It means blank . brick Stable ( It won't fall ) The premise is :1. A brick is connected directly to the top of the grid , perhaps ,2. There's at least one adjacent (4 One of the directions ) brick Stable When it doesn't fall . Give you an array hits , This is where the bricks need to be removed in turn . Whenever eliminated hitsi = (rowi, coli) A brick in position , Bricks in the corresponding positions ( If exist ) Will disappear , Then other bricks may fall due to this elimination operation . Once a brick falls , It will immediately disappear from the grid ( namely , It doesn't fall on other stable bricks ). Returns an array result , among resulti It means the first one i The number of bricks dropped corresponding to one elimination operation . Be careful , Elimination may point to a blank space without bricks , If this happens , No bricks fall .
Fuda answer 2021-08-20:
Union checking set . Converse thinking .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
grid := [][]int{{1, 0, 0, 0}, {1, 1, 1, 0}}
hits := [][]int{{1, 0}}
ret := hitBricks(grid, hits)
fmt.Println(ret)
}
func hitBricks(grid [][]int, hits [][]int) []int {
for i := 0; i < len(hits); i++ {
if grid[hits[i][0]][hits[i][1]] == 1 {
grid[hits[i][0]][hits[i][1]] = 2
}
}
unionFind := NewUnionFind(grid)
ans := make([]int, len(hits))
for i := len(hits) - 1; i >= 0; i-- {
if grid[hits[i][0]][hits[i][1]] == 2 {
ans[i] = unionFind.finger(hits[i][0], hits[i][1])
}
}
return ans
}
// Union checking set
type UnionFind struct {
N int
M int
// How many bricks are there , Connected to the ceiling
cellingAll int
// Original matrix , Because of the impact of shells ,1 -> 2
grid [][]int
// cellingSet[i] = true; i It's the head node , The collection is the ceiling collection
cellingSet []bool
fatherMap []int
sizeMap []int
stack []int
}
func NewUnionFind(matrix [][]int) *UnionFind {
res := &UnionFind{}
res.initSpace(matrix)
res.initConnect()
return res
}
func (this *UnionFind) initSpace(matrix [][]int) {
this.grid = matrix
this.N = len(this.grid)
this.M = len(this.grid[0])
all := this.N * this.M
this.cellingAll = 0
this.cellingSet = make([]bool, all)
this.fatherMap = make([]int, all)
this.sizeMap = make([]int, all)
this.stack = make([]int, all)
for row := 0; row < this.N; row++ {
for col := 0; col < this.M; col++ {
if this.grid[row][col] == 1 {
index := row*this.M + col
this.fatherMap[index] = index
this.sizeMap[index] = 1
if row == 0 {
this.cellingSet[index] = true
this.cellingAll++
}
}
}
}
}
func (this *UnionFind) initConnect() {
for row := 0; row < this.N; row++ {
for col := 0; col < this.M; col++ {
this.union(row, col, row-1, col)
this.union(row, col, row+1, col)
this.union(row, col, row, col-1)
this.union(row, col, row, col+1)
}
}
}
func (this *UnionFind) find(row int, col int) int {
stackSize := 0
index := row*this.M + col
for index != this.fatherMap[index] {
this.stack[stackSize] = index
stackSize++
index = this.fatherMap[index]
}
for stackSize != 0 {
stackSize--
this.fatherMap[this.stack[stackSize]] = index
}
return index
}
func (this *UnionFind) union(r1 int, c1 int, r2 int, c2 int) {
if this.valid(r1, c1) && this.valid(r2, c2) {
father1 := this.find(r1, c1)
father2 := this.find(r2, c2)
if father1 != father2 {
size1 := this.sizeMap[father1]
size2 := this.sizeMap[father2]
status1 := this.cellingSet[father1]
status2 := this.cellingSet[father2]
if size1 <= size2 {
this.fatherMap[father1] = father2
this.sizeMap[father2] = size1 + size2
if status1 && !status2 || !status1 && status2 {
this.cellingSet[father2] = true
this.cellingAll += twoSelectOne(status1, size2, size1)
}
} else {
this.fatherMap[father2] = father1
this.sizeMap[father1] = size1 + size2
if status1 && !status2 || !status1 && status2 {
this.cellingSet[father1] = true
this.cellingAll += twoSelectOne(status1, size2, size1)
}
}
}
}
}
func (this *UnionFind) valid(row int, col int) bool {
return row >= 0 && row < this.N && col >= 0 && col < this.M && this.grid[row][col] == 1
}
func (this *UnionFind) cellingNum() int {
return this.cellingAll
}
func (this *UnionFind) finger(row int, col int) int {
this.grid[row][col] = 1
cur := row*this.M + col
if row == 0 {
this.cellingSet[cur] = true
this.cellingAll++
}
this.fatherMap[cur] = cur
this.sizeMap[cur] = 1
pre := this.cellingAll
this.union(row, col, row-1, col)
this.union(row, col, row+1, col)
this.union(row, col, row, col-1)
this.union(row, col, row, col+1)
now := this.cellingAll
if row == 0 {
return now - pre
} else {
return twoSelectOne(now == pre, 0, now-pre-1)
}
}
func twoSelectOne(c bool, a int, b int) int {
if c {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- What is required for domain name filing and how to select an enterprise domain name
- How to select a suitable optical fiber tester
- What is an evpn switch?
- Before creating an image, it is recommended to execute the following code to purify the image as an administrator
- Black horse programmer machine learning handout: preliminary use of linear regression API
- Open source and SaaS, how to choose software?
- Use of golang testing framework goshub
- Introduction to vulnerability priority technology (VPT)
- The function of nearby people in the applet is realized, and the cloud development database is used to realize nearby people and friends within a distance of the neighborhood
- Locating memory leaks with poolmon
猜你喜欢

011_ Cascader cascade selector

What is the new generation cloud computing architecture cipu of Alibaba cloud?

少儿编程课程改革后的培养方式

Leetcode (question 1) - sum of two numbers

Zhang Xiaodan, chief architect of Alibaba cloud hybrid cloud: evolution and development of government enterprise hybrid cloud technology architecture

CTF learning notes 18:iwesec file upload vulnerability-03-content-type filtering bypass

Introduction to the "penetration foundation" cobalt strike Foundation_ Cobalt strike linkage msfconsole

解析90后创客教育的主观积极性

Loss and optimization of linear regression, machine learning to predict house prices
![[leetcode daily question] push domino](/img/81/1c31e97d9a245816514bcf47c92107.jpg)
[leetcode daily question] push domino
随机推荐
Problem: SQL create stored procedure
What is stored in the domain name server? How does the domain name server provide services?
Share 10 creative projects of Gaoxing!
Where is the cheaper domain name? What should I pay attention to when buying a domain name?
Introduction to the "penetration foundation" cobalt strike Foundation_ Cobalt strike linkage msfconsole
The conference assistant hidden in wechat is the best way to work efficiently!
Spirit breath development log (11)
The easyplayer player displays compileerror:webassembly Reason for instance() and its solution
Functional advantages of industrial wireless router
How unity runs code every few frames
When remote, your resolution is lower than a × B. Some items may not be displayed on the screen
LeetCode 1791. Find the central node of the star chart
Pgbouncer lightweight PG connection pool management tool
Use of golang testing framework test
Qiming cloud sharing: tips on esp32c3 simple IO and serial port
Bi-sql where
CTF learning notes 18:iwesec file upload vulnerability-03-content-type filtering bypass
There are many ways to confirm and modify the remote port number
3 minutes to understand JSON schema
Where to buy domain name space? Can the domain name be repeated?