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

Leetcode刷题笔记1 图论part03

卡码网 101 孤岛总面积

from collections import deque
directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]
count = 0def main():global countn, m = map(int, input().split())grid = []for _ in range(n):grid.append(list(map(int, input().split())))for i in range(n):if grid[i][0] == 1:dfs(grid, i, 0)if grid[i][m - 1] == 1:dfs(grid, i, m - 1)for j in range(m):if grid[0][j] == 1:dfs(grid, 0, j)if grid[n - 1][j] == 1:dfs(grid, n - 1, j)count = 0for i in range(n):for j in range(m):if grid[i][j] == 1:dfs(grid, i, j)print(count)def dfs(grid, i, j):que = deque()global countque.append((i, j))grid[i][j] = 0count += 1while que:cur_x, cur_y = que.popleft()for x, y in directions:next_x, next_y = cur_x + x, cur_y + yif next_x < 0 or next_y < 0 or next_x >= len(grid) or next_y >= len(grid[0]):continueif grid[next_x][next_y] == 1:count += 1que.append((next_x, next_y))grid[next_x][next_y] = 0if __name__ == '__main__':main()

卡码网 102 沉没孤岛

from collections import dequen, m = list(map(int, input().split()))
g = []
for _ in range(n):row = list(map(int,input().split()))g.append(row)directions = [(1,0),(-1,0),(0,1),(0,-1)]
count = 0def bfs(r,c,mode):global count q = deque()q.append((r,c))count += 1while q:r, c = q.popleft()if mode:g[r][c] = 2for di in directions:next_r = r + di[0]next_c = c + di[1]if next_c < 0 or next_c >= m or next_r < 0 or next_r >= n:continueif g[next_r][next_c] == 1:q.append((next_r,next_c))if mode:g[r][c] = 2count += 1for i in range(n):if g[i][0] == 1: bfs(i,0,True)if g[i][m-1] == 1: bfs(i, m-1,True)for j in range(m):if g[0][j] == 1: bfs(0,j,1)if g[n-1][j] == 1: bfs(n-1,j,1)for i in range(n):for j in range(m):if g[i][j] == 2:g[i][j] = 1else:g[i][j] = 0for row in g:print(" ".join(map(str, row)))

卡码网 103 水流问题

first = set()
second = set()
directions = [[0, 1], [1, 0], [0 ,-1], [-1, 0]]
def dfs(i, j, grid, visited, side):if visited[i][j]:returnvisited[i][j] = Trueside.add((i, j))for x, y in directions:new_x = i + x new_y = j + yif (0 <= new_x < len(grid)and 0 <= new_y < len(grid[0])and int(grid[new_x][new_y]) >= int(grid[i][j])):dfs(new_x, new_y, grid, visited, side)def main():global firstglobal secondn, m = map(int, input().split())grid = []for _ in range(n):grid.append(list(map(int, input().strip().split())))visited = [[False] * m for _ in range(n)]for i in range(m):dfs(0, i, grid, visited, first)for i in range(n):dfs(i, 0, grid, visited, first)visited = [[False] * m for _ in range(n)]for i in range(m):dfs(n - 1, i, grid, visited, second)for i in range(n):dfs(i, m - 1, grid, visited, second)res = first & secondfor x, y in res:print(f"{x} {y}")if __name__ == '__main__':main()

卡码网 104 建造最大岛屿

