题目如下:

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. css linear-gradient;心跳animation

    css线性背景 background:linear-gradient(20deg,#ccffff,#ffcccc); transform transform:scale(1.5); transform ...

  2. React笔记02——React中的组件

    一个网页可以被拆分成若干小部分,每个部分都可以称为组件,即组件是网页中的一部分.组件中还可以有多个组件. 上一节中的App.js就是一个组件(继承了React.Component类的类). 一个组件的 ...

  3. Java Web学习总结(8)JSP(二)

    一,JSP中的九个内置对象 名称 类型 描述 out javax.servlet.jsp.JspWriter 用于页面输出 request javax.servlet.http.HttpServlet ...

  4. HTTP请求流程基础知识

    HTTP协议解析: HTTP即超文本传输协议,是一种详细规定了浏览器和万维网服务器之间互相通信的规则,它是万维网交换信息的基础,它允许将HTML文档从WEB服务器传输到WEB浏览器. URL(统一资源 ...

  5. php strtok()函数 语法

    php strtok()函数 语法 作用:逐一分割字符串大理石构件 语法:strtok(string,split) 参数: 参数 描述 string 必需.规定要分割的字符串. split 必需.规定 ...

  6. Pangu and Stones HihoCoder - 1636 区间DP

    Pangu and Stones HihoCoder - 1636 题意 给你\(n\)堆石子,每次只能合成\(x\)堆石子\((x\in[L, R])\),问把所有石子合成一堆的最小花费. 思路 和 ...

  7. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

  8. DZY Loves Math

    DZY Loves Math 对于正整数 $n$,定义 $f(n)$ 为 $n$ 所含质因子的最大幂指数. 例如 $f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, ...

  9. python中的encode()和decode()函数

    前言: 我们知道,计算机是以二进制为单位的,也就是说计算机只识别0和1,也就是我们平时在电脑上看到的文字,只有先变成0和1,计算机才会识别它的意思.这种数据和二进制的转换规则就是编码.计算机的发展中, ...

  10. get the deadlock information from sql server

    https://stackoverflow.com/questions/12422986/sql-query-to-get-the-deadlocks-in-sql-server-2008 You c ...