方法一:dfs的非递归形式

  using ll=long long;
const ll MAXN=50LL;
unordered_set<ll> vis,mark;
vector<vector<int>> colorBorder(vector<vector<int>>& G, int r0, int c0, int color) {
queue<ll> Q;
Q.push(r0*MAXN+c0);
int c=G[r0][c0];
int dx[]={-,,,},dy[]={,,-,};
while (!Q.empty())
{
int x=Q.front()/MAXN;
int y=Q.front()%MAXN;
Q.pop();
vis.insert(x*MAXN+y);
if (x==||x==G.size()-||y==||y==G[].size()-) // 边界方块可变色
mark.insert(x*MAXN+y);
else if (G[x-][y]!=c||G[x+][y]!=c||G[x][y-]!=c||G[x][y+]!=c) // 四个方向中,任意一个方块颜色不同,则可变色
mark.insert(x*MAXN+y);
for (int d=;d<;d++) // 放入连通分量的所有方块
{
int nx=x+dx[d],ny=y+dy[d];
if (<=nx&&nx<G.size()&&<=ny&&ny<G[].size()&&!vis.count(nx*MAXN+ny)&&G[nx][ny]==c)
Q.push(nx*MAXN+ny);
}
}
for (auto it:mark)
G[it/MAXN][it%MAXN]=color;
return G;
}

思路:用vis记录访问过的方块,mark标记连通分量中需要修改颜色的方块,并非连通分量中所有的方块都要修改颜色,比如:一个方块如果四周(四个方向邻接的)都是相同颜色,那么只需要修改四周方块的颜色,而自己颜色不变(开始的时候没理解题意,以为只要是连通分量内的方块颜色都需要改变)

方法二: dfs递归形式,只不过把上面的非递归改为递归了

    using ll=long long;
const ll MAXN=50LL;
unordered_set<ll> vis,mark;
void dfs(vector<vector<int>>& G, int x, int y, int c)
{
int dx[]={-,,,},dy[]={,,-,};
vis.insert(x*MAXN+y);
if (x==||x==G.size()-||y==||y==G[].size()-) // 边界方块可变色
mark.insert(x*MAXN+y);
else if (G[x-][y]!=c||G[x+][y]!=c||G[x][y-]!=c||G[x][y+]!=c) // 四个方向中,任意一个方块颜色不同,则可变色
mark.insert(x*MAXN+y);
for (int d=;d<;d++) // 放入连通分量的所有方块
{
int nx=x+dx[d],ny=y+dy[d];
if (<=nx&&nx<G.size()&&<=ny&&ny<G[].size()&&!vis.count(nx*MAXN+ny)&&G[nx][ny]==c)
dfs(G,nx,ny,c);
}
}
vector<vector<int>> colorBorder(vector<vector<int>>& G, int r0, int c0, int color) {
dfs(G,r0,c0,G[r0][c0]);
for (auto it:mark)
G[it/MAXN][it%MAXN]=color;
return G;
}

方法三:dfs递归,但通过修改G中的数据,来记录是否访问过,和是否需要修改颜色,国外的一个大佬写的

From an initial point, perform DFS and flip the cell color to negative to track visited cells.

After DFS is complete for the cell, check if this cell is inside. If so, flip its color back to the positive.

In the end, cells with the negative color are on the border. Change their color to the target color.

void dfs(vector<vector<int>>& g, int r, int c, int cl) {
if (r < || c < || r >= g.size() || c >= g[r].size() || g[r][c] != cl) return; // 剪枝(越界,非着色块)
g[r][c] = -cl; // 着色
dfs(g, r - , c, cl), dfs(g, r + , c, cl), dfs(g, r, c - , cl), dfs(g, r, c + , cl);
if (r > && r < g.size() - && c > && c < g[r].size() - && cl == abs(g[r - ][c]) &&
cl == abs(g[r + ][c]) && cl == abs(g[r][c - ]) && cl == abs(g[r][c + ])) // 将原四周同色的块,颜色还原
g[r][c] = cl;
}
vector<vector<int>> colorBorder(vector<vector<int>>& grid, int r0, int c0, int color) {
dfs(grid, r0, c0, grid[r0][c0]);
for (auto i = ; i < grid.size(); ++i) // 根据dfs标记(负数)过的方块进行着色
for (auto j = ; j < grid[i].size(); ++j) grid[i][j] = grid[i][j] < ? color : grid[i][j];
return grid;
}

结论: 无论是递归还是非递归,先标记(标记vis),再遍历

【leetcode 5040. 边框着色】解题报告的更多相关文章

  1. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. 固定容器内任意个div填充算法

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. springboot成神之——mybatis和mybatis-generator

    项目结构 依赖 generator配置文件 properties配置 生成文件 使用Example 本文讲解如何在spring-boot中使用mybatis和mybatis-generator自动生成 ...

  3. selenium webdriver 的事件处理

    package www.zr.com; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; imp ...

  4. 用Python语言设计GUI界面

    我们大家都编写过程序,但是如果能够设计一个GUI界面,会使程序增添一个很大的亮点!今天就让我们来用目前十分流行的python语言写出一个最基本的GUI,为日后设计更加漂亮的GUI打下基础. 工具/原料 ...

  5. Halcon学习之二:摄像头获取图像和相关参数

    1.close_all_framegrabbers ( : : : ) 关闭所有图像采集设备. 2.close_framegrabber ( : : AcqHandle : ) 关闭Handle为Ac ...

  6. spring cloud微服务搭建(一)

    martin fowler大神提出微服务的概念后,各种微服务的技术满天飞,现在用的比较多的是spring cloud和阿里的dubbo,由于dubbo 在16年10月份就停止更新了,不过好像前些天又更 ...

  7. mysql日期获取

    获取当前日期在本周的周一:select subdate(curdate(),date_format(curdate(),'%w')-1) 获取当前日期在本周的周日:select subdate(cur ...

  8. consul watch

    consul watch -type key -key mhc ./key_handler.py [root@mhc consul]# cat key_handler.py #!/usr/bin/py ...

  9. 浅谈块元素绝对定位的margin属性

    对于div的绝对定位一直以为margin属性是无效的,只是通过top,left,bottom,right定位,然而今天的却发现不是这样的,于是对其做了些实验: 使用的HTML原始测试文件: <! ...

  10. codeforce 460DIV2 D题

    感觉这个题不错,对拓扑排序有了更深的了解,用两种拓扑排序都写了些试试. dfs #include <cstdio> #include <algorithm> #include ...