尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
元宝    LeetCode 675. Cut Off Trees for Golf Event Python3 Implement
Approach: Sort by Height BFS Between Consecutive TreesThe rules force a fixed order: trees must be cut shortest to tallest. So the walk decomposes into independent legs — go from your current cell to the next required tree by the shortest path, pay that distance, repeat. Shortest path on an unweighted grid with obstacles is BFS. If any leg is unreachable (a tree walled off by0s), return“-1”.from collections import dequefrom typing import Listclass Solution:def cutOffTree(self, forest: List[List[int]]) - int:if not forest or not forest[0]:return -1m, n len(forest), len(forest[0])def bfs(sr: int, sc: int, tr: int, tc: int) - int: Shortest step count from (sr,sc) to (tr,tc); -1 if unreachable. if sr tr and sc tc: return 0 seen [[False] * n for _ in range(m)] seen[sr][sc] True q deque([(sr, sc, 0)]) # (row, col, dist) while q: r, c, d q.popleft() for nr, nc in ((r 1, c), (r - 1, c), (r, c 1), (r, c - 1)): if 0 nr m and 0 nc n and not seen[nr][nc] and forest[nr][nc] 0: if nr tr and nc tc: return d 1 # early exit on hit seen[nr][nc] True # mark on enqueue q.append((nr, nc, d 1)) return -1 # Height 1 means a tree; cut in ascending height order. trees sorted((forest[r][c], r, c) for r in range(m) for c in range(n) if forest[r][c] 1) total row col 0 for _, tr, tc in trees: d bfs(row, col, tr, tc) if d -1: return -1 total d row, col tr, tc return totalKey detailsHeight“1” is walkable ground, not a tree. Only“ 1” enters the“trees” list — the classic bug here is treating“1” as a tree.“forest[nr][nc] 0” is the walkability test:“0” is blocked, everything else is passable (including trees you haven’t cut yet — you may walk over them).Mark“seen” on enqueue, not dequeue, or the same cell gets pushed repeatedly.Distance rides in the queue tuple, so no“dist[][]” grid per leg.Early return the moment the target is generated as a neighbor; no need to drain the queue.Sorting is by height only — heights are guaranteed distinct, so no tie-break needed.ComplexityTime: O(T · m · n) for T trees (one BFS per leg), plus O(T log T) to sort.Space: O(m · n) for“seen” and the queue, rebuilt per leg.Verified against the official cases:“[[1,2,3],[0,0,4],[7,6,5]] → 6”,“[[1,2,3],[0,0,0],[7,6,5]] → -1”,“[[2,3,4],[0,0,5],[8,7,6]] → 6”, and the large 4×4 case →“46”.
RELATED

相关推荐

3D视觉Infra核心痛点与落地实践:从数据采集到线上监控

3D视觉Infra核心痛点与落地实践:从数据采集到线上监控

我是在一个自动化设备项目里,第一次被“3D视觉Infra”这个词击中。当时团队里算法、工程、产品各干各的,模型训练出来效果也不差,可一到现场测试就翻车。后来排查发现,问题根本不在模型,而在于数据采集时的标定乱了、标…

📅 2026/10/19 19:05:49
(论文速读)Sonata:摆脱几何捷径,学习可靠的点云自监督表征

(论文速读)Sonata:摆脱几何捷径,学习可靠的点云自监督表征

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📅 2026/10/19 19:05:49
Node-RED队列节流实战:用queue-gate解决消息丢包、乱序与雪崩

Node-RED队列节流实战:用queue-gate解决消息丢包、乱序与雪崩

简介:这是一份面向Node-RED开发者与IoT系统集成工程师的实用扩展节点资源,用于精细化控制消息流并实现带缓冲能力的逻辑门控。该节点弥补了标准gate节点缺乏队列管理的短板,支持open/closed/queueing三种状态切换,可按序释放、预览…

📅 2026/10/19 19:05:49
MORE NEWS

更多资讯

📰

Python+大模型+微博舆情分析:从数据采集到可视化全链路实战指南

每年到毕业设计季,“微博舆情分析”这个方向都会被大量同学翻牌,尤其是加上Python和大模型之后,听起来既有技术含量又紧跟热点。但真正动手之后很多人会发现:微博数据怎么拿、情感分析怎么做准、大模型怎么接、预测结果怎么才能不…

📰

浅层模型与深度神经网络的选型之道:从特征工程到工程成本的全面对比

做算法这些年,我参加过的技术选型会里,争论最热烈的永远是同一个话题:传统机器学习(浅层模型)和深度学习(深度神经网络)到底该选哪个。上周项目评审,我提的方案是先上XGBoost搭基线&…

📰

Qt通过COM操作Word生成文档:完整实战指南

1. 为什么项目里总会冒出"Qt输出Word"的需求做了几年Qt桌面端开发,你会发现一个规律:凡是涉及业务系统、办公自动化的项目,十有八九会碰到"在程序里生成Word文档"这种需求。比如项目里要定期输出月度报告、按模板生成合同…

📰

Spring Boot四层XSS防御实战:过滤器+包装器+CSP+输出编码

我处理项目里那个"搜索框变成按钮"的Bug,才真正理解XSS不是文档里的一句名词,而是能在几分钟内把Cookie、页面内容、用户操作全部劫走的入口。当时用Spring Boot做的后台管理系统,前端同学随手往标题栏输入了一段带尖括号的内容&am…

📰

U2-Flash 1M上下文实测:从能塞到能用的工程实践

1. 当"1M上下文"不再是PPT上的数字第一次看到"1M上下文"这个说法,我的反应是:又一个拿理论值说事的营销话术。做过大模型应用的人都知道,上下文窗口标称值和实际可用值之间,往往隔着一道叫"注意力衰减&q…

📰

双麦克风语音分离:基于GCC-PHAT与IVA的轻量级实时声源解耦方案

简介:本资源是一套基于MATLAB实现的双麦克风语音分离完整源码工程,面向音频信号处理初学者、语音算法研究者及高校相关课程实践者,聚焦于利用TDOA与空间滤波技术从混合语音中分离目标声源,适用于噪声抑制、会议系统增强与语音识别…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