当前位置:   article > 正文

14.优化算法之BFS解决FloodFill算法1

14.优化算法之BFS解决FloodFill算法1

0.FloodFill简介

dfs:深度优先遍历(红色)

bfs:宽度优先遍历

1.图像渲染

算法原理

  1. class Solution {
  2. int[] dx = { 0, 0, 1, -1 };
  3. int[] dy = { 1, -1, 0, 0 };
  4. public int[][] floodFill(int[][] image, int sr, int sc, int color) {
  5. int prev = image[sr][sc]; // 统计刚开始的颜⾊
  6. if (prev == color)
  7. return image; // 处理边界情况
  8. int m = image.length, n = image[0].length;
  9. Queue<int[]> q = new LinkedList<>();
  10. q.add(new int[] { sr, sc });
  11. while (!q.isEmpty()) {
  12. int[] t = q.poll();
  13. int a = t[0], b = t[1];
  14. image[a][b] = color;
  15. // 上下左右四个⽅向
  16. for (int i = 0; i < 4; i++) {
  17. int x = a + dx[i], y = b + dy[i];
  18. if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] == prev) {
  19. q.add(new int[] { x, y });
  20. }
  21. }
  22. }
  23. return image;
  24. }
  25. }

 2.岛屿数量

200. 岛屿数量 - 力扣(LeetCode)

算法原理 

  1. class Solution {
  2. int[] dx = { 0, 0, -1, 1 };
  3. int[] dy = { 1, -1, 0, 0 };
  4. boolean[][] vis = new boolean[301][301];
  5. int m, n;
  6. public int numIslands(char[][] grid) {
  7. m = grid.length;
  8. n = grid[0].length;
  9. int ret = 0;
  10. for (int i = 0; i < m; i++) {
  11. for (int j = 0; j < n; j++) {
  12. if (grid[i][j] == '1' && !vis[i][j]) {
  13. ret++;
  14. bfs(grid, i, j);
  15. }
  16. }
  17. }
  18. return ret;
  19. }
  20. public void bfs(char[][] grid, int i, int j) {
  21. Queue<int[]> q = new LinkedList<>();
  22. q.add(new int[] { i, j });
  23. vis[i][j] = true;
  24. while (!q.isEmpty()) {
  25. int[] t = q.poll();
  26. int a = t[0], b = t[1];
  27. for (int k = 0; k < 4; k++) {
  28. int x = a + dx[k], y = b + dy[k];
  29. if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' &&
  30. !vis[x][y]) {
  31. q.add(new int[] { x, y });
  32. vis[x][y] = true;
  33. }
  34. }
  35. }
  36. }
  37. }

 3.岛屿的最大面积

695. 岛屿的最大面积 - 力扣(LeetCode)

  1. class Solution {
  2. int[] dx = { 0, 0, 1, -1 };
  3. int[] dy = { 1, -1, 0, 0 };
  4. boolean[][] vis = new boolean[51][51];
  5. int m, n;
  6. public int maxAreaOfIsland(int[][] grid) {
  7. m = grid.length;
  8. n = grid[0].length;
  9. int ret = 0;
  10. for (int i = 0; i < m; i++) {
  11. for (int j = 0; j < n; j++) {
  12. if (grid[i][j] == 1 && !vis[i][j]) {
  13. ret = Math.max(ret, bfs(grid, i, j));
  14. }
  15. }
  16. }
  17. return ret;
  18. }
  19. public int bfs(int[][] grid, int i, int j) {
  20. int count = 0;
  21. Queue<int[]> q = new LinkedList<>();
  22. q.add(new int[] { i, j });
  23. vis[i][j] = true;
  24. count++;
  25. while (!q.isEmpty()) {
  26. int[] t = q.poll();
  27. int a = t[0], b = t[1];
  28. for (int k = 0; k < 4; k++) {
  29. int x = a + dx[k], y = b + dy[k];
  30. if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 &&
  31. !vis[x][y]) {
  32. q.offer(new int[] { x, y });
  33. vis[x][y] = true;
  34. count++;
  35. }
  36. }
  37. }
  38. return count;
  39. }
  40. }

 4.被围绕的区域

130. 被围绕的区域 - 力扣(LeetCode)

算法原理

  1. class Solution {
  2. int n, m;
  3. public void solve(char[][] board) {
  4. n = board.length;
  5. if (n == 0) {
  6. return;
  7. }
  8. m = board[0].length;
  9. for (int i = 0; i < n; i++) {
  10. dfs(board, i, 0);
  11. dfs(board, i, m - 1);
  12. }
  13. for (int i = 1; i < m - 1; i++) {
  14. dfs(board, 0, i);
  15. dfs(board, n - 1, i);
  16. }
  17. for (int i = 0; i < n; i++) {
  18. for (int j = 0; j < m; j++) {
  19. if (board[i][j] == 'A') {
  20. board[i][j] = 'O';
  21. } else if (board[i][j] == 'O') {
  22. board[i][j] = 'X';
  23. }
  24. }
  25. }
  26. }
  27. public void dfs(char[][] board, int x, int y) {
  28. if (x < 0 || x >= n || y < 0 || y >= m || board[x][y] != 'O') {
  29. return;
  30. }
  31. board[x][y] = 'A';
  32. dfs(board, x + 1, y);
  33. dfs(board, x - 1, y);
  34. dfs(board, x, y + 1);
  35. dfs(board, x, y - 1);
  36. }
  37. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/859503
推荐阅读
相关标签
  

闽ICP备14008679号