leetcode733.图像渲染C/C++

链接:

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值