【leetcode】934. Shortest Bridge
题目如下:
In a given 2D binary array
A
, there are two islands. (An island is a 4-directionally connected group of1
s not connected to any other 1s.)Now, we may change
0
s to1
s so as to connect the two islands together to form 1 island.Return the smallest number of
0
s 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 <= 100
A[i][j] == 0
orA[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- ...
随机推荐
- noteone
- boost bimap
The library Boost.Bimap is based on Boost.MultiIndex and provides a container that can be used immed ...
- php ucwords()函数 语法
php ucwords()函数 语法 作用:把每个单词的首字符转换为大写 语法:ucwords(string) 参数: 参数 描述 string 必须,规定要转换的字符串 说明:把字符串中每个单词的首 ...
- SGU 176 (有源汇最小流)
转载:http://blog.csdn.net/dan__ge/article/details/51207951 题意:n个节点,m条路径,接下来m行a,b,c,d,如果d等于1,则a到b的流量必须为 ...
- moment.js 时间库
一.概念: https://www.cnblogs.com/Jimc/p/10591580.html 或 http://momentjs.cn/(官网) 1.Moment.js是一个 ...
- 第六周学习总结&实验报告四
一.实验目的 (1)掌握类的继承 (2)变量的继承和覆盖,方法的继承,重载和覆盖的实现: 二.实验的内容 (1)根据下面的要求实现圆类Circle. 1.圆类Circle的成员变量:radius表示圆 ...
- 实用|从0到1 搭建Web性能监控系统
工具介绍 1. Statsd 是一个使用Node开发网络守护进程,它的特点是通过UDP(性能好,及时挂了也不影响主服务)或者TCP来监听各种数据信息,然后发送聚合数据到后端服务进行处理. 常见支持的「 ...
- git 忽略提交及已push过得文件忽略提交
在使用Git的过程中,我们喜欢有的文件比如日志,临时文件,编译的中间文件等不要提交到代码仓库,这时就要设置相应的忽略规则,来忽略这些文件的提交 Git 忽略文件提交的方法 这种方式通过在项目的某个文件 ...
- 斯坦福【概率与统计】课程笔记(五):EDA | 箱线图
介绍箱线图之前,需要先介绍若干个其需要的术语 min:整个样本的最小值 max:整个样本的最大值 Range:即整个样本的取值范围,Range = max - min Inter-Quartile R ...
- Django 上下文管理器的应用
使用场景:模板继承可以减少页面内容的重复定义,实现页面内容的重用.个人博客右侧的导航栏都是继承base页面从而让代码得到最大程度的复用.但是当父模板中有动态数据的话,这些动态数据在子模版中是不会显示的 ...