Java 我的算法只在2d数组中查找最长路径
我有一个学校作业,我的方法需要找到从给定节点开始的所有可能路径。现在的问题是,我的方法只找到最长的路径,然后停止创建新路径,我似乎不明白为什么要这样做(可能对Java太缺乏经验)。 我使用的是方法需要迭代的Java 我的算法只在2d数组中查找最长路径,java,algorithm,search,path,Java,Algorithm,Search,Path,我有一个学校作业,我的方法需要找到从给定节点开始的所有可能路径。现在的问题是,我的方法只找到最长的路径,然后停止创建新路径,我似乎不明白为什么要这样做(可能对Java太缺乏经验)。 我使用的是方法需要迭代的char[3][3]数组。基本的想法是可行的,但不是所有的路径 我写的方法是: private void computeAllPaths(Point current, ArrayList<Point> currentFullPath) { if (currentFullP
char[3][3]
数组。基本的想法是可行的,但不是所有的路径
我写的方法是:
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);
}
}
}
}
我尝试过许多类型的列表和集合,但似乎没有一个有效,我也不知道为什么,有人能帮我解决这个问题吗
电路板示例:
N|N|AR|E|T
N|T|O 允许的移动: 假设我们从R(1,0)开始,那么允许的移动是:
- N(0,0)
- N(0,1)
- E(1,1)
- T(2,1)
- N(2,0) 所以基本上是直接邻居
currentFullPath
将指向它的指针添加到path
中,并且应该添加指向它的副本的指针,所以猜猜会发生什么。将其添加到路径
后,您将在以后对其进行进一步更改。基本上要调试它,只需打印出内部循环的每一次路径。此外,您还应该为neighturingcoords
中的每个coord
创建currentFullPath
的副本。如果不这样做,您最终会将所有邻居添加到同一路径
请记住,Java总是向对象传递指针
编辑:
我看到仍然有很多胡说八道的东西在四处飞扬。试试这个:
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);
}
}
}
}
private void计算路径(当前点,ArrayList currentFullPath){
if(currentFullPath.isEmpty()){
currentFullPath.add(当前);
}
for(点坐标:neiguringcoords.get(当前)){
如果(!(currentFullPath.contains(coord))){
ArrayList newList=新的ArrayList(currentFullPath);
添加(协调);
if(!(path.contains(newList))){
添加路径(newList);
//以同样的方式重新开始
ComputeAllPath(currentFullPath.get(0),new ArrayList());
}否则{
//尝试添加另一个坐标
计算路径(坐标、新列表);
}
}
}
}
显示结果
编辑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));
}
}
}
private void计算路径(当前点,ArrayList currentFullPath){
if(currentFullPath.isEmpty()){
currentFullPath.add(当前);
}
for(点坐标:neiguringcoords.get(当前)){
如果(!(currentFullPath.contains(coord))){
ArrayList newList=新的ArrayList(currentFullPath);
添加(协调);
添加路径(newList);
计算路径(坐标,新数组列表(新列表));
}
}
}
我不明白你的问题。您能提供一些关于底层数据和允许移动规则的更多信息吗?当前路径大小:10296,在3x3数组中找到39个有效字!非常感谢你!我很接近您的解决方案,但一直在向currentFullPath添加坐标,这并没有带来任何好的结果。虽然它非常适合3x3阵列,但当我在4x4阵列上尝试时,它会出现堆栈溢出。我正在使用哈希集存储路径。我不想尝试计算4x4阵列的路径数量,因为它可能比3x3阵列的路径数量多10^5倍。如果要查找所有有效的单词,请不要存储所有路径!记住你的路线,并限制它的长度。还存储有效的单词。应用新的修复程序后,它显示1012519条从一个坐标开始的不同路径。。。我相信这对我的机器来说是一种过度的杀伤力,当我试图计算所有的路径时?没有1012519从4x4阵列中的一个点开始。我从0,0开始,得到了1012519条不同的路径。以下是我的结果:所需时间:6.279秒当前路径大小:1012519
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)
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);
}
}
}
}
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));
}
}
}