当前位置: 首页 > news >正文

leetcode算法题--找出最安全路径

原题链接:https://leetcode.cn/problems/find-the-safest-path-in-a-grid/description/

func maximumSafenessFactor(grid [][]int) int {n := len(grid)type pair struct {x inty int}p := make([]pair, 0)dis := make([][]int, n)for i := range dis {dis[i] = make([]int, n)for j := range dis[i] {if grid[i][j] == 1 {p = append(p, pair{i, j}) } else {dis[i][j] = -1}}}dirs := [][]int{{0, -1}, {1, 0}, {0, 1}, {-1, 0}};groups := [][]pair{p}for len(p) != 0 {tmp := pp = make([]pair, 0) for _, pa := range tmp {for _, dir := range dirs {i, j := pa.x, pa.yx := i + dir[0] y := j + dir[1]if x >= 0 && x < n && y >= 0 && y < n && dis[x][y] < 0 {p = append(p, pair{x, y})dis[x][y] = len(groups)}} } groups = append(groups, p)}// 并查集m := n * n - 1 fa := make([]int, m + 1)for i := 0; i <= m; i ++ {fa[i] = i}var find func(x int) intfind = func(x int) int {if fa[x] != x {fa[x] = find(fa[x])}return fa[x]}nn := len(groups) for i := nn - 2; i > 0; i -- {pairs := groups[i] for _, pair := range pairs {for _, dir := range dirs {i, j := pair.x, pair.yx := i + dir[0] y := j + dir[1]if x >= 0 && x < n && y >= 0 && y < n && dis[x][y] >= dis[i][j] {fa[find(x*n+y)] = find(i*n+j)}} if find(0) == find(m) {return i }}} return 0 
}
http://www.lryc.cn/news/126748.html

相关文章:

  • 神经网络基础-神经网络补充概念-34-正则化
  • idea打jar包
  • 民安汇智(第三方旅游服务暗访)开展旅游景区度假区明察暗访复核检查服务
  • 《游戏编程模式》学习笔记(六)单例模式 Singleton Pattern
  • 《Go 语言第一课》课程学习笔记(二)
  • 神经网络基础-神经网络补充概念-26-前向和反向传播
  • Gin路由组
  • 安防监控视频云存储平台EasyNVR通道频繁离线的原因排查与解决
  • Redis-分布式锁!
  • Unity如何把游戏导出成手机安装包
  • 使用爱校对软件保证公文材料质量的关键步骤
  • Spring Data Elasticsearch 的简单使用
  • 2024」预备研究生mem-角平分线定理中线定理垂线定理、射影定理
  • nginx部署时http接口正常,ws接口404
  • 数学建模的概念和学习方法(什么是数学建模)
  • ChatGPT在智能安全监测和入侵检测中的应用如何?
  • 智能数据建模软件DTEmpower 2023R2新版本功能介绍
  • BDA初级分析——认识SQL,认识基础语法
  • Qt应用开发(基础篇)——MDI窗口 QMdiArea QMdiSubWindow
  • 图片转换成pdf格式?这几种转换格式方法了解一下
  • thingsboard编译安装踩坑记录
  • 汇编语言例子集合
  • 强化学习:用Python训练一个简单的机器人
  • 【Docker】Docker使用之容器技术发展史
  • postgresql的在windows下的安装
  • python 自动化学习(四) pyppeteer 浏览器操作自动化
  • P1009 阶乘之和
  • Linux内核源码剖析之TCP保活机制(KeepAlive)
  • 后端 springboot 给 vue 提供参数
  • 《vue3实战》运用radio单选按钮或Checkbox复选框实现单选多选的试卷制作