当前位置:网站首页>934. Shortest Bridge
934. Shortest Bridge
2022-06-22 13:14:00 【Sterben_ Da】
934. Shortest Bridge
Medium
2633131Add to ListShare
You are given an n x n binary matrix grid where 1 represents land and 0 represents water.
An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid.
You may change 0's to 1's to connect the two islands to form one island.
Return the smallest number of 0's you must flip to connect the two islands.
Example 1:
Input: grid = [[0,1],[1,0]] Output: 1
Example 2:
Input: grid = [[0,1,0],[0,0,0],[0,0,1]] Output: 2
Example 3:
Input: grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]] Output: 1
Constraints:
n == grid.length == grid[i].length2 <= n <= 100grid[i][j]is either0or1.- There are exactly two islands in
grid.
class Solution:
def shortestBridge(self, grid: List[List[int]]) -> int:
"""
assert Solution().shortestBridge([[0, 1, 0],
[0, 0, 0],
[0, 0, 1]]) == 2
assert Solution().shortestBridge([[0, 1],
[1, 0]]) == 1
assert Solution().shortestBridge([[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 1, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1]]) == 1
There are exactly two islands in grid.!!!! Only 2 An island . Vomit
Their thinking : Think a lot , Thought it was a multi island . I didn't think of it . It turns out that there are only 2 An island
First dfs Find the first island to dye , And add the points of the waters around the first island to the queue ,
Then, the points in the queue are bfs Until we find another island , The number of cycles is the number of links required
"""
# Deep search marks the first island stain
def dfs(x: int, y: int):
# dyeing
grid[x][y] = 2
for d in direction:
x1 = x + d[0]
y1 = y + d[1]
if x1 < 0 or x1 >= n or y1 < 0 or y1 >= m:
continue
if grid[x1][y1] == 0:
# waters
points.append([x1, y1])
continue
if grid[x1][y1] == 2:
continue
dfs(x1, y1)
direction = [[-1, 0], [0, -1], [0, 1], [1, 0]]
points = []
n, m = len(grid), len(grid[0])
# Find the first island
for i in range(n):
if len(points) > 0:
# Found the first island
break
for j in range(m):
if grid[i][j] == 1:
dfs(i, j)
break
times = 0
while len(points) > 0:
times += 1
num = len(points)
while num > 0:
num -= 1
[x, y] = points.pop(0)
grid[x][y] = 2
for d in direction:
x1 = x + d[0]
y1 = y + d[1]
if x1 < 0 or x1 >= n or y1 < 0 or y1 >= m:
continue
if grid[x1][y1] == 1:
return times
if grid[x1][y1] == 2:
# First island
continue
grid[x1][y1] = 2
points.append([x1, y1])边栏推荐
- Sap-mm-migo 311 intra factory transfer inventory
- Leetcode 297 match de la semaine
- Making rectangular border according to metric system through PostGIS
- Testing methodology - data driven testing
- 240. Search a 2D Matrix II
- Ffmpeg converts AMR format to MP3 format
- leetcode 32. 最长有效括号
- Tis tutorial 01- installation
- Jushan database was invited to attend Kunpeng developers' annual event 2022 to jointly build a domestic digital base
- [QT] qfileinfo get the components of the file name
猜你喜欢

Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting

Arcpy adding layers to map documents

SICF批量激活服务节点

Tianyi cloud explores a new idea of cloud native and edge computing integration

MAUI使用Masa blazor组件库

46. Permutations

Sap-mm-migo 311 intra factory transfer inventory

JAXB元素详解

On the routing tree of gin

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
随机推荐
Tianyi cloud explores a new idea of cloud native and edge computing integration
Precautions for wechat official account development
Secondary development of robotframework - real time log
leetcode 1130. 叶值的最小代价生成树
PHP反序列化&魔术方法
OO2022第四单元作业总结
The Chinese display of SAP client is garbled
Isn't the execution process of ODPs SQL executed from top to bottom
windows系统安装多个mysql版本(不用卸载原版本),新旧版本兼容。
260. Single Number III
RobotFramework二次开发——实时日志
go语言笔记
SiCf batch activation service node
The solution of VPC network automatic configuration based on terraform
318. Maximum Product of Word Lengths
通过 postgis 制作 按照米制的矩形边框
建立自己的网站(5)
记录阿里云ECS实例重启之后无法登录解决方法(亲身实践)
6月《中国数据库行业分析报告》发布!智能风起,列存更生
130. Surrounded Regions