题目如下:

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:

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

Example 2:

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

Example 3:

  1. 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]]
  2. 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最近的距离,这个距离就是结果。

代码如下:

  1. class Solution(object):
  2. def getOneIsland(self,visit,A):
  3. for i in range(len(A)):
  4. for j in range(len(A[i])):
  5. if A[i][j] == 0:
  6. continue
  7. q = [(i,j)]
  8. visit[i][j] = 1
  9. A[i][j] = 2
  10. while len(q) > 0:
  11. x,y = q.pop(0)
  12. if x - 1 >= 0 and A[x - 1][y] == 1 and visit[x - 1][y] == 0:
  13. visit[x - 1][y] = 1
  14. A[x - 1][y] = 2
  15. q.append((x - 1, y))
  16. if y - 1 >= 0 and A[x][y - 1] == 1 and visit[x][y - 1] == 0:
  17. visit[x][y - 1] = 1
  18. A[x][y - 1] = 2
  19. q.append((x, y - 1))
  20. if x + 1 < len(A) and A[x + 1][y] == 1 and visit[x + 1][y] == 0:
  21. visit[x + 1][y] = 1
  22. A[x + 1][y] = 2
  23. q.append((x + 1, y))
  24. if y + 1 < len(A[0]) and A[x][y + 1] == 1 and visit[x][y + 1] == 0:
  25. visit[x][y + 1] = 1
  26. A[x][y + 1] = 2
  27. q.append((x, y + 1))
  28. return
  29. def shortestBridge(self, A):
  30. """
  31. :type A: List[List[int]]
  32. :rtype: int
  33. """
  34. visit = []
  35. for i in A:
  36. tl = [0] * len(i)
  37. visit.append(tl)
  38.  
  39. res = 10000
  40. self.getOneIsland(visit,A)
  41. for i in range(len(A)):
  42. for j in range(len(A[i])):
  43. if A[i][j] != 2:
  44. continue
  45. q = [(i,j,0)]
  46. while len(q) > 0:
  47. x,y,z = q.pop(0)
  48. if z > res:
  49. continue
  50. if x - 1 >= 0 and A[x - 1][y] == 1:
  51. res = min(res,z)
  52. if y - 1 >= 0 and A[x][y - 1] == 1:
  53. res = min(res, z)
  54. if x + 1 < len(A) and A[x + 1][y] == 1:
  55. res = min(res, z)
  56. if y + 1 < len(A[0]) and A[x][y + 1] == 1:
  57. res = min(res, z)
  58.  
  59. if x - 1 >= 0 and A[x - 1][y] == 0 and (visit[x - 1][y] == 0 or visit[x - 1][y] > z + 1):
  60. visit[x - 1][y] = z+1
  61. q.append((x - 1, y,z+1))
  62. if y - 1 >= 0 and A[x][y - 1] == 0 and (visit[x][y-1] == 0 or visit[x][y - 1] > z + 1):
  63. visit[x][y - 1] = z+1
  64. q.append((x, y - 1,z+1))
  65. if x + 1 < len(A) and A[x + 1][y] == 0 and (visit[x+1][y] == 0 or visit[x + 1][y] > z + 1):
  66. visit[x + 1][y] = z+1
  67. q.append((x + 1, y,z+1))
  68. 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):
  69. visit[x][y + 1] = z+1
  70. q.append((x, y + 1,z+1))
  71. 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. Ubuntu 16.04配置vncviewer

    网上有各种各样的教程,既混乱又复杂.这是提供一个亲自测试可用的配置方案,十分简单,桌面环境选用xfce,Ubuntu版本是16.04. 1 安装 Xfce 和 TightVNC sudo apt in ...

  2. JavaWeb(五):MVC案例

    MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它把应用程序分成三个核心模块:模型.视图.控制器,它们各自处理自己的任务.模型是应用程序的主体部分 ...

  3. PHP计算经纬度在百度多边形区域内

    最近做一个项目需要使用到区域,并且要判断当前的经纬度是否在区域内,已便对应业务需求变化.废话不多说直接上代码: /** * 验证区域范围 * @param array $coordArray 区域 * ...

  4. Linux操作系统之安全审计功能

    内核编译时,一般打开NET选项就打开AUDIT选项了.在系统中查看audit是否打开,root 用户执行:service auditd status 我们知道在Linux系统中有大量的日志文件可以用于 ...

  5. css 图片 和 文本 的处理

    图片 1.css3已经可以实现 img标签 和 img内图片分开处理的功能了.类似标签的背景图.   https://www.zhangxinxu.com/wordpress/2015/03/css3 ...

  6. [CSP-S模拟测试]:chinese(数学)

    题目传送门(内部题25) 输入格式 一行三个整数$n,m,k$. 输出格式 一行一个整数表示答案. 样例 样例输入: 2 2 2 样例输出: 数据范围与提示 样例解释: $f_0=10,f_1=4,f ...

  7. (转)Docker 网络

    转:https://www.cnblogs.com/allcloud/p/7150564.html 本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker ...

  8. 2017 JUST Programming Contest 2.0

    B. So You Think You Can Count? 设dp[i]表示以i为结尾的方案数,每个位置最多往前扫10位 #include<bits/stdc++.h> using na ...

  9. lerna----一个强大的专注组件库的管理工具

    最近在看关于lerna的资料,没有中文版api.简书上有一篇文章半英半汉,可以撸来看看. http://www.jianshu.com/p/63ec67445b0f

  10. QTP与QC整合

    QC-QTP整合 在本节中,我们将学习如何将QTP和QC整合.通过整合,在QTP自动化脚本可以直接从Quality Center执行.建立连接,第一个步骤是安装所需的加载项.我们将了解如何通过采取样品 ...