当前位置:网站首页>LeetCode 733、图像渲染
LeetCode 733、图像渲染
2022-04-21 15:31:00 【亡于灬】
733、图像渲染
1)题目描述
有一幅以 m x n 的二维整数数组表示的图画 image ,其中 image[i][j] 表示该图画的像素值大小。
你也被给予三个整数 sr , sc 和 newColor 。你应该从像素 image[sr][sc] 开始对图像进行 上色填充 。
为了完成 上色工作 ,从初始像素开始,记录初始坐标的 上下左右四个方向上 像素值与初始坐标相同的相连像素点,接着再记录这四个方向上符合条件的像素点与他们对应 四个方向上 像素值与初始坐标相同的相连像素点,……,重复该过程。将所有有记录的像素点的颜色值改为 newColor 。
最后返回 经过上色渲染后的图像 。
示例 1:

输入: image = [[1,1,1],[1,1,0],[1,0,1]],sr = 1, sc = 1, newColor = 2
输出: [[2,2,2],[2,2,0],[2,0,1]]
解析: 在图像的正中间,(坐标(sr,sc)=(1,1)),在路径上所有符合条件的像素点的颜色都被更改成2。
注意,右下角的像素没有更改为2,因为它不是在上下左右四个方向上与初始点相连的像素点。
示例 2:
输入: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, newColor = 2
输出: [[2,2,2],[2,2,2]]
提示:
m == image.lengthn == image[i].length1 <= m, n <= 500 <= image[i][j], newColor < 2^160 <= sr < m0 <= sc < n
2)分析
深度优先搜索: 我们从给定的起点开始,进行深度优先搜索。每次搜索到一个方格时,如果其与初始位置的方格颜色相同,就将该方格的颜色更新,以防止重复搜索;如果不相同,则进行回溯。深度优先搜索回溯的过程实际上就是递归,因此使用递归来实现深度优先搜索。
广度优先搜索: 从给定的起点开始,进行广度优先搜索。每次搜索到一个方格时,如果其与初始位置的方格颜色相同,就将该方格加入队列,并将该方格的颜色更新,以防止重复入队。
3)C++代码
深度优先搜索
class Solution {
public:
int dx[4]={
0,1,0,-1};
int dy[4]={
1,0,-1,0};
void dfs(vector<vector<int>>& image,int x,int y,int oldColor,int newColor){
if(image[x][y]==oldColor){
image[x][y]=newColor;
for(int i=0;i<4;i++){
int newX=x+dx[i];
int newY=y+dy[i];
if(newX>=0&&newX<image.size()&&newY>=0&&newY<image[0].size())
dfs(image,newX,newY,oldColor,newColor);
}
}
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int curColor=image[sr][sc];
if(curColor!=newColor)
dfs(image,sr,sc,curColor,newColor);
return image;
}
};
广度优先搜索
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int dx[4]={
0,1,0,-1};
int dy[4]={
1,0,-1,0};
if(image[sr][sc]==newColor)
return image;
queue<pair<int,int>> que;
int curColor=image[sr][sc];
que.emplace(sr,sc);
image[sr][sc]=newColor;
while(!que.empty()){
int x=que.front().first;
int y=que.front().second;
que.pop();
for(int i=0;i<4;i++){
int newX=x+dx[i];
int newY=y+dy[i];
if(newX>=0&&newX<image.size()&&newY>=0&&newY<image[0].size()&&image[newX][newY]==curColor){
que.emplace(newX,newY);
image[newX][newY]=newColor;
}
}
}
return image;
}
};
版权声明
本文为[亡于灬]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38342510/article/details/124316066
边栏推荐
- 智慧工地综合解决方案
- IT服务管理框架要怎么落地?看这篇就够了
- Latex common formula query
- Introduction to openlayers (II)
- What is an email address? Easy to use email registration application
- Simple explanation of impermanence loss
- 一级等保怎么做?要收费吗?等保要求是什么?
- 49页企业数字化转型案例及常用工具企业数字化转型思路
- mysql 将某个字段修改成自增
- Universal navigation: a concise and practical comprehensive navigation website
猜你喜欢

机器学习方法创建可学习的化学语法,构建可合成的单体和聚合物

111頁精細化工股份公司數據字轉型解决方案

Spark / Scala - read rcfile & orcfile

Page 92 group informatization planning scheme enterprise application integration solution

MySQL通过Binlog恢复数据

What mailbox do foreign trade companies usually use and how to send e-mail in groups?

Hanoi tower game and recursion

What is an email address? Easy to use email registration application

JUC学习记录

What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
随机推荐
58页西门子机床行业数字化解决方案
105 page digital twin city information model CIM platform construction technical scheme
Deltix Round, Summer 2021 D. Take a Guess(二进制计算常用性质汇总)
Latex common formula query
智慧工地综合解决方案
GLASS:用于子图表示学习的 GNN 标签技巧
Spark / Scala - read rcfile & orcfile
111 page data word transformation solution of Fine Chemical Co., Ltd
嵌入式驱动模块的加载与卸载
2的0次方为什么等于1?
Deltix Round, Summer 2021 E. Equilibrium
外贸公司一般用什么邮箱,电子邮件如何群发?
用户交互、格式化输出、基本运算符
小程序简介和开发工具
MySQL-IF-THEN-ELSE语句
50页京东云·睿擎-打造企业数字化转型的敏捷引擎业务中台解决方案
How to set start date query statement in SQL Server
LaTeX常用公式查询
mysql中判断某一条数据是否存在
Web.xml文件详解