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
Runtime: 40 ms, faster than 61.25% of C++ online submissions for Shortest Bridge.

class Solution {
private:
int dirs[][] = {{,},{,-},{,},{-,}}; public: int dist(int x1, int x2, int y1, int y2){
return abs(x1 - x2) + abs(y1 - y2);
} int shortestBridge(vector<vector<int>>& A) {
// cout << A.size() << endl;
// cout << A[0].size() << endl;
vector<vector<int>> t1, t2;
bool found1 = false;
for(int i=; i<A.size(); i++){
for(int j=; j<A[].size(); j++){
if(A[i][j] == ) {
if(!found1) {
found1 = true;
helper(A, i, j, t1);
}
else helper(A, i, j, t2);
}
}
}
int mindist = INT_MAX;
for(int i=; i<t1.size(); i++){
for(int j=; j<t2.size(); j++){
mindist = min(mindist, dist(t1[i][], t2[j][], t1[i][], t2[j][]));
}
}
return mindist-;
} void helper(vector<vector<int>>& A, int x, int y, vector<vector<int>>& target) {
A[x][y] = -;
for(int i=; i<; i++){
int dx = x + dirs[i][];
int dy = y + dirs[i][];
if(dx >= && dx < A.size() && dy >= && dy < A[].size() && A[dx][dy] == ) {
target.push_back({x,y});
break;
}
}
for(int i=; i<; i++){
int dx = x + dirs[i][];
int dy = y + dirs[i][];
if(dx >= && dx < A.size() && dy >= && dy < A[].size() && A[dx][dy] == ) {
helper(A, dx, dy, target);
}
}
}
};

LC 934. Shortest Bridge的更多相关文章

  1. [LeetCode] 934. Shortest Bridge 最短的桥梁

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...

  2. LeetCode 934. Shortest Bridge

    原题链接在这里:https://leetcode.com/problems/shortest-bridge/ 题目: In a given 2D binary array A, there are t ...

  3. 【leetcode】934. Shortest Bridge

    题目如下: In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connecte ...

  4. 【LeetCode】934. Shortest Bridge 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...

  5. Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)

    Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...

  6. LC 245. Shortest Word Distance III 【lock, medium】

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

  7. LC 244. Shortest Word Distance II 【lock, Medium】

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  8. [LC] 244. Shortest Word Distance II

    Design a class which receives a list of words in the constructor, and implements a method that takes ...

  9. [LC] 243. Shortest Word Distance

    Given a list of words and two words word1 and word2, return the shortest distance between these two ...

随机推荐

  1. CHD-5.3.6集群上Flume的文件监控

    收集hive的log     hive的运行日志:    /home/hadoop/CDH5.3.6/hive-0.13.1-cdh5.3.6/log/hive.log * memory *hdfs  ...

  2. CAFFE(FAQ.2):Ubuntu 配置caffe 框架之数据库读取,错误解决:ImportError: No module named leveldb解决办法

    Z: 在安装了caffe框架后需要读取大量的数据进行学习训练.比如在MNIST识别训练中,一般直接读图片会比较耗时,我们一般将图片转存为数据库中.目前主流的数据库有以下两种选择: LevelDB Lm ...

  3. 原生js中如果有多个onload事件解决方案

    在一个页面中有两个JavaScript 分别都用到了window.onload 一个是:window.onload=func1,另一个是:window.onload=func2 这样就造成了一个Jav ...

  4. String 类的常用方法都有那些?(未完成)

    String 类的常用方法都有那些?(未完成)

  5. 遍历二叉树 - 基于栈的DFS

    之前已经学过二叉树的DFS的遍历算法[http://www.cnblogs.com/webor2006/p/7244499.html],当时是基于递归来实现的,这次利用栈不用递归也来实现DFS的遍历, ...

  6. js获取页面元素的位置

    一.网页的大小和浏览器窗口的大小 首先,要明确两个基本概念. 一张网页的全部面积,就是它的大小.通常情况下,网页的大小由内容和CSS样式表决定. 浏览器窗口的大小,则是指在浏览器窗口中看到的那部分网页 ...

  7. 用代理服务加快brew下载速度。方法:curl

    加快brew更新速度的方式:用代理 参考: https://www.zhihu.com/question/31360766常用的ss客户端都自带PAC模式的,比如ShadowsocksX-NG. 再次 ...

  8. RHEL8 创建本地YUM存储库

    yum 的好处及本地yum的好处不在本文讨论范畴,本文针对rhel8中的新功能yum做简要介绍和配置,在 RHEL 8中分为两个存储库: BaseOS 应用程序流(AppStream) BaseOS中 ...

  9. 2、django后端:课程表、课程详细表

    1.课程表录入数据 有些人卖接口,接口就是 数据 1.课程表 2张表+3张表 分布式数据库分表 数据库优化 垂直分表 1000w条数据,999w条数据不常看,经常查的1w条数据放在另一个表 水平分表 ...

  10. nextUntil([exp|ele][,fil]) 查找当前元素之后所有的同辈元素,直到遇到匹配的那个元素为止。

    nextUntil([exp|ele][,fil]) 概述 查找当前元素之后所有的同辈元素,直到遇到匹配的那个元素为止. 如果提供的jQuery代表了一组DOM元素,.nextUntil()方法也能让 ...