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): ...
随机推荐
- centos7开放8080端口
1. firewall-cmd --state :令防火墙处于开启状态 systemctl start firewalld.service: 2. firewall-cmd --zone=publi ...
- vue-print-nb的应用
1.cnpm i vue-print-nb 2.触发事件:v-print="printObj" 3.触发的是个对象: printObj:{ id: ...
- Java笔记第八弹
设置和获取线程名称 //方法 void setName(String name);//将此线程的名称更改为等于参数name String getName();//返回此线程的名称 public sta ...
- 一文学会Flex布局
参考: <CSS权威指南>(第四版) flex布局教程-语法篇-阮一峰 1.Flex布局是什么 FlexBox,弹性盒布局,顾名思义,就是元素具有弹性,能根据可用空间大小增减尺寸. 2.基 ...
- Java面试——架构设计与分布式
更多内容,移步 IT-BLOG 一.用 Java 自己实现一个 LRU LRU(Least Recently Used:最近最少使用):简单的说,就是保证基本的 Cache容量,如果超过容量则必须丢掉 ...
- Java面试——数据库
一.数据库隔离级别 [1]Read Uncommitted(读取未提交内容):出现脏读,也就是可能读取到其他会话中未提交事务修改的数据.[2]Read Committed(读取已提交内容):不可重复读 ...
- 如何提取 x64 程序那些易失的方法参数
一:背景 1. 讲故事 最近经常遇到有朋友反馈,在 x64 环境下如何提取线程栈中的方法参数,熟悉 x64 调用协定的朋友应该知道,这种协定范围下,方法的前四个参数都是用寄存器传递的,比如rcx,rd ...
- 集合-HashMap 源码详细分析(JDK1.8)
1. 概述 本篇文章我们来聊聊大家日常开发中常用的一个集合类 - HashMap.HashMap 最早出现在 JDK 1.2中,底层基于散列算法实现.HashMap 允许 null 键和 null 值 ...
- Unity3D中的Attribute详解(三)
上一篇我们对系统的Attributes进行了MSIL代码的查看,了解到了其本质就是一个类的构造函数.本章我们将编写自己的Attributes. 首先我们定义书的属性代码,如下: [AttributeU ...
- keepalived的简单使用
原理简述 本篇主要学习keepalived配合nginx实现nginx的高可用, 也就是需要keepalived检测到nginx宕机时停用keepalived, 备用keepalived会自动接收过来 ...