[leetcode]934. Shortest Bridge


Analysis

2019要开心鸭—— [每天刷题并不难0.0]

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.)

Explanation:

先用DFS标记island,再用BFS扩充island,从而找到bridge的数量

Implement

class Solution {public:int shortestBridge(vector<vector<int>>& A) {pair<int, int> start;m = A.size();n = A[0].size();vector<vector<int>> visit(m, vector<int>(n));queue<pair<int, int>> mq;int flag = 0;for(int i=0; i<m; i++){if(flag == 1)break;for(int j=0; j<n; j++){if(A[i][j] == 1){DFS(i, j, A, visit, mq);flag = 1;break;}}}//DFS(start.first, start.second, A, visit, mq);int res = 0;while(!mq.empty()){int sz = mq.size();while(sz-- > 0){pair<int, int> cur = mq.front();mq.pop();for(int i=0; i<4; i++){int x = cur.first+dir[i][0];int y = cur.second+dir[i][1];if(x>=0 && y>=0 && x<m && y<n && visit[x][y]==0){if(A[x][y] == 1)return res;visit[x][y] = 1;mq.emplace(x, y);}}}res++;}return -1;}void DFS(int x, int y, vector<vector<int>>& A, vector<vector<int>>& visit, queue<pair<int, int>>& mq){if(x<0 || x>=m || y<0 || y>=n || visit[x][y]==1 || A[x][y]==0)return ;visit[x][y] = 1;mq.emplace(x, y);for(int i=0; i<4; i++)DFS(x+dir[i][0], y+dir[i][1], A, visit, mq);}
private:int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};int m, n;
};

[leetcode]934. Shortest Bridge相关推荐

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

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

  2. 【Breadth-first Search 】934. Shortest Bridge

    输入:一个二维数组,每个元素的值为0/1. 规则:所有连在一起的1是一个岛屿,数组中包含2个岛屿.连在一起是指上下左右4个方向.可以将0变为1,将2个岛屿链接在一起. 输出:最小改变多少个0就可以将2 ...

  3. LeetCode 613. Shortest Distance in a Line --SQL

    LeetCode 613. Shortest Distance in a Line --SQL LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部 ...

  4. 【To Understand! 回文串6 KMP算法】LeetCode 214. Shortest Palindrome

    LeetCode 214. Shortest Palindrome Solution1:最笨的方法.时间复杂度O(n2)O(n2)O(n^2) 竟然能AC,xixixi class Solution ...

  5. [LeetCode] 1091. Shortest Path in Binary Matrix

    LeetCode刷题记录 传送门 Description In an N by N square grid, each cell is either empty (0) or blocked (1). ...

  6. [leetcode] 748. Shortest Completing Word

    Description Find the minimum length word from a given dictionary words, which has all the letters fr ...

  7. [Swift]LeetCode934. 最短的桥 | Shortest Bridge

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. leetcode 581. Shortest Unsorted Continuous Subarray | 581. 最短无序连续子数组(单调栈)

    题目 https://leetcode.com/problems/shortest-unsorted-continuous-subarray/ 题解 乍一看,没思路,直奔 Related Topics ...

  9. leetcode 214. Shortest Palindrome | 214. 最短回文串(Java)

    题目 https://leetcode.com/problems/shortest-palindrome/ 题解 看了 Related Topics - Rolling Hash 下的相关题目,看到了 ...

  10. leetcode 1293. Shortest Path in a Grid with Obstacles Elimination | 1293. 网格中的最短路径(BFS)

    题目 https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/ 题解 DFS 递归超时,看了评 ...

最新文章

  1. Halcon中MIN宏定义与自己定义的C++中MIN函数--重命名冲突问题解决方法
  2. vue开发环境和生产环境里面解决跨域的几种方法
  3. opencv sgbm 三维重建_图像三维重建方法综述
  4. REDHAT6.3 udev 配置 存储器磁盘
  5. 遍历一个数据去掉最后一个元素的样式
  6. CentOS 5.9 yum安装LAMP(Apache+MySQL+PHP)
  7. 【大牛疯狂教学】深入拆解java虚拟机百度云
  8. 实木地板被机器人弄成坑_实木地板的常规保养
  9. 如何弄ad装配图_[分享][BCW]上海西康路189弄——“世界最美购物中心”幕墙的诞生...
  10. 别在学习框架了,那些让你起飞的计算机基础知识。
  11. Gradle教程--基础篇
  12. 第1章:基本句型及补语
  13. 【Matlab】线性回归之最小二乘法的应用与验证
  14. Google Python命名规范
  15. 大学计算机案例教程旧照片修复,「教你一招」使用自动软件修复老照片
  16. 2014年FME校园培训考核通过名单
  17. 手机电脑Mac地址修改方法
  18. linux系统mysql报err1055_MySQL Err 1055的解决
  19. 软件设计中的csc_一名设计研究生吐血整理的CSC/国家留学基金委申请须知【成功经验分享】...
  20. 汽车变速器虚拟现实VR教学方案

热门文章

  1. nas服务器加密文件夹,NAS加密文件夹创建和使用教程
  2. 加密文件夹密码忘记怎么办?
  3. ug12无法连接服务器系统,NX许可证错误:无法连接至许可证服务器系统。SPLM_LICENSE_SERVER错误[-15]...
  4. 线性稳压芯片的选取要素
  5. Python爬取豆瓣top250书籍(beautifulsoup法)
  6. 数据科学家VS大数据专家VS数据分析师:有什么不同?
  7. 魔童降世-暴走的哪吒
  8. 降薪潮袭来,中高层考虑去留,底层员工别无选择
  9. 马化腾是该全面反思腾讯战略了:吃老本不能让腾讯变得伟大!
  10. 世界读书日之后聊聊读书