LC 934. Shortest Bridge
In a given 2D binary array A
, there are two islands. (An island is a 4-directionally connected group of 1
s not connected to any other 1s.)
Now, we may change 0
s to 1
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
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的更多相关文章
- [LeetCode] 934. Shortest Bridge 最短的桥梁
In a given 2D binary array A, there are two islands. (An island is a 4-directionally connected grou ...
- LeetCode 934. Shortest Bridge
原题链接在这里:https://leetcode.com/problems/shortest-bridge/ 题目: In a given 2D binary array A, there are t ...
- 【leetcode】934. Shortest Bridge
题目如下: In a given 2D binary array A, there are two islands. (An island is a 4-directionally connecte ...
- 【LeetCode】934. Shortest Bridge 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS + BFS 相似题目 参考资料 日期 题目地 ...
- Leetcode之深度+广度优先搜索(DFS+BFS)专题-934. 最短的桥(Shortest Bridge)
Leetcode之广度优先搜索(BFS)专题-934. 最短的桥(Shortest Bridge) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary ...
- 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 ...
- 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 ...
- [LC] 244. Shortest Word Distance II
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- [LC] 243. Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
随机推荐
- Hadoop_19_MapReduce&&Yarn运行机制
1.YARN的运行机制 1.1.概述: Yarn集群:负责海量数据运算时的资源调度,集群中的角色主要有:ResourceManager.NodeManager Yarn是一个资源调度(作业调度和集群资 ...
- windows下虚拟python环境
Windows虚拟环境 cd %HOMEDRIVE%%HOMEPATH%\Desktop python3 -m venv venv 环境变量修改脚本bat,把脚本放到%HOMEDRIVE%%HOM ...
- GetHashCode之于引用类型和值类型及其特性
GetHashCode 方法可由派生类型重写.如果 GetHashCode 未重写,则通过调用基类的 Object.GetHashCode 方法来计算引用类型的哈希代码. 引用类型:Object.Ge ...
- Vue入门(四)——Axios向SpringMVC传数据
在实际业务需求中,经常会出现前台传表单或者对象到后台,后台Handler接受并转换成对应的POJO以供业务代码使用 此时在SpringMVC框架中就要用到@RequestBody注解,该注解用于将请求 ...
- DAG及拓扑排序
1.有向无环图和拓扑排序 有向无环图(Directed Acyclic Graph,简称DAG):拓扑排序指的对DAG一个有序的线性排列.即每次选出一个没有入度的节点,然后输出该点并将节点和其相关连的 ...
- 洛谷P4983 忘情 (WQS二分+斜率优化)
题目链接 忘情水二分模板题,最优解对划分段数的导数满足单调性(原函数凸性)即可使用此方法. 详细题解洛谷里面就有,不啰嗦了. 二分的临界点让人有点头大... #include<bits/stdc ...
- java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream
扩展阅读:https://blog.csdn.net/kimylrong/article/details/50353161
- 前端知识体系:JavaScript基础-一个原型继承例子
function Elem(id) { this.elem = document.getElementById(id); } Elem.prototype.html = function(val){ ...
- jvm crash分析
问题描述:线上进程异常退出,查看服务器端日志,有jvm crash文件生成 # # A fatal error has been detected by the Java Runtime Enviro ...
- 牛客国庆集训派对Day6 && CCPC-WannaFly-Camp #1 F. kingdom(DP)
题目链接:https://www.nowcoder.com/acm/contest/206/F 题意:一棵 n 个点的树,根为 1,重儿子到父亲的费用为 0,其余为 1,问所有点到 1 的最大总费用是 ...