In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you how java uses BFS and DFS to solve the number of islands. The content is simple and clear. I hope it can help you solve your doubts. Let me take you to study and learn the article "how java uses BFS and DFS to solve the number of islands".
However dark and scary the world might be right now, there will be light.
No matter how dark and terrible the world is now, it will be bright again one day.
Problem description
Give you a two-dimensional grid consisting of'1' (land) and'0' (water). Please calculate the number of islands in the grid.
Islands are always surrounded by water, and each island can only be formed by adjacent land connections horizontally or vertically.
In addition, you can assume that all four sides of the mesh are surrounded by water.
Example 1:
Enter:
[
['1pm, 0']
['1, 1, 1, 1, 1, 0, 0, 1, 4, 4, 1, 4, 4, 1, 4, 4, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
['1, 1, 1, 1, 0, 0, 0, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
['0pm, 0pm, 0pm, 0pm, 0pm, 0m, 0m, 0']
]
Output: 1
Example 2:
Enter:
[
['1, 1, 1, 1, 0, 0, 0, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
['1, 1, 1, 1, 0, 0, 0, 4, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
['0pm, 0pm, 0pm, 1pm, 0pm, 0m, 0']
['0,0,0,0,0,0,0,0,0,1 and 1]
]
Output: 3
Explanation: each island can only be connected by adjacent land horizontally and / or vertically.
DFS solution
This problem is to find the area of the island, the median value of 1 in the two-dimensional array is all islands, if multiple 1s are connected, then they can only be counted as one island.
The easiest way is to traverse every value in the array, if it is 1, it is an island, and then set it to 0 or any other character, as long as it is not 1, and then traverse its upper, lower, left and right four positions. If it is 1, it means that the two islands are connected and can only be regarded as one island. We have to set it to 0, and then take it as the center to traverse his upper and lower four positions. If it is 0, it means it is not an island, and it is not traversing four positions above and below him. Let's take example 1 as an example.
As long as each position is 1, first set it to 0, and then continue to traverse along its upper, lower, left and right directions, perform the same operation, and pay attention to the judgment of the boundary conditions. The code is relatively simple. Let's take a look.
1public int numIslands (char [] [] grid) {2 / / Boundary condition judgment 3 if (grid = = null | | grid.length = = 0) 4 return 0; 5 / count the number of islands 6 int count = 0; 7 / / two for loops traverse each grid 8 for (int I = 0; I)
< grid.length; i++) 9 for (int j = 0; j < grid[0].length; j++) {10 //只有当前格子是1才开始计算11 if (grid[i][j] == '1') {12 //如果当前格子是1,岛屿的数量加113 count++;14 //然后通过dfs把当前格子的上下左右415 //个位置为1的都要置为0,因为他们是连着16 //一起的算一个岛屿,17 dfs(grid, i, j);18 }19 }20 //最后返回岛屿的数量21 return count;22}2324//这个方法会把当前格子以及他邻近的为1的格子都会置为125public void dfs(char[][] grid, int i, int j) {26 //边界条件判断,不能越界27 if (i < 0 || i >= grid.length | | j
< 0 || j >= grid [0] .length | | grid [I] [j] = ='0') 28 return;29 / / set the current grid to 0, and then continue traversing 30 grid [I] [j] = '0crossing 31 dfs (grid, I-1, j) from top to bottom; / / upper 32 dfs (grid, I + 1, j); / / lower 33 dfs (grid, I, j + 1) / / left 34 dfs (grid, I, j-1); / / right 35}
BFS solution
DFS walks along a path and returns only when it encounters a termination condition, while BFS first goes through the visit near the current location, like the following, first visits the circle, and then enlarges the circle to continue the visit, like the following
This problem can be solved by using BFS and DFS. If you encounter a grid with position 1, as long as you can set all the 1 next to them to 0, and then next to 1 also set to 0, and then. Go on like this, take a look at the code.
1public int numIslands (char [] [] grid) {2 / / Boundary condition judgment 3 if (grid = = null | | grid.length = = 0) 4 return 0; 5 / count the number of islands 6 int count = 0; 7 / / two for loops traverse each grid 8 for (int I = 0; I)
< grid.length; i++) 9 for (int j = 0; j < grid[0].length; j++) {10 //只有当前格子是1才开始计算11 if (grid[i][j] == '1') {12 //如果当前格子是1,岛屿的数量加113 count++;14 //然后通过bfs把当前格子的上下左右415 //个位置为1的都要置为0,因为他们是连着16 //一起的算一个岛屿,17 bfs(grid, i, j);18 }19 }20 return count;21}2223private void bfs(char[][] grid, int x, int y) {24 //把当前格子先置为025 grid[x][y] = '0';26 int n = grid.length;27 int m = grid[0].length;28 //使用队列,存储的是格子坐标转化的值29 Queue queue = new LinkedList();30 //我们知道平面坐标是两位数字,但队列中存储的是一位数字,31 //所以这里是把两位数字转化为一位数字32 int code = x * m + y;33 //坐标转化的值存放到队列中34 queue.add(code);35 while (!queue.isEmpty()) {36 //出队37 code = queue.poll();38 //在反转成坐标值(i,j)39 int i = code / m;40 int j = code % m;41 if (i >0 & & grid [I-1] [j] = ='1') {/ / Upper 42 / / if the upper grid is 1, set it to 0, then add it to the queue 43 / / the same as 44 grid [I-1] [j] = 45 queue.add ((I-1) * m + j); 46} 47 if (I
< n - 1 && grid[i + 1][j] == '1') {//下48 grid[i + 1][j] = '0';49 queue.add((i + 1) * m + j);50 }51 if (j >0 & & grid [I] [j-1] = ='1') {/ / left 52 grid [I] [j-1] ='0 grid 53 queue.add (I * m + j-1); 54} 55 if (j < m-1 & & grid [I] [j + 1] = ='1') {/ right 56 grid [I] [j + 1] ='0' What are the characteristics of 57 queue.add (I * m + j + 1); 58} 59} 60} Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.
The above is about "how java uses BFS and DFS to solve the number of islands". If this article is helpful and well written, please share it with your friends to learn new knowledge. if you want to know more about it, please pay more attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.