from typing import List 
from collections import defaultdictclass Solution():def __init__(self):self.direction = [[0, 1], [1, 0], [0, -1], [-1, 0]]self.res = 0self.count = 0self.idx = 1self.count_area = defaultdict(int)def max_area_island(self, grid: List[List[int]]) -> int:if not grid or len(grid) == 0 or len(grid[0]) == 0:return 0for i in range(len(grid)):for j in range(len(grid[0])):if grid[i][j] == 1:self.count = 0self.idx += 1self.dfs(grid, i, j)self.check_area(grid)if self.check_largest_connnect_island(grid):return self.res + 1return max(self.count_area.values())def dfs(self, grid, x, y):grid[x][y] = self.idxself.count += 1for i, j in self.direction:next_x = x + inext_y = y + jif 0 <= next_x < len(grid) and 0 <= next_y < len(grid[0]) and grid[next_x][next_y] == 1:self.dfs(grid, next_x, next_y)returndef check_area(self, grid):for i in range(len(grid)):for j in range(len(grid[0])):self.count_area[grid[i][j]] = self.count_area.get(grid[i][j], 0) + 1returndef check_largest_connnect_island(self, grid):m, n = len(grid), len(grid[0])has_connect = Falsefor i in range(m):for j in range(n):if grid[i][j] == 0:has_connect = Truearea = 0visited = set()for x, y in self.direction:next_x = x + inext_y = y + jif 0 <= next_x < len(grid) and 0 <= next_y < len(grid[0]) and grid[next_x][next_y] != 0 and grid[next_x][next_y] not in visited:visited.add(grid[next_x][next_y])area += self.count_area[grid[next_x][next_y]]self.res = max(self.res, area)return has_connectdef main():n, m = map(int, input().split())grid = []for _ in range(n):grid.append(list(map(int, input().strip().split())))sol = Solution()print(sol.max_area_island(grid))
if __name__ == '__main__':main()

相关文章:

Leetcode刷题笔记1 图论part03

卡码网 101 孤岛总面积 from collections import deque directions [[0, 1], [1, 0], [0, -1], [-1, 0]] count 0def main():global countn, m map(int, input().split())grid []for _ in range(n):grid.append(list(map(int, input().split())))for i in range(n):if gri…...

【模拟面试】计算机考研复试集训(第十一天)

文章目录 前言一、专业面试1、什么是面向对象编程&#xff1f;2、软件工程的主要模型有哪些&#xff1f;3、Cache和寄存器的区别4、卷积层有哪些参数&#xff0c;它们代表什么&#xff1f;5、你有读博的打算吗&#xff1f;6、你的师兄/姐临近毕业&#xff0c;仍做不出成果&…...

查看自己的公有ip

IP 地址 112.3.88.1** 是一个 公有 IP 地址&#xff0c;而不是私有 IP 地址。 公有 IP 地址 vs 私有 IP 地址 公有 IP 地址: 用于在互联网上唯一标识设备。由互联网服务提供商&#xff08;ISP&#xff09;分配。可以在全球范围内路由和访问。例如&#xff1a;112.3.88.156、8.8…...

【js逆向入门】图灵爬虫练习平台 第九题

地址&#xff1a;aHR0cHM6Ly9zdHUudHVsaW5ncHl0b24uY24vcHJvYmxlbS1kZXRhaWwvOS8 f12进入了debugger&#xff0c;右击选择一律不在此处暂停&#xff0c; 点击继续执行 查看请求信息 查看载荷&#xff0c;2个加密参数&#xff0c;m和tt 查看启动器&#xff0c;打上断点 进来 往…...

NET6 WebApi第5讲:中间件(源码理解,俄罗斯套娃怎么来的?);Web 服务器 (Nginx / IIS / Kestrel)、WSL、SSL/TSL

一、NET6的启动流程 区别&#xff1a; .NET6 WebApi第1讲&#xff1a;VSCode开发.NET项目、区别.NET5框架【两个框架启动流程详解】_vscode webapi-CSDN博客 2、WebApplicationBuilder&#xff1a;是NET6引入的一个类&#xff0c;是建造者模式的典型应用 1>建造者模式的…...

Nginx及前端部署全流程:初始化配置到生产环境部署(附Nginx常用命令)

nginx&前端从初始化配置到部署&#xff08;xshell&#xff09; 前言下载nginx前端打包与创建具体文件夹路径配置nginx.nginx.conf文件配置项内容 配置nginx.service文件配置项内容 启动nginx常用nginx命令 前言 目标&#xff1a;在xshell中部署前端包。 第一步&#xff1a…...

python 实现一个简单的window 任务管理器

import tkinter as tk from tkinter import ttk import psutil# 运行此代码前&#xff0c;请确保已经安装了 psutil 库&#xff0c;可以使用 pip install psutil 进行安装。 # 由于获取进程信息可能会受到权限限制&#xff0c;某些进程的信息可能无法获取&#xff0c;代码中已经…...

【AI模型】深度解析:DeepSeek的联网搜索的实现原理与认知误区

