0、前言
本篇博客是力扣上 733. 图像渲染 题的一篇题解,写下这篇博客原因一个是 此题是BFS
和 DFS
两种算法的练习题,一个是因为这个与 作文以记之 ~ 岛屿数量 中的练习题可以说是一脉相承,或者说一个可以给另一个打基础,详细情况可点击上述链接进行对比分析!
GIthub上相关内容可 点击此处 进行查看!
1、题目描述
2、解题思路
2.1 方法1 ~ 利用BFS
2.1.1 思路
这个题主要是要找出整个图像中与起始点位置处像素值相同的位置,然后将其像素值转换为目标像素值即可,此题结合队列进行实现就很简单,具体如下。
- 先将起始点位置加入队列
que
,以此点为根据点,以此位置的像素值为判断标准,向周围开始搜索,当找到满足条件的位置,先将其值变为目标像素值,并将该位置插入que
中,方便后续迭代操作! - 每次迭代都会从
que
中取出队首元素,并在que
中将其删除,防止后续产生误操作。同时,迭代条件与其他BFS
例题一致,都是以que
是否为空 进行判断,如果为空则跳出循环,非空则继续进行操作! - 需要注意的是,程序开始前需判断起始位置的像素值是否与目标像素值一致,一致则直接返回原图像,不同则进行后续操作。此种操作必须在程序中出现,不管是前或后,如果没有会导致后续操作进入死循环!
2.1.2 程序代码
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int obj = image[sr][sc];
if(obj == newColor) //判断起始点的像素值是否与目标像素值一致,一致则返回原图
return image;
int rowmax = image.size(),colmax = image[0].size();
image[sr][sc] = newColor;
queue<pair<int,int>> que;
que.push({sr,sc});
while(!que.empty())//开始遍历
{
auto cur = que.front();
que.pop();
int row = cur.first,col = cur.second;
if((row - 1) >= 0 && image[row-1][col] == obj) //向上
{
que.push({row-1,col});
image[row-1][col] = newColor;
}
if((row + 1) < rowmax && image[row+1][col] == obj) //向下
{
que.push({row+1,col});
image[row+1][col] = newColor;
}
if((col - 1) >= 0 && image[row][col-1] == obj) //向左
{
que.push({row,col-1});
image[row][col-1] = newColor;
}
if((col + 1) < colmax && image[row][col+1] == obj) //向右
{
que.push({row,col+1});
image[row][col+1] = newColor;
}
}
return image;
}
};
2.2 方法2 ~ 使用DFS
2.2.1 思路
与2.1.1
中的思路大致相似,此处只不过是将迭代转为了递归,同样的解决思路,此处我就不再赘述了,因为我觉得有浪费口水的时间各位大佬应该都就看懂了~
2.2.2 程序代码
class Solution {
public:
void dfs(vector<vector<int>>& image, int row, int col, int obj, int newColor)
{
int rowmax = image.size(),colmax = image[0].size();
image[row][col] = newColor;
if((row - 1) >= 0 && image[row-1][col] == obj) //向上
dfs(image,row-1,col,obj,newColor);
if((row + 1) < rowmax && image[row+1][col] == obj) //向下
dfs(image,row+1,col,obj,newColor);
if((col - 1) >= 0 && image[row][col-1] == obj) //向左
dfs(image,row,col-1,obj,newColor);
if((col + 1) < colmax && image[row][col+1] == obj) //向右
dfs(image,row,col+1,obj,newColor);
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int obj = image[sr][sc];
if(obj == newColor) //判断起始点的像素值是否与目标像素值一致,一致则返回原图
return image;
dfs(image,sr,sc,obj,newColor);
return image;
}
};
注:上述代码有点冗杂,可基于原理进行优化,比如找寻当前位置的周围位置元素时,可利用数组形式去寻找,就比如上述程序中遍历位置的伪代码主要为
if((row - 1) >= 0 && image[row-1][col] == obj) //向上
...
if((row + 1) < rowmax && image[row+1][col] == obj) //向下
...
if((col - 1) >= 0 && image[row][col-1] == obj) //向左
...
if((col + 1) < colmax && image[row][col+1] == obj) //向右
...
将其改成利用数组遍历位置可为
int pos[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };//用于寻找当前位置的上下左右位置
for (int i = 0; i < 4; i++)
{
int row = r + pos[i][0], col = c + pos[i][1];
if (row >= 0 && row < rowmax && col >= 0 && col < colmax && cpy[row][col] != 0)
...
}