2021-08-20:打砖块。有一个 m x n 的二元网格,其中 1 表示砖块,0 表示空白。砖块 稳定(不会掉落)的前提是:1.一块砖直接连接到网格的顶部,或者,2.至少有一块相邻(4 个方向之一
2021-08-20:打砖块。有一个 m x n 的二元网格,其中 1 表示砖块,0 表示空白。砖块 稳定(不会掉落)的前提是:1.一块砖直接连接到网格的顶部,或者,2.至少有一块相邻(4 个方向之一)砖块 稳定 不会掉落时。给你一个数组 hits ,这是需要依次消除砖块的位置。每当消除 hits[i] = (rowi, coli) 位置上的砖块时,对应位置的砖块(若存在)会消失,然后其他的砖块可能因为这一消除操作而掉落。一旦砖块掉落,它会立即从网格中消失(即,它不会落在其他稳定的砖块上)。返回一个数组 result ,其中 result[i] 表示第 i 次消除操作对应掉落的砖块数目。注意,消除可能指向是没有砖块的空白位置,如果发生这种情况,则没有砖块掉落。
福大大 答案2021-08-20:
并查集。逆向思维。
代码用golang编写。代码如下:
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
}
// 并查集
type UnionFind struct {
N int
M int
// 有多少块砖,连到了天花板上
cellingAll int
// 原始矩阵,因为炮弹的影响,1 -> 2
grid [][]int
// cellingSet[i] = true; i 是头节点,所在的集合是天花板集合
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
}
}
执行结果如下:

2021-08-20:打砖块。有一个 m x n 的二元网格,其中 1 表示砖块,0 表示空白。砖块 稳定(不会掉落)的前提是:1.一块砖直接连接到网格的顶部,或者,2.至少有一块相邻(4 个方向之一的更多相关文章
- 2021.08.30 前缀函数和KMP
2021.08.30 前缀函数和KMP KMP算法详解-彻底清楚了(转载+部分原创) - sofu6 - 博客园 (cnblogs.com) KMP算法next数组的一种理解思路 - 挠到头秃 - 博 ...
- 2021.08.16 P1260 工程规划(差分约束)
2021.08.16 P1260 工程规划(差分约束) 重点: 1.跑最短路是为了满足更多约束条件. P1260 工程规划 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 造 ...
- 2021.08.16 P1078 文化之旅(最短路)
2021.08.16 P1078 文化之旅(最短路) 题意: n个地,k个信仰,每个地都有自己的信仰,信仰之间会相互排斥,同信仰之间也会相互排斥,有m条路,问从s到t的最短距离是多少? 有一位使者要游 ...
- 2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意)
2021.08.16 P1363 幻象迷宫(dfs,我感受到了出题人浓浓的恶意) P1363 幻象迷宫 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 幻象迷宫可以认为是无限 ...
- 2021.08.09 P4868 Preprefix sum(树状数组)
2021.08.09 P4868 Preprefix sum(树状数组) P4868 Preprefix sum - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 前缀和(pr ...
- 2021.08.09 P6037 Ryoku的探索(基环树)
2021.08.09 P6037 Ryoku的探索(基环树) P6037 Ryoku 的探索 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.树的性质 2.基环树的性质 ...
- 2021.08.09 P6225 抑或橙子(树状数组)
2021.08.09 P6225 抑或橙子(树状数组) 重点: 1.异或用法 题意: Janez 喜欢橙子!他制造了一个橙子扫描仪,但是这个扫描仪对于扫描的每个橙子的图像只能输出一个 3232 位整数 ...
- 2021.08.06 P4392 Sound静音问题(ST表)
2021.08.06 P4392 Sound静音问题(ST表) [P4392 BOI2007]Sound 静音问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 序列a,求 ...
- 2021.08.06 P3478 STA-Station(树形结构)
2021.08.06 P3478 STA-Station(树形结构) [P3478 POI2008]STA-Station - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 给 ...
- 2021.08.05 P5357 康托展开模板(康托展开)
2021.08.05 P5357 康托展开模板(康托展开) P5367 [模板]康托展开 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 重点: 1.康托展开 算法学习笔记(56): ...
随机推荐
- Hadoop-HA节点介绍
设计思想 hadoop2.x启用了主备节点切换模式(1主1备) 当主节点出现异常的时候,集群直接将备用节点切换成主节点 要求备用节点马上就要工作 主备节点内存几乎同步 有独立的线程对主备节点进行监控健 ...
- PHP 文件和文件夹操作
文件夹操作 创建文件夹 mkdir(名称,权限,递归创建):创建文件 例如: #创建文件夹 mkdir('./aa') # 创建 aa 文件夹 mkdir('./aa/bb') # 在 aa 目录下创 ...
- vulnhub靶场之MOMENTUM: 1
准备: 攻击机:虚拟机kali.本机win10. 靶机:Momentum: 1,下载地址:https://download.vulnhub.com/momentum/Momentum.ova,下载后直 ...
- 如何自动化测试你的接口?—— Rest Assured
前言 不知道大家的项目是否都有对接口API进行自动化测试,反正像我们这种小公司是没有的.由于最近一直被吐槽项目质量糟糕,只能研发自己看看有什么接口测试方案.那么在本文中,我将探索如何使用 Rest A ...
- 5分钟带你彻底搞懂async底层实现原理!
ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是什么?一句话,它就是 Generator 函数的语法糖.研究 async 的原理,就必须先弄清楚 Genera ...
- Win11右键菜单改回传统样式
Win11右键菜单,比较不人性化,隐藏了一些常用选项,需要点"更多选项"才能显示,多次一举. 解决方法,一句话: reg.exe add "HKCU\Software\C ...
- 使用 screw(螺丝钉) 快速生成数据库文档
一.框架介绍 回想起那个去年的7月份,第一份实习,组长让我写一份金蝶云的SQL文档,当时一看2000多张表,当时就猛吸一根烟,然后去gitee看看有没有好的框架快速生成 SQL 文档 ,由此找到了 s ...
- HTTP协议初见
HTTP协议 四大特性 基于请求端响应 客户端发送请求,服务端才响应,服务端不会主动给客户端发送响应. 基于TCP/IP作用于应用层之上的协议 此协议属于应用层 无状态 服务端不会保存客户 ...
- python入门教程之五数据结构
变量 Python 变量类型 变量存储在内存中的值,这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同 ...
- [Java]算法练习:新农村建设
1 题目描述 from 网友 CASE1 输入 A1 A8 输出 [A1,A2,A3,A4,A5,A6,A7,A8] CASE2 输入 A1 K1 输出 [A1,B1,C1,D1,E1,F1,G1,H ...