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. C++手动调用析构函数无效问题排查

    在学习C++的时候,都知道不要手动调用析构函数,也不要在构造函数.析构函数里调用虚函数.工作这么多年,这些冷门的知识极少用到,渐渐被繁杂的业务逻辑淹没掉. 不过,最近项目里出现了析构函数没有被正确地调 ...

  2. 手写走通HTTP server 第二版本

    HTTP server 2.0 1 接收客户请求 2 解析客户端请求 3 组合数据,形成HTTP response 4 将数据发送给客户端 升级 : 1 多线程接收客户端请求 2 基本的请求解析,根据 ...

  3. C# Winfrom TabControl美化

    实例一: using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; na ...

  4. Python GUI--Tkinter简单实现个性签名设计

    一.Tkinter的介绍和简单教程Tkinter 是 Python 的标准 GUI 库.Python 使用 Tkinter 可以快速的创建 GUI 应用程序.由于 Tkinter 是内置到 pytho ...

  5. 一个线程相关的高CPU占用问题的定位

    最近在重构项目代码时,发现两个线程同时访问一个加锁的std::list队列时,会出现恶性竞争锁的现象. 具体现象是A线程总是拿不到锁,B线程抢占几次后,A才抢占到. 由于是重构项目,也无法通过回滚代码 ...

  6. [NOI2016]循环之美——结论+莫比乌斯反演

    原题链接 好妙的一道神仙题 题目大意 让你求在\(k\)进制下,\(\frac{x}{y}\)(\(x\in [1,n],y\in [1,m]\))中有多少个最简分数是纯循环小数 SOLUTION 首 ...

  7. 单独使用ibatis做事物控制。

    当项目中,只使用到了ibatis而没有使用spring来作为事物控制的时候,可以这样写: try { Reader reader = Resources.getResourceAsReader(&qu ...

  8. GNS3错误’Could not start Telnet console with command 'Solar-PuTTY.exe‘

    这个报错是由于电脑中没有安装Solar-Putty导致的.直接安装一个solar-putty或者putty,然后在gns3上方工具栏点击:edit - preferences - general -c ...

  9. python-platform模块:平台相关属性

    import platform x=platform.machine() #返回平台架构 #AMD64 x=platform.node() #网络名称(主机名) #DESKTOP-KIK668C x= ...

  10. learing cbor protocol

    https://tools.ietf.org/html/rfc7049 https://github.com/panzidongfamily/tinycbor