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. 8.Spring整合Hibernate_2_声明式的事务管理(Annotation的方式)

    声明式的事务管理(AOP的主要用途之一) (Annotation的方式) 1.加入annotation.xsd 2.加入txManager bean 3.<tx:annotation-drive ...

  2. Centos 6.5 版本的下载教程

    1.CentOS下载CentOS是免费版,推荐在官网上直接下载,网址:https://www.centos.org/download/ 2.选择版本推荐6.5 3.选择isos 4.根据系统电脑的配置 ...

  3. 智能指针原理及实现(2)unique_ptr

    只允许基础指针的一个所有者. 可以移到新所有者(具有移动语义),但不会复制或共享(即我们无法得到指向同一个对象的两个unique_ptr). 替换已弃用的 auto_ptr. 相较于 boost::s ...

  4. vue-element-admin 之改变登录界面input的光标颜色

    前话:用框架原有的login更改而不重写的话,恰好当你input背景设置成白色的时候,光标会找不到=>原因:原框架的光标颜色是#fff 操作更改光标颜色: 找到src/views/login/i ...

  5. C#编程 LINQ查询

    LINQ查询表达式 约束 LINQ查询表达式必须以from子句开头,以select或group子句结束 关键字 from...in...:指定要查找的数据以及范围变量,多个from子句则表示从多个数据 ...

  6. C# Winform TabControl 双击关闭TabPage

    在使用TabControl控件时,需要考虑自动创建的TabPage页实现——关闭功能 思路一:自定义TabControl控件,添加关闭按钮 思路二:TabControl控件TabPage显示ToolT ...

  7. Hadoop_13_Hadoop Shell脚本采集日志上传到HDFS

    案例1:开发shell采集脚本 1.点击流日志每天都10T,在业务应用服务器上,需要准实时上传至数据仓库(Hadoop HDFS)上 2.一般上传文件都是在凌晨24点操作,由于很多种类的业务数据都要在 ...

  8. UESTC 2016 Summer Training #1 J - Objects Panel (A) 按条件遍历树

    #include <iostream> #include <cstdio> #include <vector> using namespace std; typed ...

  9. poj1236 Network of Schools(SCC缩点+结论推导)

    第一问简单不讲. 第二问简化后问题是给一张DAG求最少添加几条边使得DAG变成一个SCC.首先所有中间点(有入度有出度)肯定直接顺着走到无出度点,所以肯定是无出度点连向无入度点. 把无入度点作为点集S ...

  10. DTcmsV4.0分析学习——(3)URL重写

    3.URL重写 3.1 控制流程 通过IHttpModule控制所有页面请求,具体流程如下 (1)真实路径可正常访问 (2)前台页面通过URL重写映射aspx目录,后台页面通过URL重写映射admin ...