方法一: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. python开发进程:进程开启方式&多进程

    一,进程的开启方式 利用模块开启进程 from multiprocessing import Process import time,random import os def piao(name): ...

  2. VS2017自动添加头部注释

    让VS自动生成类的头部注释,只需修改两个文集即可,一下两个路径下个有一个 Class.cs文件 D:\Program Files (x86)\Microsoft Visual Studio\2017\ ...

  3. Vue 简单的总结一

    let 变量 1. 局部作用域 2. 不会存在变量提升 3. 变量不能重复声明 const 变量 1. 局部作用域 2. 不会存在变量提升 3. 变量不能重复声明 4. 只能声明常量,不可变得量 th ...

  4. 关于datatable对象的用法

    在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...

  5. SpringMVC 接收表单数据的方式

    1.@RequestParam @RequestMapping(value = "/xxxx.do") public void create(@RequestParam(value ...

  6. Java如何解决form表单上传文件,以及页面返回处理结果通知!

    前端JSP代码 <form id='formSumbit' class='form-horizontal' action='/ncpay/route/chlsubmcht/batchImpor' ...

  7. wordpress 学习笔记

    (1) __()函数 function __( $text, $domain = 'default' ) { return translate( $text, $domain ); } 返回一个字符串 ...

  8. sql合并列

    oralce写法: select WM_CONCAT(A.title) as citys from tmpcity A sql server写法: select stuff((select ','+A ...

  9. Timer定时函数的用法

  10. Eclipse右击jsp没有运行选项

    maven项目低级错误,没有更新maven资源库.....更新后就运行起来了