怎样做信息收费网站,赣州91人才网赣州招聘,咨询公司成本费用包括哪些内容,广州网站建设系统flood fill 算法常常用来找极大连通子图#xff0c;这是必须掌握的基本算法之一#xff01;
图形渲染 算法原理
我们可以利用DFS遍历数组把首个数组的值记为color#xff0c;然后上下左右四个方向遍历二维数组数组如果其他方块的值不等于color 或者越界就剪枝 return
代码…flood fill 算法常常用来找极大连通子图这是必须掌握的基本算法之一
图形渲染 算法原理
我们可以利用DFS遍历数组把首个数组的值记为color然后上下左右四个方向遍历二维数组数组如果其他方块的值不等于color 或者越界就剪枝 return
代码实现
class Solution {
public:int row,col;int color;bool visted[50][50];int dx[4]{0,0,-1,1};int dy[4]{-1,1,0,0};void dfs(vectorvectorint image, int sr, int sc, int new_color){if( sr row || sr 0 || sc col || sc 0||image[sr][sc] ! color||visted[sr][sc] ){return ;}image[sr][sc] new_color;visted[sr][sc] true;for(int i 0; i 4; i){dfs(image,srdx[i],scdy[i],new_color);}}vectorvectorint floodFill(vectorvectorint image, int sr, int sc, int new_color) {color image[sr][sc];row image.size();col image[0].size();dfs(image,sr,sc,new_color);return image;}
};魔鬼细节
dx dy数组用来干嘛的
dx dy可以看作 x方向 和 y 方向的向量srdx[i],scdy[i] 用来合成四个方向。 我们输入 dx 和 dy 数组时只需要对应位置只有一个零1和-1的先后顺序不用管 dx{01-10} dy {-1001} 这个数组合成的涵义是 y 方向先 -1 x方向 1 x方向-1y方向1
visted数组用来干嘛的
为了避免往复走 从逻辑上每个节点只需要走一次即可