130. 被围绕的区域
- 原题链接:
- 完成情况:
- 解题思路:
- 参考代码:
- _130被围绕的区域_dfs
- _130被围绕的区域_bfs_01
- 错误经验吸取
原题链接:
添加链接描述130. 被围绕的区域
https://leetcode.cn/problems/surrounded-regions/description/
完成情况:
解题思路:
这段代码实现了一个深度优先遍历算法,用于解决一个矩阵问题。下面是对代码的清晰、简洁和可读性强的解释:
- 首先定义了一个二维数组
position
,包含四个方向的偏移量,分别为上、右、下、左。 dfs
方法用于深度优先遍历矩阵,从指定的位置(row, col)
开始向四个方向进行搜索。solve
方法用于解决矩阵问题,首先从左右两侧开始遍历,标记连通的区域为特殊值’A’,并调用dfs
方法进行深度优先搜索。- 然后从上下两边开始遍历,同样标记连通的区域为特殊值’A’,不需要遍历四个角。
- 最后遍历整个矩阵,将未被标记的’O’修改为’X’,将特殊值’A’修改回’O’。
这段代码的主要目的是将矩阵中被包围的区域中的’O’修改为’X’,被包围的条件是指该区域中的’O’必须与边界相连。
参考代码:
_130被围绕的区域_dfs
package 代码随想录.图论;public class _130被围绕的区域_dfs {int position[][] = {{1,0},{-1,0},{0,1},{0,-1}};/**** @param board*/public void solve(char[][] board) {int row = board.length,column = board[0].length;boolean[][] visited = new boolean[row][column];//一样的,先遍历掉外围for (int r = 0; r < row; r++) {if (board[r][0] == 'O' && !visited[r][0]){visited[r][0] = true;dfs_solve(board,row,0,visited);}if (board[r][column-1] == 'O' && !visited[r][column-1]){visited[r][column-1] = true;dfs_solve(board,r,column-1,visited);}}for (int c = 1;c < column-1;c++){if (board[0][c] == 'O' && !visited[0][c]){visited[0][c] = true;dfs_solve(board,0,c,visited);}if (board[row-1][c] == 'O' && !visited[row-1][c]){visited[row-1][c] = true;dfs_solve(board,row-1,c,visited);}}//将剩下部分全部判断并修改for (int r = 0;r<row;r++){for (int c = 0;c<column;c++){if (board[r][c] == 'O' && !visited[r][c]) board[r][c] = 'X';}}}private void dfs_solve(char[][] board, int row, int column, boolean[][] visited) {for (int [] pos : position){int nextRow = row + pos[0],nextCol = column + pos[1];//位置越界if (nextRow < 0 || nextRow >= board.length || nextCol < 0 || nextCol >= board[0].length) continue;if (visited[nextRow][nextCol] || board[nextRow][nextCol] != 'O') continue;visited[nextRow][nextCol] = true;dfs_solve(board,nextRow,nextCol,visited);}}}
_130被围绕的区域_bfs_01
package 代码随想录.图论;import java.util.ArrayDeque;
import java.util.Deque;public class `_130被围绕的区域_bfs_01` {int position[][] = {{1,0},{-1,0},{0,1},{0,-1}};/**A* 将所有与边界无关的‘0’全部替换成‘X’即可。* @param board*/public void solve(char[][] board) {int row = board.length,column = board[0].length;boolean[][] visited = new boolean[row][column];Deque<int []> myQueue = new ArrayDeque<int[]>();//先遍历外围边for (int r = 0;r < row;r++) {if (board[r][0] == 'O'){visited[r][0] = true;myQueue.add(new int[]{r,0});}if (board[r][column - 1] == 'O'){visited[r][column - 1] = true;myQueue.add(new int[]{r,column-1});}}//此处存在重复检查,不过影响不大的话,无所谓╮(╯_╰)╭for (int c = 1;c < column - 1;c++){if (board[0][c] == 'O'){visited[0][c] = true;myQueue.add(new int[]{0,c});}if (board[row-1][c] == 'O'){visited[row-1][c] = true;myQueue.add(new int[]{row-1,c});}}//然后对队列里面的元素进行四个角的循环找‘1’即可。while (!myQueue.isEmpty()){int curQueue [] = myQueue.poll();for (int pos [] : position){int r = curQueue[0] + pos[0],c = curQueue[1] + pos[1];//防止越界if (r < 0 || r >= row || c < 0 || c >= column) continue;if (visited[r][c] || board[r][c] != '0') continue;visited[r][c] = true;myQueue.add(new int[]{r,c});}}//最后遍历数组,把所有剩下的未访问到的’O‘改成’X‘for (int r = 0;r <row;r++){for (int c = 0;c < column;c++){if (board[r][c] == 'O' && !visited[r][c]){board[r][c] = 'X';}}}}
}