链接:
https://leetcode-cn.com/problems/flood-fill/submissions/
描述:
示例和注意:
代码:
深度优先搜索
C++
int nextP[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};
class Solution {
public:
void DFS(vector<vector<int>>& image,int row,int col,vector<vector<int>>& book
,int curX,int curY,int oldcolor,int newColor)
{
//当前位置修改颜色
image[curX][curY] = newColor;
book[curX][curY] = 1;
//搜索上下左右
for(int i = 0;i<4;++i)
{
int newX = curX + nextP[i][0];
int newY = curY + nextP[i][1];
//判断新的位置是否越界
if(newX>=row || newX <0 || newY>= col || newY <0)
continue;
//判断是否需要渲染
if(image[newX][newY] == oldcolor && book[newX][newY] == 0)
DFS(image,row,col,book,newX,newY,oldcolor,newColor);//处理新的位置
}
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if(image.empty()) return image;
int row = image.size();
int col = image[0].size();
vector<vector<int>> book(row,vector<int>(col,0));
int oldcolor = image[sr][sc];
DFS(image,row,col,book,sr,sc,oldcolor,newColor);
return image;
}
};
C
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int m;
int n;
int value;
void dfs(int **image, int sr, int sc, int newColor){
if(sr >= m || sr < 0 || sc >= n || sc < 0)
return;
if(image[sr][sc] == value){
image[sr][sc] = newColor;
dfs(image, sr - 1, sc, newColor);
dfs(image, sr, sc - 1, newColor);
dfs(image, sr + 1, sc, newColor);
dfs(image, sr, sc + 1, newColor);
}
}
int** floodFill(int** image, int imageSize, int* imageColSize, int sr, int sc, int newColor, int* returnSize, int** returnColumnSizes){
m = imageSize;
n = imageColSize[0];
value = image[sr][sc];
if(value != newColor)
dfs(image, sr, sc, newColor);
*returnSize = imageSize;
*returnColumnSizes = imageColSize;
return image;
}
广度优先搜索:
C++
class Solution {
public:
const int dx[4] = {1, 0, 0, -1};
const int dy[4] = {0, 1, -1, 0};
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int currColor = image[sr][sc];
if (currColor == newColor) return image;
int n = image.size(), m = image[0].size();
queue<pair<int, int>> que;
que.emplace(sr, sc);
image[sr][sc] = newColor;
while (!que.empty()) {
int x = que.front().first, y = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int mx = x + dx[i], my = y + dy[i];
if (mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor) {
que.emplace(mx, my);
image[mx][my] = newColor;
}
}
}
return image;
}
};
C
const int dx[4] = { 1, 0, 0, -1 };
const int dy[4] = { 0, 1, -1, 0 };
int n, m;
void dfs(int** image, int x, int y, int color, int newColor) {
if (image[x][y] == color) {
image[x][y] = newColor;
for (int i = 0; i < 4; i++) {
int mx = x + dx[i], my = y + dy[i];
if (mx >= 0 && mx < n && my >= 0 && my < m) {
dfs(image, mx, my, color, newColor);
}
}
}
}
int** floodFill(int** image, int imageSize, int* imageColSize, int sr, int sc, int newColor, int* returnSize, int** returnColumnSizes) {
n = imageSize, m = imageColSize[0];
*returnSize = n;
for (int i = 0; i < n; i++) {
(*returnColumnSizes)[i] = m;
}
int currColor = image[sr][sc];
if (currColor != newColor) {
dfs(image, sr, sc, currColor, newColor);
}
return image;
}