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).
I'm using a char[3][3]
array the method needs to iterate through. 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:
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 | A
R | E | T
N | T | O
Allowed movements: Let's say we start at R (1,0) then allowed moves would be:
- N(0,0)
- N(0,1)
- E(1,1)
- T(2,1)
- N(2,0) So basically it's direct neighboors.