前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >leetcode-733-Flood Fill

leetcode-733-Flood Fill

作者头像
chenjx85
发布2018-08-01 15:40:39
8330
发布2018-08-01 15:40:39
举报

题目描述:

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.

To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.

At the end, return the modified image.

Example 1:

代码语言:javascript
复制
Input: 
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation: 
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected 
by a path of the same color as the starting pixel are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected
to the starting pixel.

Note:

  • The length of image and image[0] will be in the range [1, 50].
  • The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
  • The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

要完成的函数:

vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) 

说明:

1、这道题给定一个二维vector表示一张图片,一个sr表示起始点的行坐标,sc表示起始点的纵坐标,要求从起始点开始,找到四个方向上跟起始点相同颜色的像素点,把起始点以及周边找到的点都给改变颜色,变成给定的newcolor。之后再从周边找到的点往外扩散,相同方法找到新的点,继续改变颜色……

2、这是一道DFS或者BFS的题目,笔者对于BFS比较熟悉,也就写成了BFS。

代码如下:(附详解)

代码语言:javascript
复制
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) 
    {
        int oldcolor=image[sr][sc];
        if(oldcolor==newColor)return image;//如果旧的颜色和新的颜色一样,那么根本不需要改变,返回原本的image就好了
        queue<int>q1;//定义一个队列
        q1.push(sr);//塞入行坐标
        q1.push(sc);//塞入纵坐标
        while(!q1.empty())//当队列非空的情况下,迭代处理
        {
            sr=q1.front();//取出行坐标
            q1.pop();
            sc=q1.front();//取出列在坐标
            q1.pop();
            image[sr][sc]=newColor;//改变当前点的颜色,同时避免之后的重复寻找
            if(sr-1>=0&&image[sr-1][sc]==oldcolor)//判断上方点是不是相同颜色
            {
                q1.push(sr-1);
                q1.push(sc);
            }
            if(sr+1<image.size()&&image[sr+1][sc]==oldcolor)//判断下方点是不是相同颜色
            {
                q1.push(sr+1);
                q1.push(sc);
            }
            if(sc-1>=0&&image[sr][sc-1]==oldcolor)//判断左边的点是不是相同颜色
            {
                q1.push(sr);
                q1.push(sc-1);
            }
            if(sc+1<image[0].size()&&image[sr][sc+1]==oldcolor)//判断右边的点是不是相同颜色
            {
                q1.push(sr);
                q1.push(sc+1);
            }
        }
        return image;
    }

上述代码实测35ms,beats 68.76% of cpp submissions。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-06-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述:
  • 要完成的函数:
  • 说明:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档