【leetcode】934. Shortest Bridge
题目如下:
In a given 2D binary array
A, there are two islands. (An island is a 4-directionally connected group of1s not connected to any other 1s.)Now, we may change
0s to1s 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: 1Example 2:
Input: [[0,1,0],[0,0,0],[0,0,1]]
Output: 2Example 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: 1Note:
1 <= A.length = A[0].length <= 100A[i][j] == 0orA[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的更多相关文章
- 【LeetCode】934. Shortest Bridge 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...
- 【LeetCode】862. Shortest Subarray with Sum at Least K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 队列 日期 题目地址:https://leetcod ...
- 【LeetCode】214. Shortest Palindrome 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...
- 【LeetCode】847. Shortest Path Visiting All Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/shortest ...
- 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...
- 【LeetCode】821. Shortest Distance to a Character 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 过两遍数组 日期 题目地址:https://leet ...
- 【LeetCode】214. Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 【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 ...
- 【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- ...
随机推荐
- 【leetcode】564. Find the Closest Palindrome
题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...
- CSS中的一些伪类
一.:nth-child 和 :nth-of-type (1):nth-child() :nth-child(n) 选择器选取某任意一父元素的第 n 个子元素( p:nth-child(n) 即选中任 ...
- 使用kindeditor直接粘贴本地图片或者是qq截图
我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...
- Bugku | Easy_Re
学到一个函数: _mm_storeu_si128((__m128i *)&v5, _mm_loadu_si128((const __m128i *)&xmmword_413E34)); ...
- CVE-2017-0213 | 记一次失败的提权经历
环境: CVE-2017-0213下载 提权步骤: 提权失败.... 好迷啊,,,,事后查了一下补丁 我的wind7上也没装啊,然后防火墙也是关闭的 迷了迷了....
- Redis的高级特性一览
更多内容,欢迎关注微信公众号:全菜工程师小辉.公众号回复关键词,领取免费学习资料. 应用场景 缓存系统:用于缓解数据库的高并发压力 计数器:使用Redis原子操作,用于社交网络的转发数,评论数,粉丝数 ...
- 测开之路四十一:常用的jquery函数
jQuery选择器菜鸟教程:https://www.runoob.com/jquery/jquery-selectors.html 引用jquery2.1.1标签:<script src=&qu ...
- 发布delphi程序(build with runtime package)要带哪些文件?
Delphi提供两种方式来编译你的程序:使用包或者是单独的exe 使用包,你可以使用如下方法设置: 项目选项(菜单project->options->Packages页), 在Runtim ...
- flask扩展系列之 - 访问速度限制
flask-limiter 是一个对客户端的访问速率进行限制的flask扩展.可以自定义一些访问的(速度)限制条件来把那些触发限制的请求拒之门外.一般常用来进行对爬虫的限制. 下面就常见的用法,举了一 ...
- shell 删除隐藏文件.svn
参考:https://blog.csdn.net/zhangxinrun/article/details/6409125 echo "recursively removing .svn fo ...