0

I am trying to write a piece of Java code which can deduce a 1D integer array from a 2D integer array. My 2D array looks something like this:

Node1  Node2   Path
 6      8      501
 2      6      500 
 8      10     505 
 2      4      502

And my task is as follows. I want to find "Path"s from elements of the first column (Node 1) to the elements of the second column (Node2). I mean something like these:

Path from "2" to "8" would be 500 501 in that order. And path from "2" to "4" would be 502 (not 500 since it terminates at "6")

I have been trying to achieve this by iterating with simple for loop but have been struggling. Could someone please let me know how I can solve this?

The code snippet which I am trying to write to achieve is this:

            PathSequence:
            for(int i = 0; i < PathGraphRow; i++){
                if(PathGraph[i][0] == source){
                    if(i==0)
                    {
                        nextNode = PathGraph[i][1];
                        resultantPaths[counter] = PathGraph[i][2];
                        prev = lightPathGraph[i][2];
                        if(nextNode == dest){
                            break PathSequence;
                        }
                        counter++;
                    }else if(i > 0 && prev != PathGraph[i][2])
                    {
                        nextNode = PathGraph[i][1];
                        resultantPaths[counter] = PathGraph[i][2];
                        prev = PathGraph[i][2];
                        if(nextNode == dest){
                            break PathSequence;
                        }
                        counter++;
                    }
                }

                if(nextNode == PathGraph[i][0] && prev != PathGraph[i][2]){
                    nextNode = PathGraph[i][1];
                    resultantPaths[counter] = PathGraph[i][2];
                    prev = PathGraph[i][2];
                    if(nextNode == dest){
                        break PathSequence;
                    }
                    counter++;                    
                }

                if(i == PathGraphRow-1 && PathGraph[i][2] != resultantPaths[counter]){
                    if(PathGraph[i][1] != dest){
                        resultantPaths = new int[PathCount];
                    }
                }

            }        
    }

Thank you.

Regards

4

1 回答 1

0

除非我忽略了一些细节,否则如果没有子路径,问题就相当简单了。您只需要一个非常具体的 for 循环。

 for (int i = 0; i < columns; i++)
 {
      Path path = new Path(array[i][0], array[i][1], array[i][2]);
      pathsArray.Add(path);
 }

那当然是伪代码,但这基本上就是我的做法。我会让每条路径成为一个三元组。我仍然不太明白你打算如何存储数据,如果你把所有的值都放在一个一维 int 数组中,你怎么知道路径的起点和终点?假设所有路径都由可以在 1D 数组中工作的开始、结束和结果定义,尽管使用起来比 2D 数组更糟糕。如果路径是任意长度的,则无法将数据存储在一维数组中。无论如何,这里的代码将提供的表格放在一维数组中(假设没有更大的复杂性);

int[] paths = new int[2dArray.length * 3];
int mapper = 0;

for (int i = 0; i < 2dArray.length; i++)
{
     paths[mapper] = 2dArray[i][0];
     paths[mapper + 1] = 2dArray[i][1];
     paths[mapper + 2] = 2dArray[i][2];
     mapper += 3;
}

然后要访问数组,你需要做一堆 mod 3 废话来知道你得到了哪个值......

 if (index % 3 == 0)
    //path start
 else if (index % 3 == 1)
    //path end
 else if (index % 3 == 2)
    // result of path 
于 2012-12-11T21:36:18.117 回答