算法——统计岛数量
package com.jiucaiyuan.net.question; /** * 统计岛数量 * 【问题】一个矩阵中只有0和1两个数值,每个位置都可以和自己的上、下、左、右四个位置相连, * 如果有一片1连在一起,这个部分叫做一个岛,求一个矩阵中有多少个岛? * 【进阶】 * 如何设计一个并行算法解决这个问题(一张地图,点特别多,需要统计时,并行解决会更快些) * <p> * P12 11.基础提升 有序表、并查集等 00:03 * * @Author jiucaiyuan 2022/3/2 23:58 * @mail services@jiucaiyuan.net */ public class CountIslands { /** * 统计岛的个数 * 时间复杂度O(row*col) * * @param arr * @return */ public static int countIslands(int[][] arr) { if (arr == null || arr.length < 1 || arr[0].length < 1) { return 0; } int result = 0; int row = arr.length; int col = arr[0].length; //两个for,从左往右,从上往下,把所有节点处理一遍 for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { //如果是1,岛数加1,和他相连的都感染 if (arr[i][j] == 1) { result++; //岛数+1 infect(arr, i, j, row, col); //已经树的岛数标记为2(感染) } } } return result; } /** * 检查(i,j)位置是否进行感染,并继续向周围感染 * * @param arr 二维数组表示矩阵 * @param i 检查(i,j)位置是否进行感染,并继续向周围感染 * @param j 检查(i,j)位置是否进行感染,并继续向周围感染 * @param row 矩阵行数 * @param col 矩阵列数 */ private static void infect(int[][] arr, int i, int j, int row, int col) { if (i < 0 || j < 0 || i >= row || j >= col || arr[i][j] != 1) { return; } //原来是1,感染修改为2 arr[i][j] = 2; infect(arr, i - 1, j, row, col); infect(arr, i + 1, j, row, col); infect(arr, i, j - 1, row, col); infect(arr, i, j + 1, row, col); } private static void print(int[][] arr) { System.out.println("-----------------------------------------"); for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); } } public static void main(String[] args) { int[][] arr = { {0, 0, 0, 1, 0, 0, 1, 0}, {0, 1, 1, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 1, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 1, 0}, }; print(arr); System.out.println(countIslands(arr)); print(arr); int[][] arr2 = { {0, 0, 0, 1, 0, 0, 1, 0}, {0, 1, 1, 1, 0, 0, 1, 0}, }; print(arr2); System.out.println(countIslands(arr2)); print(arr2); int[][] arr3 = { {0, 1, 0, 1, 0, 0, 1, 0}, }; print(arr3); System.out.println(countIslands(arr3)); print(arr3); } }
« Java位运算符
|
算法——拿纸牌,最大积分是多少»
日历
个人资料

diaba 寻求合作请留言或联系mail: services@jiucaiyuan.net
链接
最新文章
存档
- 2025年4月(6)
- 2025年3月(25)
- 2025年2月(20)
- 2025年1月(2)
- 2024年10月(1)
- 2024年8月(2)
- 2024年6月(4)
- 2024年5月(1)
- 2023年7月(1)
- 2022年10月(1)
- 2022年8月(1)
- 2022年6月(11)
- 2022年5月(6)
- 2022年4月(33)
- 2022年3月(26)
- 2021年3月(1)
- 2020年9月(2)
- 2018年8月(1)
- 2018年3月(1)
- 2017年3月(3)
- 2017年2月(6)
- 2016年12月(3)
- 2016年11月(2)
- 2016年10月(1)
- 2016年9月(3)
- 2016年8月(4)
- 2016年7月(3)
- 2016年6月(4)
- 2016年5月(7)
- 2016年4月(9)
- 2016年3月(4)
- 2016年2月(5)
- 2016年1月(17)
- 2015年12月(15)
- 2015年11月(11)
- 2015年10月(6)
- 2015年9月(11)
- 2015年8月(8)
分类
热门文章
- SpringMVC:Null ModelAndView returned to DispatcherServlet with name 'applicationContext': assuming HandlerAdapter completed request handling
- Mac-删除卸载GlobalProtect
- java.lang.SecurityException: JCE cannot authenticate the provider BC
- MyBatis-Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2}
- Idea之支持lombok编译
标签
最新评论
- logisqykyk
Javassist分析、编辑和创建jav... - xxedgtb
Redis—常见参数配置 - 韭菜园 ... - wdgpjxydo
SpringMVC:Null Model... - rllzzwocp
Mysql存储引擎MyISAM和Inno... - dpkgmbfjh
SpringMVC:Null Model... - tzklbzpj
SpringMVC:Null Model... - bqwrhszmo
MyBatis-Improper inl... - 乐谱吧
good非常好 - diaba
@diaba:应该说是“时间的度量依据”... - diaba
如果速度增加接近光速、等于光速、甚至大于...
最新微语
- 在每件事情上花费的东西,就是生命的一部分,而我们花费的这些东西要求立即得到回报,或者在一个长时间以后得到回报。
2025-01-23 15:46
- 诺曼·文森特说:“并不是你认为自己是什么样的人,你就是什么样的人。但是你的思想是什么样,你就是什么样的人。”
2025-01-23 15:44
- 从今天起,做一个幸福的人。喂马,砍柴,(思想)周游世界
2022-03-21 23:31
- 2022.03.02 23:37:59
2022-03-02 23:38
- 几近崩溃后,找到解决方法,总是那么豁然开朗!所以遇到问题要坚持!
2018-07-18 10:49
发表评论: