1. 程式人生 > >leetcode-733-Flood Fill

leetcode-733-Flood Fill

coord all ssi 二維 dfs and spa represent ger

題目描述:

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:

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。

代碼如下:(附詳解)

    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。

leetcode-733-Flood Fill