【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: 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 <= 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- ...
随机推荐
- Ubuntu 16.04配置vncviewer
网上有各种各样的教程,既混乱又复杂.这是提供一个亲自测试可用的配置方案,十分简单,桌面环境选用xfce,Ubuntu版本是16.04. 1 安装 Xfce 和 TightVNC sudo apt in ...
- JavaWeb(五):MVC案例
MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它把应用程序分成三个核心模块:模型.视图.控制器,它们各自处理自己的任务.模型是应用程序的主体部分 ...
- PHP计算经纬度在百度多边形区域内
最近做一个项目需要使用到区域,并且要判断当前的经纬度是否在区域内,已便对应业务需求变化.废话不多说直接上代码: /** * 验证区域范围 * @param array $coordArray 区域 * ...
- Linux操作系统之安全审计功能
内核编译时,一般打开NET选项就打开AUDIT选项了.在系统中查看audit是否打开,root 用户执行:service auditd status 我们知道在Linux系统中有大量的日志文件可以用于 ...
- css 图片 和 文本 的处理
图片 1.css3已经可以实现 img标签 和 img内图片分开处理的功能了.类似标签的背景图. https://www.zhangxinxu.com/wordpress/2015/03/css3 ...
- [CSP-S模拟测试]:chinese(数学)
题目传送门(内部题25) 输入格式 一行三个整数$n,m,k$. 输出格式 一行一个整数表示答案. 样例 样例输入: 2 2 2 样例输出: 数据范围与提示 样例解释: $f_0=10,f_1=4,f ...
- (转)Docker 网络
转:https://www.cnblogs.com/allcloud/p/7150564.html 本系列文章将介绍 Docker的相关知识: (1)Docker 安装及基本用法 (2)Docker ...
- 2017 JUST Programming Contest 2.0
B. So You Think You Can Count? 设dp[i]表示以i为结尾的方案数,每个位置最多往前扫10位 #include<bits/stdc++.h> using na ...
- lerna----一个强大的专注组件库的管理工具
最近在看关于lerna的资料,没有中文版api.简书上有一篇文章半英半汉,可以撸来看看. http://www.jianshu.com/p/63ec67445b0f
- QTP与QC整合
QC-QTP整合 在本节中,我们将学习如何将QTP和QC整合.通过整合,在QTP自动化脚本可以直接从Quality Center执行.建立连接,第一个步骤是安装所需的加载项.我们将了解如何通过采取样品 ...