方法一: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. 微信小程序之页面跳转

    方式一: <navigator class="menu_block" url="/pages/address/address"> <text ...

  2. python开发面向对象进阶:反射&内置函数

    isinstance和issubclass isinstance(obj,cls)检查是否obj是否是类 cls 的对象或者子类的对象 class Foo(object): pass class ba ...

  3. tomcat7.0在centos7下中文乱码问题解决汇总

    1. 系统级的中文设置 Centos7跟之前的6和5安装的中文字符命令都不一样 [root@iZ25bdzgev8Z ~]# locale #查看当前系统的语言环境 [root@iZ25bdzgev8 ...

  4. 第十五章 MySQL日志(待续)

    ·······

  5. 【js与jquery】javascript中url编码与解码

    本文主要针对URI编解码的相关问题做了介绍,对Url编码中哪些字符需要编码.为什么需要编码做了详细的说明,并对比分析了Javascript 中和 编解码相关的几对函数 编码/解码 escape / u ...

  6. Oracle使用split和splitstr函数批量分隔字符串

    /* * Oracle 创建 split 和 splitstr 函数 */ /* 创建一个表类型 */ ) / /* 创建 split 函数 */ CREATE OR REPLACE FUNCTION ...

  7. JS播放声音 兼容所有浏览器

    JS播放声音 兼容所有浏览器 <!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http ...

  8. ResponseUtil

    package util; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.http.Htt ...

  9. C#使用ADO操作Excel

    1 说明 把excel当成一个数据库,类似于Access数据库来操作. 2 源代码 2.1 Model层 /// <summary> /// 人员信息 /// </summary&g ...

  10. tomcat配置多个host

    当一个tomcat需要配多个应用时,并且内网和外网的访问IP还不一样,就需要使用到tomcat配置多个虚拟主机. <Host name="localhost"  appBas ...