一、大模型的“联网魔法”&#xff1a;原来你是这样上网的&#xff01; 在人工智能这个舞台上&#xff0c;大模型们可是妥妥的明星。像DeepSeek、QWen这些大模型&#xff0c;个个都是知识渊博的“学霸”&#xff0c;推理、生成文本那叫一个厉害。不过&#xff0c;要是论起上网…...

【xiaozhi赎回之路-2:语音可以自己配置就是用GPT本地API】

固件作用 打通了网络和硬件的沟通 修改固件实现【改变连接到小智服务器的】 回答逻辑LLM自定义 自定义了Coze&#xff08;比较高级&#xff0c;自定义程度比较高&#xff0c;包括知识库&#xff0c;虚拟脚色-恋人-雅思老师-娃娃玩具{可能需要使用显卡对开源模型进行微调-产…...

WX小程序

下载 package com.sky.utils;import com.alibaba.fastjson.JSONObject; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.Cl…...

JavaScript案例0322

以下是一些涵盖不同高级JavaScript概念和应用的案例&#xff0c;每个案例都有详细解释&#xff1a; 案例1&#xff1a;实现 Promise/A 规范的手写 Promise class MyPromise {constructor(executor) {this.state pending;this.value undefined;this.reason undefined;this.o…...

Spring boot 3.4 后 SDK 升级,暨 UI API/MCP 计划

PS 写这篇文章后看到 A Deep Dive Into MCP and the Future of AI Tooling | Andreessen HorowitzWe explore what MCP is, how it changes the way AI interacts with tools, what developers are already building, and the challenges that still need solving. https://a1…...

大数据学习(78)-spark streaming与flink

&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4dd;支持一…...

2.企业级AD活动目录架构与设计原则实战指南

一、企业级AD架构核心组件解析 1.1 多域森林架构设计 核心概念&#xff1a; 单域模型&#xff1a;适用于中小型企业&#xff08;<5万用户&#xff09; 多域模型&#xff1a;满足跨国/多部门隔离需求 森林&#xff08;Forest&#xff09;&#xff1a;安全信任边界&#x…...

Linux下JDK1.8安装配置

