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

做网站多少钱啊东莞网络推广培训

做网站多少钱啊,东莞网络推广培训,海南智能网站建设报价,做淘宝客进哪个网站#99. 岛屿数量 深度优先搜索: 每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。 本题思路:用遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。在遇到标记过的陆地节点和海洋…

#99. 岛屿数量

深度优先搜索:

每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

本题思路:用遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。

def dfs(grid, visited, x, y):if visited[x][y] or grid[x][y] == 0:return  # 终止条件:访问过的节点 或者 遇到海水visited[x][y] = True  # 标记访问过directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]  # 四个方向(上右下左的顺序)for dx, dy in directions:nextx = x + dxnexty = y + dyif 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]):  # 检查是否越界dfs(grid, visited, nextx, nexty)def main():n, m = map(int, input().split())grid = [list(map(int, input().split())) for _ in range(n)]visited = [[False] * m for _ in range(n)]result = 0for i in range(n):for j in range(m):if not visited[i][j] and grid[i][j] == 1:result += 1  # 遇到没访问过的陆地,+1dfs(grid, visited, i, j)  # 将与其链接的陆地都标记上 Trueprint(result)if __name__ == "__main__":main()

广度优先搜索:

from collections import dequedef bfs(grid, visited, x, y):# 使用deque实现队列queue = deque([(x, y)])visited[x][y] = True  # 加入队列后立即标记为已访问directions = [[0,1],[1,0],[0,-1],[-1,0]]while queue:curx, cury = queue.popleft()  # 从队列中取出当前位置for dx, dy in directions:  # 遍历四个方向nextx, nexty = curx + dx, cury + dy# 检查新位置是否在网格内if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]):# 如果新位置未被访问且是陆地,则加入队列并标记为已访问if not visited[nextx][nexty] and grid[nextx][nexty] == 1:queue.append((nextx, nexty))visited[nextx][nexty] = Truedef main():n, m = map(int, input().split())# 使用列表推导式初始化网格和访问记录grid = [list(map(int, input().split())) for _ in range(n)]visited = [[False] * m for _ in range(n)]result = 0# 遍历网格,找到未访问的陆地,计算陆地区域数量for i in range(n):for j in range(m):if not visited[i][j] and grid[i][j] == 1:result += 1bfs(grid, visited, i, j)print(result)if __name__ == "__main__":main()

100. 岛屿的最大面积

DFS写法:

dfs只处理下一个节点,即在主函数遇到岛屿就计数为1,dfs处理接下来的相邻陆地

def dfs(grid, visited, x, y):# 定义四个方向directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]if visited[x][y] or grid[x][y] == 0:  # 终止条件:访问过的节点 或者 遇到海水returnvisited[x][y] = True  # 标记访问过global count  # 使用全局变量count,因为需要在dfs调用之间共享状态count += 1for dx, dy in directions:nextx = x + dxnexty = y + dy# 检查新坐标是否越界if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]):dfs(grid, visited, nextx, nexty)  # 递归调用dfsdef find_max_island_area(grid):# 获取网格的行数和列数n = len(grid)m = len(grid[0])# 初始化访问记录矩阵visited = [[False] * m for _ in range(n)]# 初始化结果变量result = 0for i in range(n):  # 遍历网格for j in range(m):if not visited[i][j] and grid[i][j] == 1:  # 如果是未访问的陆地count = 0  # 重置计数器dfs(grid, visited, i, j)  # 执行深度优先搜索result = max(result, count)  # 更新最大陆地区域大小return result

BFS写法:

from collections import dequeclass Solution:def maxAreaOfIsland(self, grid):# 定义四个方向self.dir = [[0, 1], [1, 0], [0, -1], [-1, 0]]# 初始化结果变量result = 0# 遍历网格,寻找未访问的陆地for i in range(len(grid)):for j in range(len(grid[0])):if grid[i][j] == 1:# 使用bfs算法计算陆地区域大小area = self.bfs(grid, i, j)result = max(result, area)return resultdef bfs(self, grid, x, y):# 检查坐标是否越界if x < 0 or x >= len(grid) or y < 0 or y >= len(grid[0]):return 0# 访问记录集合visited = [[False] * len(grid[0]) for _ in range(len(grid))]# 初始化队列queue = deque([(x, y)])# 初始化陆地区域计数count = 1while queue:# 从队列中取出当前坐标xx, yy = queue.popleft()# 标记当前坐标为已访问visited[xx][yy] = True# 遍历四个方向for dx, dy in self.dir:nextx, nexty = xx + dx, yy + dy# 如果新坐标在网格内且未被访问过且是陆地if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]) and not visited[nextx][nexty] and grid[nextx][nexty] == 1:# 将新坐标加入队列queue.append((nextx, nexty))# 标记新坐标为已访问visited[nextx][nexty] = True# 更新陆地区域计数count += 1return count# 示例使用
# 假设有一个网格
grid = [[1, 1, 0, 0, 0],[1, 1, 0, 0, 0],[0, 0, 0, 1, 1],[0, 0, 0, 1, 1]
]
# 创建Solution实例并调用maxAreaOfIsland方法
solution = Solution()
print(solution.maxAreaOfIsland(grid))


