题目如下:

In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected group of 1s not connected to any other 1s.)

Now, we may change 0s to 1s so as to connect the two islands together to form 1 island.

Return the smallest number of 0s that must be flipped.  (It is guaranteed that the answer is at least 1.)

Example 1:

Input: [[0,1],[1,0]]
Output: 1

Example 2:

Input: [[0,1,0],[0,0,0],[0,0,1]]
Output: 2

Example 3:

Input: [[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

Note:

  1. 1 <= A.length = A[0].length <= 100
  2. A[i][j] == 0 or A[i][j] == 1

解题思路:典型的DFS/BFS场景。题目中约定了只存在两个岛,所以先用DFS/BFS找出第一个岛的所有坐标,并把Input中属于第一个岛的元素值标记成2。接下来再对值为2的元素做DFS/BFS,找出与元素1最近的距离,这个距离就是结果。

代码如下:

class Solution(object):
def getOneIsland(self,visit,A):
for i in range(len(A)):
for j in range(len(A[i])):
if A[i][j] == 0:
continue
q = [(i,j)]
visit[i][j] = 1
A[i][j] = 2
while len(q) > 0:
x,y = q.pop(0)
if x - 1 >= 0 and A[x - 1][y] == 1 and visit[x - 1][y] == 0:
visit[x - 1][y] = 1
A[x - 1][y] = 2
q.append((x - 1, y))
if y - 1 >= 0 and A[x][y - 1] == 1 and visit[x][y - 1] == 0:
visit[x][y - 1] = 1
A[x][y - 1] = 2
q.append((x, y - 1))
if x + 1 < len(A) and A[x + 1][y] == 1 and visit[x + 1][y] == 0:
visit[x + 1][y] = 1
A[x + 1][y] = 2
q.append((x + 1, y))
if y + 1 < len(A[0]) and A[x][y + 1] == 1 and visit[x][y + 1] == 0:
visit[x][y + 1] = 1
A[x][y + 1] = 2
q.append((x, y + 1))
return
def shortestBridge(self, A):
"""
:type A: List[List[int]]
:rtype: int
"""
visit = []
for i in A:
tl = [0] * len(i)
visit.append(tl) res = 10000
self.getOneIsland(visit,A)
for i in range(len(A)):
for j in range(len(A[i])):
if A[i][j] != 2:
continue
q = [(i,j,0)]
while len(q) > 0:
x,y,z = q.pop(0)
if z > res:
continue
if x - 1 >= 0 and A[x - 1][y] == 1:
res = min(res,z)
if y - 1 >= 0 and A[x][y - 1] == 1:
res = min(res, z)
if x + 1 < len(A) and A[x + 1][y] == 1:
res = min(res, z)
if y + 1 < len(A[0]) and A[x][y + 1] == 1:
res = min(res, z) if x - 1 >= 0 and A[x - 1][y] == 0 and (visit[x - 1][y] == 0 or visit[x - 1][y] > z + 1):
visit[x - 1][y] = z+1
q.append((x - 1, y,z+1))
if y - 1 >= 0 and A[x][y - 1] == 0 and (visit[x][y-1] == 0 or visit[x][y - 1] > z + 1):
visit[x][y - 1] = z+1
q.append((x, y - 1,z+1))
if x + 1 < len(A) and A[x + 1][y] == 0 and (visit[x+1][y] == 0 or visit[x + 1][y] > z + 1):
visit[x + 1][y] = z+1
q.append((x + 1, y,z+1))
if y + 1 < len(A[0]) and A[x][y + 1] == 0 and (visit[x][y+1] == 0 or visit[x][y + 1] > z + 1):
visit[x][y + 1] = z+1
q.append((x, y + 1,z+1))
return res

【leetcode】934. Shortest Bridge的更多相关文章

  1. 【LeetCode】934. Shortest Bridge 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...

  2. 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...

  3. 【LeetCode】214. Shortest Palindrome 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...

  4. 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...

  5. 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...

  6. 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...

  7. 【LeetCode】214. Shortest Palindrome

    Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...

  8. 【leetcode】1129. Shortest Path with Alternating Colors

    题目如下: Consider a directed graph, with nodes labelled 0, 1, ..., n-1.  In this graph, each edge is ei ...

  9. 【leetcode】1091. Shortest Path in Binary Matrix

    题目如下: In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top- ...

随机推荐

  1. 如何使用WidsMob Montage—蒙太奇制作有趣的动物照片?

    今天,越来越多的人有宠物.根据最近的一项调查,超过六成的美国人在家中至少有一只宠物.这些宠物不是动物,而是家庭成员.因此,有趣的动物照片成为社交媒体上的热门话题是有道理的.当您打开朋友圈或短视频APP ...

  2. el-tag标签使用三元表达动态改变type类型

    <el-tag :type="item.payCode=='在线' ? 'success' : 'danger'" >{{item.payCode}}</el-t ...

  3. SimpleDateFormat线程不安全原因及解决方案

    一. 线程不安全验证: /** * SimpleDateFormat线程安全测试 * 〈功能详细描述〉 * * @author 17090889 * @see [相关类/方法](可选) * @sinc ...

  4. Ext js-01 -helloworld

    一.下载ext: 登陆这个网址  https://www.sencha.com/products/evaluate/ 下载下来解压后如下:安装cmd程序 二.开始helloworld 新建一个idea ...

  5. webpack使用的补充

    1.分离生产环境和开发环境的wepack.config.js 我们可以将生产环境和开发环境中的配置分离为两个不同的文件,并且还维护一个共同的配置文件 common,可以通过 webpack.merge ...

  6. StrictMode 严格模式

    StrictMode: 帮助程序员避免在主线程上执行耗时操作: 启动级别: 1. 启动线程级别:   setThreadPolicy() 2. 启动应用级别 :  setVmPolicy() —— 对 ...

  7. 7.zabbix常用item

    zabbix常用item zabbix常用item vfs.file.md5sum[/etc/crontab] {basic:vfs.file.md5sum[/etc/crontab].diff()} ...

  8. 前端每日实战:23# 视频演示如何用纯 CSS 创作一个菜单反色填充特效

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览.https://codepen.io/comehope/pen/qYMoPo 可交互视频教程 此视频是 ...

  9. Python 进阶_OOP 面向对象编程_类和继承

    目录 目录 类 最简单的类 类方法 构造器 __init__ 创建一个类 实例化一个对象 调用实例的方法和属性 创建子类 使用 super 来调用父类的构造器 实例化子类对象 调用子类的属性和方法 类 ...

  10. 小程序UI自动化(一):appium小程序自动化尝试

    appium 进行 小程序自动化尝试: 由于工作中进行app自动化用的是appium,故首先尝试用appium进行小程序自动化,以美团小程序为例(python脚本实现) 一.配置基础信息 启动微信ap ...