【发布时间】:2013-05-07 17:28:23
【问题描述】:
我正在尝试解决距离变换问题(使用曼哈顿距离)。基本上,给矩阵 0 和 1,程序必须将每个位置的距离分配到最近的 1。例如,对于这个
0000
0100
0000
0000
距离变换矩阵是
2123
1012
2123
3234
我认为可能的解决方案是:
最慢的(最慢的,因为我试图实现它们 - 它们在非常大的矩阵上滞后):
蛮力 - 对于程序读取的每 1,从开始到结束相应地更改距离。
从 0 开始的广度优先搜索 - 对于每一个 0,程序从内到外查找最近的 1。
与 2 相同,但从 1 的标记开始,每个距离由内向外。
-
更快(从其他人的代码中读取)
从 1 开始的广度优先搜索
1. Assign all values in the distance matrix to -1 or very big value. 2. While reading matrix, put all positions of 1's into queue. 3. While queue is not empty a. Dequeue position - let it be x b. For each position around x (that has distance 1 from it) if position is valid (does not exceed matrix dimensions) then if distance is not initialized or is greater than (distance of x) + 1 then I. distance = (distance of x) + 1 II. enqueue position into queue
我想问是否有更快的解决方案来解决这个问题。我试图搜索距离变换的算法,但它们中的大多数都在处理欧几里得距离。
提前致谢。
【问题讨论】:
-
4) 是广度优先搜索,以
1s 开头