BFS/DFS处理二维矩阵抽象问题例题
Posted HardyDragon_CC
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BFS/DFS处理二维矩阵抽象问题例题相关的知识,希望对你有一定的参考价值。
BFS
烂橘子
这里队列存储数组下标的方式和其他的不同
class Solution {
int[] dr = new int[]{-1, 0, 1, 0};
int[] dc = new int[]{0, -1, 0, 1};
public int orangesRotting(int[][] grid) {
int R = grid.length, C = grid[0].length;
Queue<Integer> queue = new ArrayDeque<Integer>();
Map<Integer, Integer> depth = new HashMap<Integer, Integer>();
for (int r = 0; r < R; ++r) {
for (int c = 0; c < C; ++c) {
if (grid[r][c] == 2) {
// 代替 int[] 队列元素 通过除、取余获取x,y的值
int code = r * C + c;
queue.add(code);
depth.put(code, 0);
}
}
}
int ans = 0;
while (!queue.isEmpty()) {
int code = queue.remove();
int r = code / C, c = code % C;
for (int k = 0; k < 4; ++k) {
int nr = r + dr[k];
int nc = c + dc[k];
if (0 <= nr && nr < R && 0 <= nc && nc < C && grid[nr][nc] == 1) {
grid[nr][nc] = 2;
int ncode = nr * C + nc;
queue.add(ncode);
// map 存放表示第几层搜索,即返回最后烂掉的橘子需要几分钟
depth.put(ncode, depth.get(code) + 1);
ans = depth.get(ncode);
}
}
}
for (int[] row: grid) {
for (int v: row) {
if (v == 1) {
return -1;
}
}
}
return ans;
}
}
01矩阵
这里的方向移动和上面的不同
class Solution {
public int[][] updateMatrix(int[][] matrix) {
Queue<int[]> queue = new LinkedList<>();
int[] directions = {-1, 0, 1, 0, -1};
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[0].length; col++) {
if (matrix[row][col] == 0) {
queue.offer(new int[] {row, col});
} else {
//标记非零元素为负,和遍历后设定的正数距离加以区分
matrix[row][col] = -1;
}
}
}
int step = 1;
while (!queue.isEmpty()) {
//对当前队列中所有零元素遍历,所有元素向四周走一步
int size = queue.size();
for (int i = 0; i < size; i++) {
//获取队列中的元素位置
int[] cur = queue.poll();
//向四个方向依次走一步
for (int j = 0; j < directions.length - 1; j++) {
int x = cur[0] + directions[j];
int y = cur[1] + directions[j + 1];
//如果超出矩阵范围,或者遇见零元素及设置过距离step的元素则跳过,只对未遍历到的-1操作
if (x < 0 || x >= matrix.length || y < 0 || y >= matrix[0].length || matrix[x][y] >= 0) {
continue;
}
matrix[x][y] = step;
queue.offer(new int[] {x, y});
}
}
//下次遍历到的-1元素相比前一次距离step加1
step++;
}
return matrix;
}
}
图像渲染
class Solution {
int[] dx = {1,0,0,-1};
int[] dy = {0,1,-1,0};
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int oldColor = image[sr][sc];
// 新老颜色相同不变
if(oldColor == newColor){
return image;
}
int n = image.length;
int m = image[0].length;
Queue<int[]> queue = new LinkedList<int[]>();
queue.offer(new int[]{sr,sc});
image[sr][sc] = newColor;
while(!queue.isEmpty()){
int[] cell = queue.poll();
int x = cell[0];
int y = cell[1];
// 向上下左右走
for(int i=0;i<4;i++){
int mx = x + dx[i];
int my = y + dy[i];
// 需要不越界且颜色等于老颜色才入队
if(mx >=0 && mx < n && my>=0 && my < m && image[mx][my] == oldColor){
queue.offer(new int[]{mx,my});
image[mx][my] = newColor;
}
}
}
return image;
}
}
DFS
岛屿数量
class Solution {
void dfs(char[][] grid, int r, int c) {
int nr = grid.length;
int nc = grid[0].length;
if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
return;
}
grid[r][c] = '0';
// 上下左右递归dfs搜索
dfs(grid, r - 1, c);
dfs(grid, r + 1, c);
dfs(grid, r, c - 1);
dfs(grid, r, c + 1);
}
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int nr = grid.length;
int nc = grid[0].length;
int num_islands = 0;
for (int r = 0; r < nr; ++r) {
for (int c = 0; c < nc; ++c) {
if (grid[r][c] == '1') {
++num_islands;
dfs(grid, r, c);
}
}
}
return num_islands;
}
}
岛屿最大面积
class Solution {
int dfs(int[][] grid, int r, int c) {
int nr = grid.length;
int nc = grid[0].length;
if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == 0) {
return 0;
}
grid[r][c] = 0;
int l,right,t,bd
// 上下左右递归dfs搜索
l = dfs(grid, r - 1, c);
right = dfs(grid, r + 1, c);
b = dfs(grid, r, c - 1);
t = dfs(grid, r, c + 1);
return 1 + l + right + t + b;
}
public int maxAreaOfIsland(int[][] grid) {
if (grid == null || grid.length == 0) {
return 0;
}
int nr = grid.length;
int nc = grid[0].length;
int size = 0;
int res = 0;
for (int r = 0; r < nr; ++r) {
for (int c = 0; c < nc; ++c) {
if (grid[r][c] == 1) {
size = dfs(grid, r, c);
res = Math.max(res,size);
}
}
}
return res;
}
}
以上是关于BFS/DFS处理二维矩阵抽象问题例题的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode207.课程表 | BFS DFS 邻接表 邻接矩阵
LeetCode207.课程表 | BFS DFS 邻接表 邻接矩阵