首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【图论搜索专题】常规图论搜索题(含「图论搜索专题」目录)

【图论搜索专题】常规图论搜索题(含「图论搜索专题」目录)

作者头像
宫水三叶的刷题日记
发布2022-02-09 08:00:20
发布2022-02-09 08:00:20
1.3K0
举报

题目描述

这是 LeetCode 上的「1034. 边界着色」,难度为「中等」

Tag : 「图论 DFS」、「图论 BFS」

给你一个大小为 m x n 的整数矩阵 grid,表示一个网格。另给你三个整数 rowcolcolor 。网格中的每个值表示该位置处的网格块的颜色。

当两个网格块的颜色相同,而且在四个方向中任意一个方向上相邻时,它们属于同一 连通分量 。

连通分量的边界 是指连通分量中的所有与不在分量中的网格块相邻(四个方向上)的所有网格块,或者在网格的边界上(第一行/列或最后一行/列)的所有网格块。

请你使用指定颜色 color 为所有包含网格块 grid[row][col] 的 连通分量的边界 进行着色,并返回最终的网格 grid

示例 1:

代码语言:javascript
复制
输入:grid = [[1,1],[1,2]], row = 0, col = 0, color = 3

输出:[[3,3],[3,2]]

示例 2:

代码语言:javascript
复制
输入:grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3

输出:[[1,3,3],[2,3,3]]

示例 3:

代码语言:javascript
复制
输入:grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2

输出:[[2,2,2],[2,1,2],[2,2,2]]

提示:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • 1 <= grid[i][j], color <= 1000
  • 0 <= row < m
  • 0 <= col < n

基本分析

基本题意为:从题目给定的 (row, col) 进行出发,如果遍历到 连通分量的边界 格子,则使用 color 进行上色。

同一「连通分量」的「非边界」格子满足:当前格子的四联通方向均存在相邻格子,且当前格子与四联通相邻格子颜色一致。

也就是说,我们从 (row, col) 进行出发,遍历 (row, col) 所在的「连通分量」,如果遍历到的「连通分量」格子不满足上述条件(边界格子),则进行上色。

BFS

具体的,我们可以使用 BFS 进行求解:

  • 构造 ans 矩阵作为答案,同时 ans 也作为判重数组使用(通过判断 ans[i][j] 是否为 0 来得知是否被处理);
  • 起始时,将 (row, col) 位置进行入队,每次从队列中取出元素进行「四联通拓展」:
    • 拓展格子必须与起点格子处于同一「连通分量」,即满足两者起始颜色相同;
    • 进行「四联通拓展」的同时,记录当前出队是否为边界格子。若为边界格子,则使用 color 进行上色;
  • 跑完 BFS 后,对 ans 进行遍历,将未上色(ans[i][j] = 0 )的位置使用原始色( grid[i][j] )进行上色。

代码:

代码语言:javascript
复制
class Solution {
    public int[][] colorBorder(int[][] grid, int row, int col, int color) {
        int m = grid.length, n = grid[0].length;
        int[][] ans = new int[m][n];
        int[][] dirs = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}};
        Deque<int[]> d = new ArrayDeque<>();
        d.addLast(new int[]{row, col});
        while (!d.isEmpty()) {
            int[] poll = d.pollFirst();
            int x = poll[0], y = poll[1], cnt = 0;
            for (int[] di : dirs) {
                int nx = x + di[0], ny = y + di[1];
                if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
                if (grid[x][y] != grid[nx][ny]) continue;
                else cnt++;
                if (ans[nx][ny] != 0) continue;
                d.addLast(new int[]{nx, ny});
            }
            ans[x][y] = cnt == 4 ? grid[x][y] : color;
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (ans[i][j] == 0) ans[i][j] = grid[i][j];
            }
        }
        return ans;
    }
}
  • 时间复杂度:O(m * n)
  • 空间复杂度:O(m * n)

DFS

同理,可以使用 DFS 进行求解。

由于使用 DFS 搜索时,我们使用「栈帧压栈/弹栈」作为拓展联通节点的容器,且仅在出队时进行上色。为防止「重复入队」问题,我们需要先在对节点 (nx, ny) 入队时,先设置将ans[nx][ny] 设置为 -1 标识位,以作为判重依据。

代码:

代码语言:javascript
复制
class Solution {
    int m, n, c;
    int[][] grid, ans;
    int[][] dirs = new int[][]{{1,0}, {-1,0}, {0,1}, {0,-1}};
    public int[][] colorBorder(int[][] _grid, int row, int col, int color) {
        grid = _grid; c = color;
        m = grid.length; n = grid[0].length;
        ans = new int[m][n];
        dfs(row, col);
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (ans[i][j] == 0) ans[i][j] = grid[i][j];
            }
        }
        return ans;
    }
    void dfs(int x, int y) {
        int cnt = 0;
        for (int[] di : dirs) {
            int nx = x + di[0], ny = y + di[1];
            if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
            if (grid[x][y] != grid[nx][ny]) continue;
            else cnt++;
            if (ans[nx][ny] != 0) continue;
            ans[nx][ny] = -1;
            dfs(nx, ny);
        }
        ans[x][y] = cnt == 4 ? grid[x][y] : c;
    }
}
  • 时间复杂度:O(m * n)
  • 空间复杂度:O(m * n)

图论搜索(目录)

其实「图论搜索」已经更新了一段时间了,但是一直偷懒没整理目录 🤣

于是重新梳理了一下:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-01-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 宫水三叶的刷题日记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 基本分析
  • BFS
  • DFS
  • 图论搜索(目录)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档