Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
好吧,现在看来你已经有了一个想法,而且它基本上是一个可行的想法。
它只有一个缺陷:您正在查看的邻域是错误的:对于点 (i,j),(冯诺依曼)邻居是 (i+1,j)、(i,j+1)、(i-1) ,j)和(i,j-1)。
因此,当您检查这一点时,它通常应该有效,但是:您必须特别注意边界。由于您没有 -1 列/行,也没有第 n+1 列/行,所以当您在那里时,请不考虑该邻居点。你如何处理它,取决于你想如何对待它:邻域是否应该环绕,边界是否应该具有恒定值,是否应该被视为-infty?
Ok, now it seems you have come with an idea up, and it is basically a working idea.
It has just one flaw: The neigborhood your are looking at is wrong: for a point (i,j) the (von Neumann) neighbors are (i+1,j), (i,j+1), (i-1, j), and (i,j-1).
So when you check for this point it should in general work, BUT: You have to take special care of the borders. As you have no -1st and no n+1th column/row you have when you are there take that neighbor point out of consideration. How you handle it, depends on how you want to treat it: Should the neighborhood wrap around, should the border have a constant value, should it be treated as -infty?
您可以编写
检查该元素是否小于其三个邻居的元素。那么其他五个邻居呢?让我们也检查一下它们:
这假设您想要检查元素的 8 个直接邻居,即直线和对角线。如果这不是您想要的,请进行调整。
还要求找到局部最大值。这确定了局部最小值。将
<
更改为>
。另一件事是
locals.add(i + j)
并没有像你想象的那样做。如果元素 (i=3,j=4) 是局部最大值,那么您所说的是locals.add(7)
,这显然不是您想要的。You write
which checks for whether the element is smaller than three of its neighbours. What about the other five neighbours? Let's check them, too:
This assumes you want to check the element's 8 immediate neighbours, i.e. straight and diagonal. Adjust if this is not what you want.
Also the requirement is finding the local maxima. This identifies a local minimum. Change
<
to>
.Another thing is that
locals.add(i + j)
doesn't do what you think it does. If element (i=3,j=4) is a local maximum, then you're sayinglocals.add(7)
, which clearly is not what you want.MATRIX[n+2][n+2]
:它将包含您的输入矩阵以及一个额外的数组所有 INT_MIN 的层(因此,每个矩阵元素都具有所有 8
邻居)。
Mark[n+2][n+2] = {0}
(您只需访问未标记的输入矩阵)如果矩阵元素是
声明为局部最小值,您应该将其所有邻居标记为
Mark[][] 向量。
MATRIX[n+2][n+2]
: It will contain yout input matrix with an extralayer of all INT_MIN's(So, that every matrix element has all 8
neighbours).
Mark[n+2][n+2] = {0}
(You should only have to visit the elements inthe input matrix which are not marked) If a matrix element is
declared as a local minimum, you should marks all its neighbour in
the Mark[][] vector.
您必须分别存储 i 和 j 才能正确记住元素的位置。因此,
不会起作用。
你可以用它
来代替。要添加新发现的局部最大值的位置,请使用
You'll have to store i and j separately to remember the position of an element correctly. Thus,
won't work.
You could use
instead. And to add the position of a newly found local maximum, use