2022-05-17:在一个地图上有若干个炸弹,每个炸弹会呈现十字型引爆。 每个炸弹都有其当量值,这个值决定了这个炸弹的爆炸半径。 如果一个炸弹被引爆时,有其它炸弹在其爆炸半径内,那么其它炸弹也会爆炸
2022-05-17:在一个地图上有若干个炸弹,每个炸弹会呈现十字型引爆。
每个炸弹都有其当量值,这个值决定了这个炸弹的爆炸半径。
如果一个炸弹被引爆时,有其它炸弹在其爆炸半径内,那么其它炸弹也会爆炸。
请问使地图上所有炸弹爆炸所需的最少人为引爆次数。
例如:
0,0,0,0,0
0,0,0,1,0
0,0,0,0,0
上图中val为1的单元是一个炸弹,人为引爆后地图变成下面的样子:
0, 0, 0,-1, 0
0, 0,-1,-1,-1
0, 0, 0,-1, 0
题目并没有给数据量,面经题目的通病。
来自亚马逊。
答案2022-05-17:
并查集不对。
贪心最大当量不对。
贪心最多不对。
有序表 + 强连通分量。
代码用golang编写。代码如下:
package mainimport ("fmt""sort"
)func main() {map0 := [][]int{{0, 0, 2, 0, 2},{1, 0, 0, 0, 0},{0, 0, 1, 0, 2},{0, 0, 1, 0, 0},{0, 0, 1, 0, 0},}ans := minBombs2(map0)fmt.Println(ans)
}// 正式方法
// 用到有序表 + 强连通分量
func minBombs2(map0 [][]int) int {n := len(map0)rowTreeMaps := make([]map[int]int, 0)rowTreeMapsi := make([][]int, 0)colTreeMaps := make([]map[int]int, 0)colTreeMapsi := make([][]int, 0)for i := 0; i < n; i++ {rowTreeMaps = append(rowTreeMaps, make(map[int]int))rowTreeMapsi = append(rowTreeMapsi, make([]int, 0))colTreeMaps = append(colTreeMaps, make(map[int]int))colTreeMapsi = append(colTreeMapsi, make([]int, 0))}m := 0for i := 0; i < n; i++ {for j := 0; j < n; j++ {if map0[i][j] != 0 {m++rowTreeMaps[i][j] = mrowTreeMapsi[i] = append(rowTreeMapsi[i], j)colTreeMaps[j][i] = mcolTreeMapsi[j] = append(colTreeMapsi[j], i)}}}for i := 0; i < n; i++ {sort.Ints(rowTreeMapsi[i])sort.Ints(colTreeMapsi[i])}edges := make([][]int, 0)for i := 0; i <= m; i++ {edges = append(edges, make([]int, 0))}for i := 0; i < n; i++ {for j := 0; j < n; j++ {if map0[i][j] != 0 {rowTreeMap := rowTreeMaps[i]rowTreeMapi := rowTreeMapsi[i]colTreeMap := colTreeMaps[j]colTreeMapi := colTreeMapsi[j]from := rowTreeMap[j]col := j - 1for floorKey(rowTreeMapi, col) != -1 && j-rowTreeMapi[floorKey(rowTreeMapi, col)] <= map0[i][j] {col = rowTreeMapi[floorKey(rowTreeMapi, col)]edges[from] = append(edges[from], rowTreeMap[col])col--}col = j + 1for ceilingKey(rowTreeMapi, col) != -1 && rowTreeMapi[ceilingKey(rowTreeMapi, col)]-j <= map0[i][j] {col = rowTreeMapi[ceilingKey(rowTreeMapi, col)]edges[from] = append(edges[from], rowTreeMap[col])col++}row := i - 1for floorKey(colTreeMapi, row) != -1 && i-colTreeMapi[floorKey(colTreeMapi, row)] <= map0[i][j] {row = colTreeMapi[floorKey(colTreeMapi, row)]edges[from] = append(edges[from], colTreeMap[row])row--}row = i + 1for ceilingKey(colTreeMapi, row) != -1 && colTreeMapi[ceilingKey(colTreeMapi, row)]-i <= map0[i][j] {row = colTreeMapi[ceilingKey(colTreeMapi, row)]edges[from] = append(edges[from], colTreeMap[row])row++}}}}scc := NewStronglyConnectedComponents(edges)sccn := scc.getSccn()in := make([]int, sccn+1)out := make([]int, sccn+1)dag := scc.getShortGraph()for i := 1; i <= sccn; i++ {for _, j := range dag[i] {out[i]++in[j]++}}zeroIn := 0for i := 1; i <= sccn; i++ {if in[i] == 0 {zeroIn++}}return zeroIn
}// 在arr上,找满足>=value的最左位置
func ceilingKey(arr []int, v int) int {L := 0R := len(arr) - 1index := -1 // 记录最左的对号for L <= R {mid := L + (R-L)>>1if arr[mid] >= v {index = midR = mid - 1} else {L = mid + 1}}return index
}// 在arr上,找满足<=value的最右位置
func floorKey(arr []int, v int) int {L := 0R := len(arr) - 1index := -1 // 记录最右的对号for L <= R {mid := L + (R-L)>>1if arr[mid] <= v {index = midL = mid + 1} else {R = mid - 1}}return index
}type StronglyConnectedComponents struct {nexts [][]intn intstack []intstackSize intdfn []intlow []intcnt intscc []intsccn int
}// 请保证点的编号从1开始,不从0开始
// 注意:
// 如果edges里有0、1、2...n这些点,那么容器edges的大小为n+1
// 但是0点是弃而不用的,所以1..n才是有效的点,所以有效大小是n
func NewStronglyConnectedComponents(edges [][]int) *StronglyConnectedComponents {ans := &StronglyConnectedComponents{}ans.nexts = edgesans.init()ans.scc0()return ans
}func (this *StronglyConnectedComponents) init() {this.n = len(this.nexts)this.stack = make([]int, this.n)this.stackSize = 0this.dfn = make([]int, this.n)this.low = make([]int, this.n)this.cnt = 0this.scc = make([]int, this.n)this.sccn = 0this.n--
}func (this *StronglyConnectedComponents) scc0() {for i := 1; i <= this.n; i++ {if this.dfn[i] == 0 {this.tarjan(i)}}
}func (this *StronglyConnectedComponents) tarjan(p int) {this.cnt++this.dfn[p] = this.cntthis.low[p] = this.cntthis.stack[this.stackSize] = pthis.stackSize++for _, q := range this.nexts[p] {if this.dfn[q] == 0 {this.tarjan(q)}if this.scc[q] == 0 {if this.low[p] > this.low[q] {this.low[p] = this.low[q]}}}if this.low[p] == this.dfn[p] {this.sccn++top := 0for {this.stackSize--top = this.stack[this.stackSize]this.scc[top] = this.sccnif top == p {break}}}
}func (this *StronglyConnectedComponents) getScc() []int {return this.scc
}func (this *StronglyConnectedComponents) getSccn() int {return this.sccn
}func (this *StronglyConnectedComponents) getShortGraph() [][]int {shortGraph := make([][]int, 0)for i := 0; i <= this.sccn; i++ {shortGraph = append(shortGraph, make([]int, 0))}for u := 1; u <= this.n; u++ {for _, v := range this.nexts[u] {if this.scc[u] != this.scc[v] {shortGraph[this.scc[u]] = append(shortGraph[this.scc[u]], this.scc[v])}}}return shortGraph
}
执行结果如下:
左神java代码
2022-05-17:在一个地图上有若干个炸弹,每个炸弹会呈现十字型引爆。 每个炸弹都有其当量值,这个值决定了这个炸弹的爆炸半径。 如果一个炸弹被引爆时,有其它炸弹在其爆炸半径内,那么其它炸弹也会爆炸相关推荐
- 百度地图之九如何在一个地图上显示多条导航路线
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 如何在一 ...
- 百度地图之在一个地图上显示多条导航路线
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! pack ...
- centerandzoom 无效_Django调用百度地图api在地图上批量增加标记点
在调用百度地图api进行web开发时遇到了一个需求,我们需要在网页中内嵌一个div 然后在div中调用百度地图的js显示我们所需要的地区.根据需求坐标在地图上添加若干个标记点,并批量的为各个标记点设置 ...
- java百度地图添加标注_调取百度地图接口,实现取自己的实时位置,然后可以在百度地图上添加信息标注...
前几天接了一个小项目,就是有关百度地图的,和我的上一篇不一样,上一篇是复制人家的源代码是把东西写死了的,这次的比较有难度,要调取他的接口,用js来实现. 下面我先说一下主要实现的功能,和要实现的页面 ...
- java echarts 散点图,echarts在地图上绘制散点图(任意点)
项目需求:在省份地图上绘制散点图,散点位置不一定是哪个城市或哪个区县,即任意点 通过查询官网文档,找到一个与需求类似的Demo:https://www.echartsjs.com/gallery/ed ...
- 调取百度地图接口,实现取自己的实时位置,然后可以在百度地图上添加信息标注...
前几天接了一个小项目,就是有关百度地图的,和我的上一篇不一样,上一篇是复制人家的源代码是把东西写死了的,这次的比较有难度,要调取他的接口,用js来实现. 下面我先说一下主要实现的功能,和要实现的页面 ...
- python通过经纬度在地图标点_只要两步,用Python将地址标记在地图上!
本文就将讲解,给你一个地址,如何用Python进行可视化,只需要两步:将地址转成经纬度 根据经纬度在地图上标记点 一.将地址转成经纬度 首先我们需要将地理位置转成经纬度这种统一格式,方便代码去识别.完 ...
- 只要两步,用Python将地址标记在地图上!
大家好,在之前的大众点评一线快餐品牌分析文章中,很多读者私信对如何将商家地址标记到地图上感兴趣???? 本文就将讲解,给你一个地址,如何用Python进行可视化,只需要两步: 将地址转成经纬度 根据经 ...
- python 地址_如何用用Python将地址标记在地图上
本文就将讲解,给你一个地址,如何用Python进行可视化,只需要两步: 将地址转成经纬度 根据经纬度在地图上标记点 一.将地址转成经纬度 首先我们需要将地理位置转成经纬度这种统一格式,方便代码去识别. ...
最新文章
- CentOS5.5支持ntfs文件系统
- mysql chartset utf 8_[数据库]MySQL chartset_星空网
- 《Go语言程序设计》读书笔记(二)函数
- mysql pconnect_mysql_pconnect()
- Pytorch基础语法学习
- linux dd命令实用详解
- NTL密码算法开源库拓展——SM2算法
- HBase 安装配置及下载地址
- Three.js - 加载 .OBJ 格式模型(十六)
- c语言中数组int*,C语言数组
- Selenium GUI自动化下载百度图片
- 关于 RT-Thread 内核调度算法中 __rt_ffs函数为什么要检测 value值是否为0的疑问
- python爬取网易云音乐飙升榜音乐_python爬取网易云音乐热歌榜实例代码
- 【推荐】《Java 并发编程的艺术》迷你书
- Lenovo GTX960M 配置CUDA
- python人脸识别门禁系统生产厂家_人脸识别门禁硬件生产厂家
- 智能家居系统智能联动控制
- SLAM中“camera类中坐标转换方法解读
- linux qq远程桌面连接,还在用QQ远程桌面?这款软件还能用手机控制电脑!
- CST学习笔记4----------初级端口