
这个题目作为dfs到FloodFill的过渡,FloodFill算法解决的问题就是在找相同的区域块,在本题中,题目给出了具体的位置,而且只有一片区域,那么只需要对这个区域做一个dfs即可,和之前的二维dfs思路和代码设计过程是一样的
- class Solution
- {
- int newcolor;
- int oldcolor;
- int m,n;
- public:
- vector
int>> floodFill(vectorint>>& image, int sr, int sc, int color) - {
- if(image[sr][sc] == color)
- return image;
- newcolor = color;
- oldcolor = image[sr][sc];
- m = image.size();
- n = image[0].size();
- dfs(image,sr,sc);
- return image;
- }
- int dx[4] = {-1,1,0,0};
- int dy[4] = {0,0,-1,1};
- void dfs(vector
int >>& image,int row,int col) - {
- image[row][col] = newcolor;
- for(int k = 0;k<4;k++)
- {
- int x = row + dx[k];
- int y = col + dy[k];
-