当前位置:网站首页>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])边栏推荐
- MySQL笔记
- SAP SPRO configure how to display the corresponding t-code
- 微信支付二维码生成
- 257. Binary Tree Paths
- Isn't the execution process of ODPs SQL executed from top to bottom
- Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
- 8 challenges of BSS application cloud native deployment
- 155. Min Stack
- MySQL_ Query of data processing
- docker安装postgresql
猜你喜欢

CVPR 2022 | visual language model pre training for scene text detection

leetcode 834. 树中距离之和

MySQL笔记

310. Minimum Height Trees

windows系统安装多个mysql版本(不用卸载原版本),新旧版本兼容。

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!

Lao Wang said that the sixth issue of the series: PHP programmers should build their own self-confidence

Reconstruction practice of complex C-end project of acquisition technology

Wisdom age voice +php

在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
随机推荐
Reconstruction practice of complex C-end project of acquisition technology
从零开始写一个契约测试工具——数据库设计
leetcode 854. 相似度为 K 的字符串
Sap-abap- how to transfer material master data, supplier master data, work orders, purchase orders and other information to external systems in real time - implicit enhancement.
AcWing第53场周赛
Write a contract testing tool from scratch
RobotFramework二次开发——文件解析
BSS应用程序云原生部署的8大挑战
从零开始写一个契约测试工具
SAP 客户端中文显示乱码问题
MAUI使用Masa blazor组件库
Secondary development of robotframework -- socket push real-time log
SiCf batch activation service node
Under Xinchuang: when the domestic database stars shine
448. Find All Numbers Disappeared in an Array
190. Reverse Bits
Parallels Desktop 17.1.4pd virtual machine
leetcode 1579. 保证图可完全遍历
leetcode LCP 10. 二叉树任务调度
0007 reverse integer