目录 1.下载完上传到Linux系统中 2.解压JDK压缩包 3.配置JDK环境变量 4.设置环境变量生效 5.查看环境变量是否配置成功 官网下载地址:Java Downloads | Oracle 1.下载完上传到Linux系统中 2.解压JDK压缩包 tar -zxvf jdk-8u151-linux-x64.tar.gz -C /usr/local (解压…...

Python OCR文本识别详细步骤及代码示例

光学字符识别&#xff08;OCR&#xff09;是将图像中的文字转换为可编辑文本的技术。在Python中&#xff0c;我们可以利用多种库实现OCR功能。本文将详细介绍使用Tesseract和EasyOCR进行文本识别的步骤&#xff0c;并提供完整的代码示例。 一、OCR简介 OCR&#xff08;Optical…...

OpenCV 基础模块 Python 版

OpenCV 基础模块权威指南&#xff08;Python 版&#xff09; 一、模块全景图 plaintext OpenCV 架构 (v4.x) ├─ 核心层 │ ├─ core&#xff1a;基础数据结构与操作&#xff08;Mat/Scalar/Point&#xff09; │ └─ imgproc&#xff1a;图像处理流水线&#xff08;滤…...

华为HCIE网络工程师培训选机构攻略

从 官方授权机构 到 性价比黑马&#xff0c;结合价格、师资、通过率等维度&#xff0c;为你筛选出最适合的培训方案。 一、华为官方授权机构&#xff08;优先推荐&#xff09; 华为官方授权机构拥有 真机实验环境考官级讲师&#xff0c;适合预算充足、追求高通过率的学员。 机…...

Linux固定IP方法(RedHat+Net模式)

1、查看当前网关 ip route | grep default 2、配置静态IP 双击重启 3、验证...

210、【图论】课程表(Python)

题目 思路 这道题本质上是一个拓扑排序。每次先统计每个点的入度个数、然后再统计点与点之间的邻接关系&#xff0c;找到入度为0的点作为起始遍历点。之后每遍历到这个点之后&#xff0c;就把这个点后续的邻接关系边的点入度减去一。当某个点入度为0时&#xff0c;继续被加入其…...

使用Python开发自动驾驶技术:车道线检测模型

友友们好! 我是Echo_Wish,我的的新专栏《Python进阶》以及《Python!实战!》正式启动啦!这是专为那些渴望提升Python技能的朋友们量身打造的专栏,无论你是已经有一定基础的开发者,还是希望深入挖掘Python潜力的爱好者,这里都将是你不可错过的宝藏。 在这个专栏中,你将会…...

跟着StatQuest学知识07-张量与PyTorch

一、张量tensor 张量重新命名一些数据概念&#xff0c;存储数据以及权重和偏置。 张量还允许与数据相关的数学计算能够相对快速的完成。 通常&#xff0c;张量及其进行的数学计算会通过成为图形处理单元&#xff08;GPUs&#xff09;的特殊芯片来加速。但还有张量处理单元&am…...

nginx配置https域名后,代理后端服务器流式接口变慢

目录 问题描述原因解决办法 问题描述 使用nginx配置域名和https的ssl证书后&#xff0c;代理后端web服务器&#xff0c;发现流式接口比原来直接用服务器外部ip后端web服务器端口变慢了很多。 原因 在于 HTTP 和 HTTPS 在 Nginx 代理中的处理方式不同。以下几点解释了为什么 …...

前端字段名和后端不一致?解锁 JSON 映射的“隐藏规则” !!!

&#x1f680; 前端字段名和后端不一致&#xff1f;解锁 JSON 映射的“隐藏规则” &#x1f31f; 嘿&#xff0c;技术冒险家们&#xff01;&#x1f44b; 今天我们要聊一个开发中常见的“坑”&#xff1a;前端传来的 JSON 参数字段名和后端对象字段名不一致&#xff0c;会发生…...

基于springboot的新闻推荐系统(045)

摘要 随着信息互联网购物的飞速发展&#xff0c;国内放开了自媒体的政策&#xff0c;一般企业都开始开发属于自己内容分发平台的网站。本文介绍了新闻推荐系统的开发全过程。通过分析企业对于新闻推荐系统的需求&#xff0c;创建了一个计算机管理新闻推荐系统的方案。文章介绍了…...

2024年数维杯数学建模C题天然气水合物资源量评价解题全过程论文及程序

2024年数维杯数学建模 C题 天然气水合物资源量评价 原题再现&#xff1a; 天然气水合物&#xff08;Natural Gas Hydrate/Gas Hydrate&#xff09;即可燃冰&#xff0c;是天然气与水在高压低温条件下形成的类冰状结晶物质&#xff0c;因其外观像冰&#xff0c;遇火即燃&#…...

Linux与HTTP中的Cookie和Session

HTTP中的Cookie和Session 本篇介绍 前面几篇已经基本介绍了HTTP协议的大部分内容&#xff0c;但是前面提到了一点「HTTP是无连接、无状态的协议」&#xff0c;那么到底有什么无连接以及什么是无状态。基于这两个问题&#xff0c;随后解释什么是Cookie和Session&#xff0c;以…...

linux 备份工具,常用的Linux备份工具及其备份数据的语法

在Linux系统中&#xff0c;备份数据是确保数据安全性和完整性的关键步骤。以下是一些常用的Linux备份工具及其备份数据的语法&#xff1a; 1. tar命令 tar命令是Linux系统中常用的打包和压缩工具&#xff0c;可以将多个文件或目录打包成一个文件&#xff0c;并可以选择添加压…...

C++核心语法快速整理

前言 欢迎来到我的博客 个人主页:北岭敲键盘的荒漠猫-CSDN博客 本文主要为学过多门语言玩家快速入门C 没有基础的就放弃吧。 全部都是精华&#xff0c;看完能直接上手改别人的项目。 输出内容 std::代表了这里的cout使用的标准库&#xff0c;避免不同库中的相同命名导致混乱 …...

STM32八股【3】------RAM和片上FLASH

1、RAM和FLASH构成 1.RAM ┌──────────────────────────┐ │ 栈区 (Stack) │ ← 从RAM顶端向下扩展&#xff08;存储局部变量、函数调用信息&#xff09; │--------------------------│ │ 堆区 (Heap) │ ← …...