1. 101. 孤岛的总面积
题目链接:101. 孤岛的总面积
文档讲解: 代码随想录
将周边靠陆地且相邻的陆地都变成海洋,然后再重新遍历地图,统计还剩下的陆地就可以了。
#深度优先遍历
direction = [[0,1],[0,-1],[1,0],[-1,0]]
count = 0
n,m = map(int,input().split())
grid = []
for _ in range(n):
grid.append(list(map(int,input().split())))
def dfs(x,y):
global count
grid[x][y] = 0
count += 1
for i,j in direction:
next_x = x + i
next_y = y + j
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
continue
if grid[next_x][next_y] == 1:
#赋值在递归函数内部,因此这里不用进行grid[next_x][next_y]=0和count += 1
dfs(next_x,next_y)
#按行搜索,将边缘的陆地变为海洋
for i in range(m):
#第一行
if grid[0][i] == 1:
dfs(0,i)
#最后一行
if grid[n-1][i] == 1:
dfs(n-1,i)
#按列搜索
for i in range(n):
#第一列
if grid[i][0] == 1:
dfs(i,0)
#最后一列
if grid[i][m-1] == 1:
dfs(i,m-1)
#统计孤岛
count = 0
for i in range(n):
for j in range(m):
if grid[i][j] == 1:
dfs(i,j)
print(count)
#广度优先遍历
from collections import deque
direction = [[0,1],[0,-1],[1,0],[-1,0]]
count = 0
n,m = map(int,input().split())
grid = []
for _ in range(n):
grid.append(list(map(int,input().split())))
def bfs(x,y):
global count
que = deque()
que.append((x,y))
grid[x][y] = 0
count += 1
while que:
cur_x,cur_y = que.popleft()
for i,j in direction:
next_x = cur_x + i
next_y = cur_y + j
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
continue
if grid[next_x][next_y] == 1:
que.append((next_x,next_y))
grid[next_x][next_y] = 0
count += 1
#按行搜索,将边缘的陆地变为海洋
for i in range(m):
#第一行
if grid[0][i] == 1:
bfs(0,i)
#最后一行
if grid[n-1][i] == 1:
bfs(n-1,i)
#按列搜索
for i in range(n):
#第一列
if grid[i][0] == 1:
bfs(i,0)
#最后一列
if grid[i][m-1] == 1:
bfs(i,m-1)
#统计孤岛
count = 0
for i in range(n):
for j in range(m):
if grid[i][j] == 1:
bfs(i,j)
print(count)
2. 102. 沉没孤岛
思路:从地图周边出发,将周边空格相邻的陆地都做上标记,然后遍历一遍地图,遇到陆地且没有做过标记的,那么就是地图中间的陆地,全部改成水域就行。步骤一:将地图周边的陆地1全部改为特殊标记2;步骤二:将中间的陆地1全部改为水域0;步骤三:将之前的标记2改回1。
#深度优先遍历
direction = [[0,1],[0,-1],[1,0],[-1,0]]
n,m = map(int,input().split())
grid = []
for _ in range(n):
grid.append(list(map(int,input().split())))
def dfs(x,y):
#边缘陆地特殊标记
grid[x][y] = 2
for i,j in direction:
next_x = x + i
next_y = y + j
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
continue
if grid[next_x][next_y] == 1:
dfs(next_x,next_y)
#步骤一
for i in range(n):
#遍历第一列和最后一列
if grid[i][0] == 1:
dfs(i,0)
if grid[i][m-1] == 1:
dfs(i,m-1)
for j in range(m):
#遍历第一行和最后一行
if grid[0][j] == 1:
dfs(0,j)
if grid[n-1][j] == 1:
dfs(n-1,j)
#步骤二、步骤三
for i in range(n):
for j in range(m):
if grid[i][j] == 1:
grid[i][j] =0
if grid[i][j] == 2:
grid[i][j] = 1
#打印输出
for row in grid:
print(' '.join(map(str,row)))
#广度优先遍历
from collections import deque
direction = [[0,1],[0,-1],[1,0],[-1,0]]
n,m = map(int,input().split())
grid = []
for _ in range(n):
grid.append(list(map(int,input().split())))
def bfs(x,y):
que = deque()
que.append((x,y))
grid[x][y] = 2
while que:
cur_x,cur_y = que.popleft()
for i,j in direction:
next_x = cur_x + i
next_y = cur_y + j
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
continue
if grid[next_x][next_y] == 1:
que.append((next_x,next_y))
bfs(next_x,next_y)
for i in range(n):
if grid[i][0] == 1:
bfs(i,0)
if grid[i][m-1] == 1:
bfs(i,m-1)
for j in range(m):
if grid[0][j] == 1:
bfs(0,j)
if grid[n-1][j] == 1:
bfs(n-1,j)
for i in range(n):
for j in range(m):
if grid[i][j] == 1:
grid[i][j] = 0
if grid[i][j] == 2:
grid[i][j] = 1
for row in grid:
print(' '.join(map(str,row)))
3. 103. 水流问题
#深度优先搜索
direction = [[0,1],[0,-1],[1,0],[-1,0]]
n,m = map(int,input().split())
graph = []
for _ in range(n):
graph.append(list(map(int,input().split())))
first = set()
second = set()
def dfs(x,y,visited,side):
if visited[x][y]:
return
visited[x][y] = True
side.add((x,y))
for i,j in direction:
next_x = x + i
next_y = y + j
if 0 <= next_x < len(graph) and 0 <= next_y < len(graph[0]) and graph[next_x][next_y] >= graph[x][y]:
#升序
dfs(next_x,next_y,visited,side)
#是否可以到达第一条边界
visited = [[False] * m for _ in range(n)]
for i in range(n):
#第一列,左边
dfs(i,0,visited,first)
for j in range(m):
#第一行,上边
dfs(0,j,visited,first)
#是否可以到达第二条边界
visited = [[False] * m for _ in range(n)]
for i in range(n):
#最后一列,右边
dfs(i,m-1,visited,second)
for j in range(m):
#最后一行,下边
dfs(n-1,j,visited,second)
#求交集
res = first & second
#打印结果
for x,y in res:
print(f"{x} {y}")
#广度优先搜索
from collections import deque
direction = [[0,1],[0,-1],[1,0],[-1,0]]
n,m = map(int,input().split())
graph = []
for _ in range(n):
graph.append(list(map(int,input().split())))
def bfs(x,y,visited,side):
if visited[x][y]:
return
visited[x][y] = True
side.add((x,y))
que = deque()
que.append((x,y))
while que:
cur_x, cur_y = que.popleft()
for i,j in direction:
next_x = cur_x + i
next_y = cur_y + j
if 0 <= next_x < len(graph) and 0 <= next_y < len(graph[0]) and graph[x][y] <= graph[next_x][next_y]:
bfs(next_x,next_y,visited,side)
first = set()
second = set()
#第一条边界
visited = [[False] * m for _ in range(n)]
for i in range(n):
bfs(i,0,visited,first)
for j in range(m):
bfs(0,j,visited,first)
#第二条边
visited = [[False] * m for _ in range(n)]
for i in range(n):
bfs(i,m-1,visited,second)
for j in range(m):
bfs(n-1,j,visited,second)
res = first & second
for x,y in res:
print(f"{x} {y}")
调用递归函数时,visited列表会记录节点是否被访问过,因此每个节点只会访问一边,所有算法的时间复杂度为 O ( n ∗ m ) O(n*m) O(n∗m)。
4. 104.建造最大岛屿
题目链接:104.建造最大岛屿
文档讲解: 代码随想录
第一步,使用一次深搜记录各个岛屿的面积,并做标号记录。第二步,再遍历一遍地图,遍历0,统计其由0变1周边的岛屿面积,将其相邻面积加在一起。
#深度优先遍历
from collections import defaultdict
direction = [[0,1],[0,-1],[1,0],[-1,0]]
n,m = map(int,input().split())
grid = []
for _ in range(n):
grid.append(list(map(int,input().split())))
count = 0
def dfs(x,y,visited,mark):
visited[x][y] = True
grid[x][y] = mark #岛屿编号
global count
count += 1
for i,j in direction:
next_x = x + i
next_y = y + j
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
continue
if not visited[next_x][next_y] and grid[next_x][next_y] == 1:
dfs(next_x,next_y,visited,mark)
visited = [[False] * m for _ in range(n)]
mark = 2
gridnum = defaultdict(int)
#统计各个岛屿的面积
for i in range(n):
for j in range(m):
if not visited[i][j] and grid[i][j] == 1:
count = 0
dfs(i,j,visited,mark)
gridnum[mark] = count
mark += 1
#添加陆地
res = 0
#标记访问过的岛屿
visitedgrid = set()
for i in range(n):
for j in range(m):
if grid[i][j] == 0:
count = 1
visitedgrid.clear() #用之前清空
for a,b in direction:
neari = i + a
nearj = j + b
if neari < 0 or neari >= n or nearj < 0 or nearj >= m:
continue
#访问过的跳过
if grid[neari][nearj] in visitedgrid:
continue
count += gridnum[grid[neari][nearj]]
visitedgrid.add(grid[neari][nearj])
res = max(res,count)
print(res)
#广度优先搜索
from collections import defaultdict,deque
direction = [[0,1],[0,-1],[1,0],[-1,0]]
n,m = map(int,input().split())
grid = []
for _ in range(n):
grid.append(list(map(int,input().split())))
count = 0
def bfs(x,y,visited,mark):
que = deque()
que.append((x,y))
grid[x][y] = mark
visited[x][y] = True
global count
count += 1
while que:
cur_x,cur_y = que.popleft()
for i,j in direction:
next_x = cur_x + i
next_y = cur_y + j
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
continue
if not visited[next_x][next_y] and grid[next_x][next_y] == 1:
bfs(next_x,next_y,visited,mark)
#遍历求各个岛屿的面积
mark = 2
gridnum = defaultdict(int)
visited = [[False] * m for _ in range(n)]
for i in range(n):
for j in range(m):
if not visited[i][j] and grid[i][j] == 1:
count = 0
bfs(i,j,visited,mark)
gridnum[mark] = count
mark += 1
#添加陆地
visitedgrid = set()
res = 0
for i in range(n):
for j in range(m):
if grid[i][j] == 0:
count = 1
visitedgrid.clear()
for a,b in direction:
neari = i + a
nearj = j + b
if neari < 0 or neari >= n or nearj < 0 or nearj >= m:
continue
if grid[neari][nearj] in visitedgrid:
continue
count += gridnum[grid[neari][nearj]]
visitedgrid.add(grid[neari][nearj])
res = max(res,count)
print(res)