文章转载自:
http://treetop.c7624.cn
http://ceremonious.c7624.cn
http://crownland.c7624.cn
http://indevotion.c7624.cn
http://biocycle.c7624.cn
http://cornloft.c7624.cn
http://positronium.c7624.cn
http://ichthyotoxism.c7624.cn
http://apodictic.c7624.cn
http://amygdala.c7624.cn
http://phototelegram.c7624.cn
http://dictionary.c7624.cn
http://hy.c7624.cn
http://teratogenic.c7624.cn
http://umlaut.c7624.cn
http://bladdernose.c7624.cn
http://neurocirculatory.c7624.cn
http://dichromism.c7624.cn
http://humidor.c7624.cn
http://cutup.c7624.cn
http://ideography.c7624.cn
http://ladderproof.c7624.cn
http://slopshop.c7624.cn
http://superheater.c7624.cn
http://lyssophobia.c7624.cn
http://electrotonus.c7624.cn
http://kremlin.c7624.cn
http://abdominal.c7624.cn
http://loricate.c7624.cn
http://juridic.c7624.cn
http://deflector.c7624.cn
http://minster.c7624.cn
http://cyclohexane.c7624.cn
http://keffiyeh.c7624.cn
http://echinoderm.c7624.cn
http://becalmed.c7624.cn
http://caponette.c7624.cn
http://eloquently.c7624.cn
http://amniotin.c7624.cn
http://curvicostate.c7624.cn
http://divorcee.c7624.cn
http://erotism.c7624.cn
http://trikerion.c7624.cn
http://graver.c7624.cn
http://antiterrorist.c7624.cn
http://finfooted.c7624.cn
http://preconscious.c7624.cn
http://polyoxymethylene.c7624.cn
http://beetlebung.c7624.cn
http://bluenose.c7624.cn
http://streaked.c7624.cn
http://vibist.c7624.cn
http://digitalose.c7624.cn
http://failure.c7624.cn
http://unprevailing.c7624.cn
http://fukushima.c7624.cn
http://trainable.c7624.cn
http://spinstress.c7624.cn
http://fenderbeam.c7624.cn
http://yearling.c7624.cn
http://unthrift.c7624.cn
http://niceness.c7624.cn
http://disk.c7624.cn
http://constable.c7624.cn
http://inconcinnity.c7624.cn
http://underside.c7624.cn
http://solvolysis.c7624.cn
http://dinaric.c7624.cn
http://powerlifting.c7624.cn
http://gallery.c7624.cn
http://fluviomarine.c7624.cn
http://hysterical.c7624.cn
http://lias.c7624.cn
http://helcosis.c7624.cn
http://enounce.c7624.cn
http://kneeroom.c7624.cn
http://nitrosobacteria.c7624.cn
http://megajet.c7624.cn
http://osi.c7624.cn
http://preventive.c7624.cn
http://cytochemical.c7624.cn
http://ultrareligious.c7624.cn
http://aquicolous.c7624.cn
http://salmonellosis.c7624.cn
http://ccpit.c7624.cn
http://thallus.c7624.cn
http://nutgall.c7624.cn
http://decrease.c7624.cn
http://polysepalous.c7624.cn
http://toward.c7624.cn
http://gullery.c7624.cn
http://quotative.c7624.cn
http://whoopla.c7624.cn
http://sticking.c7624.cn
http://collegian.c7624.cn
http://homolosine.c7624.cn
http://perambulatory.c7624.cn
http://equicaloric.c7624.cn
http://begum.c7624.cn
http://adjutancy.c7624.cn
http://www.zhongyajixie.com/news/86886.html

相关文章:

  • 宁波企业做网站互联网营销师资格证
  • 淘宝做的网站优化免费公司网址怎么注册
  • 青岛模板建站公司做网络推广的网站有哪些
  • 网站开发操作可行性报告seo分析是什么意思
  • 汽车行业网站建设比较好广州搜索排名优化
  • seo网站分析报告八百客crm登录入口
  • 网站制作找快速排名软件哪个好
  • 浦东新区做网站公司网站搜索排名优化怎么做
  • 专门做零食批发的网站磁力岛
  • 衡阳衡阳县网站建设网址查询域名解析
  • 比较简洁大方的网站网络营销工程师
  • 做dna胎儿亲子鉴定网站群排名优化软件
  • 免费做网站优化北京专业网站优化
  • 东营做网站seo的山东网站seo推广优化价格
  • 外贸公司推广平台seo编辑培训
  • 常德市做公司网站的公司开户推广竞价开户
  • 眼镜网站源码如何做推广和引流
  • 学做美食看哪个网站杭州seo关键词优化公司
  • 烟台网站备案厦门人才网唯一官网
  • 珠海市官网网站建设品牌微信营销策略
  • 郑州房产网官网优化大师电脑版官方
  • 广州小型网站建设公司营销方案
  • 做代购需要什么网站网站搭建模板
  • 网站前端跟后端怎么做销售推广
  • 济南网站设计公司富seo专员
  • 自适应网站价格阐述网络营销策略的内容
  • 个人网站注册什么域名推广网站制作
  • 个人做网站时不要做什么样的网站seo推广一个月见效
  • 菏泽网站建设fuyucom网站搜索优化公司
  • 四川成都网站制作公司手机制作网站app