在二维数组中找到所有最短路径

我试图弄清楚如何在我的数据结构中找到所有最短的路径,但是到目前为止,使用递归方法尝试失败。输入示例如下:(s =起始单元格,e =结束单元格)。我想找到从's'到'e'的所有短裤方式,并将它们存储在ArrayList中。

. . . . .
. s . . .
. . . . .
. . . e .
. . . . . 
. . . . .

Cell类如下所示,并表示上述矩阵中的一个单元:

public class Cell {
    private int line;
    private int column;
    private char character;
    ...
    public Cell(int line,int column) {
        this.line = line;
        this.column = column;
    }

}

我还有一个字段类,其中包含一个包含所有节点的ArrayList以及代表该字段的列和行的总数(在上述情况下,columns = 5,lines = 6和List包含所有30个单元格)。

public class Field {
    private List<Cell> cells;
    private int lines;
    private int columns;
    ...
    }

到目前为止,我尝试过的是:

// starti= line-index of cell s,starj= column-index of cell s
// should create an ArrayList<Cell> current which contains one shortest path,should add current to the 
// ArrayList<ArrayList<Cell>> path,which contains all shortest paths.
private void shortpath (int starti,int startj,int endi,int endj) {

        if (endi == starti && endj == startj) {
            current = new ArrayList<Cell>();
            current.add(new Cell(starti,startj));
            path1.add(current);
            return;
        }

        if (starti < endi) {
            shortpath (starti+1,startj,endi,endj);
            current.add(new Cell(starti,startj));
        }
        if (startj < endj) {    
            shortpath (starti,startj+1,startj));
        }
}

我真的不知道如何做得更好。 提前致谢, 帕特里克

shuangzai520 回答:在二维数组中找到所有最短路径

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3046172.html

大家都在问