从具有半径的正方形数组中选择算法?

Selection algorithm from an array of square with radius?

作为一种算法,它 select 将数组的元素设置在距支撑元素预定半径处?

例如我 select 项目 28(见图片),半径 = 1 selected :

19 20 21 
27 28 29
35 36 37

如果支撑元素是 1 半径 = 1 selected:

1 2
9 10

算法是先取28项检查上面的元素是否取到(20),检查是否有更高的取到(12),然后20和12检查取对半径内的元素共 2 个,旋转 90 度并重复。

这个算法或者它看起来像什么?

算法可视化: http://i.stack.imgur.com/Cy0Uu.jpg

如果您选择的元素位于 (row, col),那么遍历以 (row, col) 为中心的子矩阵似乎很简单。

我们必须注意避免越界问题。

伪代码:

for rowIndex = max(0, row - radius) to min(matrixHeight - 1, row + radius)
  for colIndex = max(0, col - radius) to min(matrixWidth - 1, col + radius)
    take element at (rowIndex, colIndex)

如果您还想为图像中的元素着色,则可以通过比较位置 (rowIndex, colIndex)(row, col) 轻松完成:例如,如果 rowIndex > rowcolIndex >= colIndex,然后为该元素着色 red