方法一: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. springmvc防止表单重复提交demo

    原理:在去某个页面直接生成一个随机数(这里使用的是UUID)并放入session中,用户提交表单时将这个随机数传入服务端与session中的值进行比较,如果不不存在或不相等,则认为是重复提交:如果相等 ...

  2. ctf中检测和分离隐藏的文件

    使用binwalk检测是否隐藏了文件 root@sch01ar:~# binwalk '/root/桌面/test.jpg' 还藏了一个zip文件,接下来用foremost来分离文件 root@sch ...

  3. 本人编写的一份前端vue面试题

    说明,此题目本人自出,做过本人所在公司的前端面试题,在此共享给大家 1. 如何在vue组件中实现v-model的功能?(只需给出关键代码) 2. 简述你知道的生命周期函数和执行时机 3. 谈谈你对计算 ...

  4. 1.4 Application应用

    使用celery第一件要做的最为重要的事情是需要先创建一个Celery实例,我们一般叫做celery应用,或者更简单直接叫做一个app.app应用是我们使用celery所有功能的入口,比如创建任务,管 ...

  5. Java微信公众平台开发(十六)--微信网页授权(OAuth2.0授权)获取用户基本信息

    转自:http://www.cuiyongzhi.com/post/78.html 好长时间没有写文章了,主要是最近的工作和生活上的事情比较多而且繁琐,其实到现在我依然还是感觉有些迷茫,最后还是决定静 ...

  6. Python 小练习二 数据库MySQL、Redis

    import pymysql,redis def op_mysql(host,user,password,db,sql,port=3306,charset='utf8'): conn = pymysq ...

  7. ansible基本使用

    ansible介绍 基础概念 ansible是个配置管理工具,可以批量处理一些任务.ansible只需要依赖ssh即可使用,而不需要在受管主机上安装客户端工具. ansible具有幂等性,即以结果为导 ...

  8. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 2_Linear regression with one variable 单变量线性回归

    Lecture2   Linear regression with one variable  单变量线性回归 2.1 模型表示 Model Representation 2.1.1  线性回归 Li ...

  9. easylogging++学习记录(二):流式日志

    easylogging++日志库流式日志的写入,依赖于el::base::Writer类的析构,以debug日志为例:具体代码如下: #define LOG(LEVEL) CLOG(LEVEL, EL ...

  10. 图论算法》关于SPFA和Dijkstra算法的两三事

    本来我是想把这两个算法分开写描述的,但是SPFA其实就是Dijkstra的稀疏图优化,所以其实代码差不多,所以就放在一起写了. 因为SPFA是Dijkstra的优化,所以我想来讲讲Dijkstra. ...