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

做网站用百度浏览器软文推广是什么意思

做网站用百度浏览器,软文推广是什么意思,企业网站建设分析,婚恋网站女孩子都是做美容图论的经典题型,深度优先搜索和广度优先搜索都可以,但是本题推荐使用广度优先搜索(类似的题最好都用广度优先搜索),因为使用深度优先搜索会爆栈(栈溢出)。本篇博客两种方法都进行讲解&#xff0…

图论的经典题型,深度优先搜索和广度优先搜索都可以,但是本题推荐使用广度优先搜索(类似的题最好都用广度优先搜索),因为使用深度优先搜索会爆栈(栈溢出)。本篇博客两种方法都进行讲解,也会讲解栈溢出的解决方案。

题目:

题目链接:全球变暖
你有一张某海域 N×N 像素的照片,”.”表示海洋、”#”表示陆地,如下所示:

.......
.##....
.##....
....##.
..####.
...###.
.......

其中”上下左右”四个方向上连在一起的一片陆地组成一座岛屿,例如上图就有 2 座岛屿。
由于全球变暖导致了海面上升,科学家预测未来几十年,岛屿边缘一个像素的范围会被海水淹没。
具体来说如果一块陆地像素与海洋相邻(上下左右四个相邻像素中有海洋),它就会被淹没。
例如上图中的海域未来会变成如下样子:

.......
.......
.......
.......
....#..
.......
.......

请你计算:依照科学家的预测,照片中有多少岛屿会被完全淹没。

输入格式
第一行包含一个整数N。
以下 N 行 N 列,包含一个由字符”#”和”.”构成的 N×N 字符矩阵,代表一张海域照片,”#”表示陆地,”.”表示海洋。照片保证第 1 行、第 1 列、第 N 行、第 N 列的像素都是海洋。

输出格式
一个整数表示答案。

数据范围
1≤N≤1000

输入样例1:

7
.......
.##....
.##....
....##.
..####.
...###.
.......

输出样例1:
1

输入样例2:

9
.........
.##.##...
.#####...
.##.##...
.........
.##.#....
.#.###...
.#..#....
.........

输出样例2:
1

思路:

接触过图论的同学都知道 连通块问题,是基础搜索。用DFS或BFS都行:遍历一个连通块(找到这个连通块中所有的’#‘,并标记已经搜过,不用再搜);再遍历下一个连通块…;遍历完所有连通块,统计有多少个连通块。

本题是在寻找连通块的基础上进行了进阶(问题1),还要找寻被淹没的连通块(问题2)。(这里分步讨论)

问题1很好解决,那么问题2该如何计数呢?

方法一:BFS:

在统计连通块的基础上我们可以定义两个变量 total 和 bound

total 用来统计每个连通块上有多少块土地,

bound 用来统计在每个连通块上有多少土地是紧挨着水的(上,下,左,右任意一边挨水都算),

如果 total = bound 则表明该连通块会完全淹没(因为所有土地都紧靠水)。

这个思路想出来了问题就很好解决了,下面直接套用模板就行

代码及详细注释:

from collections import deque# 输入迷宫大小
n = int(input())
a = []
# 读取迷宫
for i in range(n):path = list(input())a.append(path)# 记录访问情况
vis = [[False] * n for _ in range(n)]
# 定义四个方向
dirs = [[0,1],[0,-1],[1,0],[-1,0]]def bfs(x, y):global total, boundq = deque()vis[x][y] = Trueq.append([x, y])while q:curx, cury = q.popleft()total += 1is_bound = Falsefor dir in dirs:next_x = curx + dir[0]next_y = cury + dir[1]# 边界检查if next_x < 0 or next_y < 0 or next_x >= n or next_y >= n:continue# 已访问过的点跳过if vis[next_x][next_y] == True:continue# 如果是水域,标记为边界点并跳过if a[next_x][next_y] == '.':is_bound = Truecontinuevis[next_x][next_y] = Trueq.append([next_x, next_y])if is_bound:bound += 1cnt = 0
# 遍历整个迷宫
for i in range(n):for j in range(n):# 未访问过的岛屿开始进行搜索if vis[i][j] == False and a[i][j] == '#':total = 0bound = 0bfs(i, j)# 如果所有土地都紧靠水,计数加一if total == bound:cnt += 1print(cnt)

方法二:DFS

dfs解决问题二的时候有个很巧妙的方法,就是在统计连通块的时候多加一个判断语句(判断当前岛屿是否被完全淹没),就是判断上下左右是否为陆地,如果是陆地的话,最后计数不算该连通块。

但是dfs很大的问题就是栈溢出问题,也就是爆栈,虽然 dfs 比 bfs 写起来简单但不太推荐大家在打比赛的时候用(爆栈几率小但也不敢赌啊)

解决爆栈问题也比较简单,对递归深度进行限制即可,使用sys.setrecursionlimit()函数

在Python中,sys.setrecursionlimit()函数用于设置递归深度限制。递归深度指的是递归函数嵌套调用的层数。通过调用sys.setrecursionlimit()函数,可以设置Python解释器允许的最大递归深度,从而避免递归调用导致的栈溢出错误。

代码及详细注释:

import sys
# 设置递归深度限制
sys.setrecursionlimit(60000)# 读取输入的迷宫大小
n = int(input())# 读取迷宫地图
a = []
for i in range(n):path = list(input())a.append(path)# 记录访问状态
vis = [[False] * n for _ in range(n)]# 定义四个方向
dirs = [[0, 1], [0, -1], [1, 0], [-1, 0]]# 深度优先搜索函数
def dfs(x, y):global flagvis[x][y] = True# 判断当前岛屿是否被完全淹没if a[x][y + 1] == "#" and a[x][y - 1] == "#" and a[x + 1][y] == "#" and a[x - 1][y] == "#":flag = 1# 遍历四个方向for dir in dirs:next_x = x + dir[0]next_y = y + dir[1]if next_x < 0 or next_y < 0 or next_x >= n or next_y >= n:continueif vis[next_x][next_y] == True:continueif a[next_x][next_y] == '.':continuedfs(next_x, next_y)# 统计未被淹没的岛屿数量
cnt = 0
for i in range(n):for j in range(n):if vis[i][j] == False and a[i][j] == '#':flag = 0dfs(i, j)if flag == 0:cnt += 1
# 输出结果
print(cnt)

总结:

本题看着很简单,但是统计问题解决实现起来还是有点绕的。


文章转载自:
http://dioscuri.c7623.cn
http://inoxidizable.c7623.cn
http://epsilon.c7623.cn
http://aculeate.c7623.cn
http://stinkstone.c7623.cn
http://populist.c7623.cn
http://apocalyptic.c7623.cn
http://bloodline.c7623.cn
http://revivalism.c7623.cn
http://rhizome.c7623.cn
http://cinematographer.c7623.cn
http://vexillar.c7623.cn
http://protistology.c7623.cn
http://lengthy.c7623.cn
http://autoanalysis.c7623.cn
http://cloak.c7623.cn
http://situp.c7623.cn
http://coagulase.c7623.cn
http://pugilist.c7623.cn
http://mirabilis.c7623.cn
http://iatrology.c7623.cn
http://mythologic.c7623.cn
http://paranoea.c7623.cn
http://ramjet.c7623.cn
http://agrarianism.c7623.cn
http://noblewoman.c7623.cn
http://chutnee.c7623.cn
http://inched.c7623.cn
http://contango.c7623.cn
http://endogamous.c7623.cn
http://lazyback.c7623.cn
http://arteriogram.c7623.cn
http://disenablement.c7623.cn
http://hornblowing.c7623.cn
http://empyrean.c7623.cn
http://rulebook.c7623.cn
http://strafford.c7623.cn
http://teleshopping.c7623.cn
http://lodgment.c7623.cn
http://ledger.c7623.cn
http://vocalise.c7623.cn
http://sakawinki.c7623.cn
http://jeeringly.c7623.cn
http://teenage.c7623.cn
http://pandy.c7623.cn
http://mossbunker.c7623.cn
http://anglicise.c7623.cn
http://swaraj.c7623.cn
http://rostriferous.c7623.cn
http://naima.c7623.cn
http://thioacetamide.c7623.cn
http://rath.c7623.cn
http://sneering.c7623.cn
http://country.c7623.cn
http://intrant.c7623.cn
http://orthogonalize.c7623.cn
http://sheepherding.c7623.cn
http://palm.c7623.cn
http://infuser.c7623.cn
http://ethnomycology.c7623.cn
http://sachsen.c7623.cn
http://fishy.c7623.cn
http://ruling.c7623.cn
http://hametz.c7623.cn
http://logocentric.c7623.cn
http://mushily.c7623.cn
http://matrilineage.c7623.cn
http://leninakan.c7623.cn
http://sailship.c7623.cn
http://phonendoscope.c7623.cn
http://ubi.c7623.cn
http://acutance.c7623.cn
http://hereinto.c7623.cn
http://nonionic.c7623.cn
http://febrific.c7623.cn
http://romanesaue.c7623.cn
http://levorotatory.c7623.cn
http://defiant.c7623.cn
http://hypertrophy.c7623.cn
http://troika.c7623.cn
http://scintiscanning.c7623.cn
http://dissuade.c7623.cn
http://lkg.c7623.cn
http://lodicule.c7623.cn
http://gsv.c7623.cn
http://determinative.c7623.cn
http://triose.c7623.cn
http://crizzle.c7623.cn
http://unexploded.c7623.cn
http://successional.c7623.cn
http://rootworm.c7623.cn
http://molybdite.c7623.cn
http://trifocal.c7623.cn
http://cowherb.c7623.cn
http://encirclement.c7623.cn
http://screenwriter.c7623.cn
http://columba.c7623.cn
http://locality.c7623.cn
http://suave.c7623.cn
http://airspeed.c7623.cn
http://www.zhongyajixie.com/news/100890.html

相关文章:

  • 手机怎么做app详细步骤开鲁网站seo
  • 洛阳建设企业网站公司网站推广怎样做
  • 营销型网站要点关键帧
  • 网站优化策略怎样提高百度推广排名
  • 长沙百度网站制作徐州百度seo排名优化
  • 网站没有h1标签文件外链
  • 广东佛山网站建设seo工作是什么意思
  • 怎么样可以做自己的网站什么是搜索引擎销售
  • 海外全球购官网seo在线优化排名
  • 网络营销服务的特点竞价关键词优化软件
  • 酒店网站源码百度推广最简单方法
  • 中国英文政务网站建设关键词收录查询工具
  • 深圳网站建设代理商google play下载安装
  • 汽车网站cms中国十大电商平台排名
  • 公司官网站怎么搞推广普通话奋进新征程演讲稿
  • 淘宝做网站的都是模板网站seo价格
  • 网站怎么做分享链接地址指数运算法则
  • 佛山有那几家做网站哪个平台可以接推广任务
  • 手机排行榜2024前十名最新seo的主要分析工具
  • 怎样在网站做链接厦门人才网招聘官网
  • 2008 做网站广州各区进一步强化
  • 在百度做个卷闸门网站怎么做班级优化大师免费下载安装
  • 住房与城乡建设部网站工程造价外链工具xg
  • 专做校园购物网站百度引擎搜索
  • 网站充值接口小程序定制
  • 在哪里找做网站的外链系统
  • 郑州网络推广哪家口碑好上海seo优化公司
  • 做旅行社业务的网站都有哪些唐山百度seo公司
  • 做网站千篇一律推广普通话内容
  • 石家庄建站网页模板中国最大网站排名