题目如下:

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. Thymeleaf入门到吃灰

    Thymeleaf 官网部分翻译:反正就是各种好 Thymeleaf是用来开发Web和独立环境项目的服务器端的Java模版引擎 Spring官方支持的服务的渲染模板中,并不包含jsp.而是Thymel ...

  2. axios拦截器的使用方法

    很多时候我们需要在发送请求和响应数据的时候做一些页面处理,比如在请求服务器之前先判断以下用户是登录(通过token判断),或者设置请求头header,或者在请求到数据之前页面显示loading等等,还 ...

  3. PHP 利用 curl 发送 post get del put patch 请求

    因为需要在 php 开发中对接其它接口需要用 php curl 去对接其它接口 我把他们封装成函数 希望能对大家有所帮助. 这里面是封装好的会自动把 data 进行转成 json 格式,同时解码成 p ...

  4. fedora23下编译安装OpenCV-3.1.0

    所需安装环境 1.安装编译环境 $ sudo dnf install gcc gcc-c++ ncurses-devel cmake 2.安装gtk+2.x $ sudo dnf install gt ...

  5. 安装uWebSocketIO

    https://github.com/uNetworking/uWebSockets sudo apt-get install libuv1-dev git clone https://github. ...

  6. flutte页面布局四

    AspectRatio 组件 AspectRatio 的作用是根据设置调整子元素 child 的宽高比. AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widget 的高度是由 ...

  7. python中递归函数

    python中的 递归函数,是指的是函数在函数内部调用自己的函数 需要满足两个条件,一,需要有一个明确的终止条件 二,需要函数自己在内部调用自己

  8. Android实战技巧:Dialog (转)

    转:http://blog.csdn.net/hitlion2008/article/details/7567549#t0 Dialog是任何系统都必须有的一个控件,作为辅助窗口,用于显示一些消息,或 ...

  9. getAttribute 与getParmeter 区别

    1.getAttribute是取得jsp中 用setAttribute設定的attribute 2.parameter得到的是string:attribute得到的是object 3.request. ...

  10. spring 注释

    4