简体   繁体   English

我的算法只在2D数组中找到最长的路径

[英]My algorithm only finds the longest path in 2d array

I'm having an assignment for school and my method needs to find every possible path starting from a given node. 我正在为学校分配作业,我的方法需要找到从给定节点开始的所有可能路径。 Now the problem is, my method only finds the longests paths and then stops creating new paths and I can't seem to figure out why it is doing this (maybe too inexperienced in Java). 现在的问题是,我的方法只找到最长的路径,然后停止创建新路径,而且我似乎无法弄清楚为什么这样做(在Java中可能太缺乏经验了)。 I'm using a char[3][3] array the method needs to iterate through. 我正在使用该方法需要迭代通过的char[3][3]数组。 The basic idea is working out, but just not all paths. 基本想法正在制定中,但并非所有方法都可行。

The method I wrote: 我写的方法:

private void computeAllPaths(Point current, ArrayList<Point> currentFullPath) {

    if (currentFullPath.isEmpty()) {
        currentFullPath.add(current);
    }

    for (Point coord : neighbouringCoords.get(current)) {
        if (!(currentFullPath.contains(coord))) {
            currentFullPath.add(coord);
            if (!(paths.contains(currentFullPath))) {
                paths.add(currentFullPath);
                //start over again with same coord
                computeAllPaths(currentFullPath.get(0), new ArrayList<Point>()); 
            } else {
                //try to add another coord
                computeAllPaths(coord, currentFullPath); 
            }
        }
    }
}

The method call: 方法调用:

computeAllPaths(new Point(0, 0), new ArrayList<Point>());

The declaration: 声明:

private List<ArrayList<Point>> paths = new LinkedList<ArrayList<Point>>();

Some output for the 3x3 array: 3x3数组的一些输出:

  Current paths size: 8 (0.0,0.0)(1.0,0.0)(0.0,1.0)(0.0,2.0)(1.0,2.0)(2.0,2.0)(2.0,1.0)(2.0,0.0)(1.0,1.0) (0.0,0.0)(1.0,0.0)(2.0,0.0)(2.0,1.0)(2.0,2.0)(1.0,2.0)(0.0,2.0)(0.0,1.0)(1.0,1.0) (0.0,0.0)(1.0,0.0)(2.0,0.0)(2.0,1.0)(1.0,1.0)(2.0,2.0)(1.0,2.0)(0.0,2.0)(0.0,1.0) (0.0,0.0)(1.0,0.0)(2.0,0.0)(2.0,1.0)(2.0,2.0)(1.0,2.0)(0.0,2.0)(0.0,1.0)(1.0,1.0) (0.0,0.0)(1.0,0.0)(2.0,0.0)(2.0,1.0)(2.0,2.0)(1.0,2.0)(1.0,1.0)(0.0,2.0)(0.0,1.0) (0.0,0.0)(1.0,0.0)(2.0,0.0)(2.0,1.0)(2.0,2.0)(1.0,2.0)(0.0,2.0)(0.0,1.0)(1.0,1.0) (0.0,0.0)(1.0,0.0)(2.0,0.0)(2.0,1.0)(2.0,2.0)(1.0,2.0)(0.0,2.0)(0.0,1.0)(1.0,1.0) (0.0,0.0)(1.0,0.0)(2.0,0.0)(2.0,1.0)(2.0,2.0)(1.0,2.0)(0.0,2.0)(0.0,1.0)(1.0,1.0) 

I have tried many types of lists and sets, but noone seems to be working and I have no clue why, can someone help me figure this out? 我已经尝试了多种类型的列表和集合,但是似乎没人在工作,我也不知道为什么,有人可以帮助我解决这个问题吗?

Example of a board: 电路板示例:

N | N | N | N | A 一种
R | R | E | E | T Ť
N | N | T | T | O Ø

Allowed movements: Let's say we start at R (1,0) then allowed moves would be: 允许的移动:假设我们从R(1,0)开始,那么允许的移动将是:

  • N(0,0) N(0,0)
  • N(0,1) N(0,1)
  • E(1,1) E(1,1)
  • T(2,1) T(2,1)
  • N(2,0) So basically it's direct neighboors. N(2,0)因此,它基本上是直接邻居。

So I'm not Java coder myself, but you're doing few things badly. 因此,我自己不是Java编码人员,但是您做得不好。

with currentFullPath you're adding a pointer to it to paths and you should add a pointer to it's copy, so guess what happens. 使用currentFullPath您要添加指向paths的指针,并且应添加指向其副本的指针,所以请猜测会发生什么。 After you add it to paths you are further changing it later. 将其添加到paths后,您以后将对其进行进一步更改。 Basically to debug this, just print out paths every pass of the inside loop. 从根本上调试它,只需在内部循环的每个遍历中打印出paths Also you should create copies of currentFullPath for each coord in neighbouringCoords if you don't do this, you will eventually add all neighbours to the same path. 此外,如果不这样做,则应该为neighbouringCoords每个coord创建currentFullPath副本,否则最终会将所有邻居添加到同一路径。

Remember Java is always passing pointer to the object. 请记住,Java总是将指针传递给该对象。

Edit: I see that there still a lot of nonsense flying around. 编辑:我看到仍然有很多胡说八道。 Try this: 尝试这个:

private void computeAllPaths(Point current, ArrayList<Point> currentFullPath) {

    if (currentFullPath.isEmpty()) {
        currentFullPath.add(current);
    }

    for (Point coord : neighbouringCoords.get(current)) {
        if (!(currentFullPath.contains(coord))) {
            ArrayList<Point> newList = new ArrayList<Point>(currentFullPath);
            newList.add(coord);
            if (!(paths.contains(newList))) {
                paths.add(newList);
                //start over again with same coord
                computeAllPaths(currentFullPath.get(0), new ArrayList<Point>()); 
            } else {
                //try to add another coord
                computeAllPaths(coord, newList); 
            }
        }
    }
}

Show results. 显示结果。

Edit 2: This will be a lot faster. 编辑2:这将更快。

private void computeAllPaths(Point current, ArrayList<Point> currentFullPath) {

    if (currentFullPath.isEmpty()) {
        currentFullPath.add(current);
    }

    for (Point coord : neighbouringCoords.get(current)) {
        if (!(currentFullPath.contains(coord))) {
            ArrayList<Point> newList = new ArrayList<Point>(currentFullPath);
            newList.add(coord);
            paths.add(newList);                           
            computeAllPaths(coord, new ArrayList<Point>(newList));            
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM