我有一个数组(nodes[][]
)包含看起来像这样的有效距离值:更好的方法来搜索数组?
__ __
|1 0.4 3 |
|0.4 1 0 |
|3 3.2 1 ... |
|0.8 4 5 |
|0 0 1 |
-- --
其中第一个值,node[0][0]
是从节点0到节点0,是1
所以距离从节点2到节点1的距离为3.2(node[2][1]=3.2
)
我需要,给定一个节点列,通过行搜索,找到最遥远的距离,而不是选择本身(node[1][1]
)
的方法,我在想这样做像这样:
int n=0;
currentnode=0; //this is the column I am searching now
if(currentnode==n)
n++;
best=node[n][currentnode];
nextbest=node[n++][currentnode];
if(nextbest>best)
best=nextbest;
else
for(int x=n;x<max;x++) //max is the last column
{
if(currentnode==n)
continue;
nextbest=node[x][currentnode];
if(nextbest>best)
best=nextbest;
}
我想不出一个更好的方法来做到这一点。我可以使用函数来缩短它的长度,但这正是我想要使用的。在此之后,我必须将其循环到下一个最佳距离返回的列,并再次执行此例程。
是今天到期的作业吗? – catfood 2009-10-13 20:01:21
这是一个我正在使用贪婪的地理数据包转发方法寻找Ad-Hoc无线网络的有效距离的小型项目。所以不行?当然,这是我正在做的一个非常简化的版本。 – 2009-10-13 20:05:31
如果节点距离自身的距离是1,矩阵中怎么会有0?这是否是某种时空操纵? :) – Zed 2009-10-13 20:23:24