我在解決迷宮圖中找到最短路徑的特定問題時遇到了一些麻煩。可能是因爲迷宮由四維數組初始化這一事實而被阻塞,如 adjacent = new boolean[height][width][height][width];
第一對和第二對索引以行/列形式指定圖中的位置。該圖是這樣的:使用鄰接矩陣在迷宮圖中找到最短路徑
XXXXXXXXXXXXX
..........X.X
X.XXX.XXX.X.X
X.X.X...X.X.X
X.X.XXX.XXX.X
X...X.....X..
XXXXXXXXXXXXX
ArrayList中必須持有頂點的位置的路徑,以便從開始到結束的包容性。
我已經寫過構造函數和連接方法;然而,我發現最短路徑方法的麻煩。 下面是如何創建的迷宮圖表的例子:
final int edges[][] = {{1, 0, 1, 1}, {1, 1, 1, 2}, {1, 1, 2, 1},
{1, 2, 1, 3}, {1, 3, 1, 4}, {1, 4, 1, 5}, {1, 5, 1, 6},
{1, 5, 2, 5}, {1, 6, 1, 7}, {1, 7, 1, 8}, {1, 8, 1, 9},
{1, 9, 2, 9}, {1, 11, 2, 11}, {2, 1, 3, 1}, {2, 5, 3, 5},
{2, 9, 3, 9}, {2, 11, 3, 11}, {3, 1, 4, 1}, {3, 3, 4, 3},
{3, 5, 3, 6}, {3, 6, 3, 7}, {3, 7, 4, 7}, {3, 11, 4, 11},
{4, 1, 5, 1}, {4, 3, 5, 3}, {4, 7, 5, 7}, {4, 11, 5, 11},
{5, 1, 5, 2}, {5, 2, 5, 3}, {5, 5, 5, 6}, {5, 6, 5, 7},
{5, 7, 5, 8}, {5, 8, 5, 9}, {5, 11, 5, 12}};
MazeGraph maze = new MazeGraph(13, 7);
for (int[] edge : edges)
maze.connect(new Location(edge[0], edge[1]), new Location(edge[2], edge[3]));
你有沒有聽說過* Dijkstra的算法*? –