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. python 把函数的值赋给变量

    本博文的知识点一个是模块的调用和一个自定义函数返回值赋值给变量 编写一个简单的函数模块: [root@bigdata zw]# more d.py #!/usr/bin/python # -*- co ...

  2. MYSQL8.0+ 使用JDBC查询中文乱码的问题

    在建表时,附加一句 DROP TABLE IF EXISTS `sys_table`;CREATE TABLE `sys_table` ( ... ) ENGINE=InnoDB DEFAULT CH ...

  3. 顺丰科技面试-java开发

    顺丰科技的面试官感觉人都挺随和,总共经历三面,两轮技术面,一轮hr面. 一.专业面一 主要是对着我的简历上的东西问,我的一个项目经历,两个实习上面以及自己提到会的技能展开的提问. (1)自我简介 (2 ...

  4. PEP8规范 Python

    前言 从很多地方搬运+总结,以后根据这个标准再将python的一些奇技淫巧结合起来,写出更pythonic的代码~ PEP8 编码规范 英文原版请点击这里 以下是@bobo的整理,原文请见PEP8 P ...

  5. 为什么说Redis是单线程的以及Redis为什么这么块

    一.前言 近乎所有与Java相关的面试都会问到缓存的问题,基础一点的会问到什么是“二八定律”.什么是“热数据和冷数据”,复杂一点的会问到缓存雪崩.缓存穿透.缓存预热.缓存更新.缓存降级等问题,这些看似 ...

  6. Redis主从复制(读写分离)(四)

    Redis主从复制(读写分离) 克隆三台linux虚拟机   9.1.1.克隆虚拟机   9.1.2.生成新的mack地址 9.1.3.主从复制配置 redis主从复制 概述 1.redis的复制功能 ...

  7. AGC刷题记

    已经刷不了几天了... AGC001 A-BBQ Easy 排个序就过了 B-Mysterious Light 手膜一下,你会发现魔改一下\(gcd\)就行了 C-Shorten Diameter 刚 ...

  8. python内置函数(二)之filter,map,sorted

    filter filter()函数接收一个函数 f 和一个iterable的对象,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条 ...

  9. C# ado.net oledb方式连接(三)

    oledb 方式连接 class Program { private static string constr = "server=.;database=northwnd;integrate ...

  10. XHTML测试题

    1.XHTML 指的是? A.EXtra Hyperlinks and Text Markup Language B.EXtensible HyperText Marking Language C.E ...