当前位置:网站首页>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])边栏推荐
- Reddit product director: a practical guide for NFT members for Web3 creators
- 2017 annual summary
- 342. Power of Four
- vs code
- Termux set up the computer to connect to the mobile phone. (knock the command quickly), mobile phone termux port 8022
- Redis
- SICF批量激活服务节点
- AcWing第54场周赛
- Gradle notes
- SAP 开发Keys 申请SSCR Keys申请
猜你喜欢

leetcode 第 297 场周赛

In C # development, the third-party components lambdaparser, dynamicexpresso and z.expressions are used to dynamically parse / evaluate string expressions

46. Permutations

SAP 开发Keys 申请SSCR Keys申请

RCE&代码执行漏洞

Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster

Tis tutorial 01- installation

推荐一款M1芯片电脑快速搭建集群的虚拟机软件

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷

Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting
随机推荐
338. Counting Bits
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
从零开始写一个契约测试工具
If Tiankeng majors learn IC design by themselves, will any company want it
2022-6-21os review group linking method
MySQL 5.7 + Navicat 下载安装教程(附安装包)
leetcode 11. 盛最多水的容器
MySQL_ Create and manage tables
Gradle notes
8 challenges of BSS application cloud native deployment
termux设置电脑连接手机。(敲打命令贼快),手机termux端口8022
130. Surrounded Regions
mysql笔记
通过 postgis 制作 按照米制的矩形边框
这不会又是一个Go的BUG吧?
Redis
go语言笔记
SAP ABAP ole core code
Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting
假如,程序员面试的时候说真话