そのため、キュー、セット、ロケーション オブジェクト、および最終的に Maze オブジェクトになる Cell オブジェクトを使用して、迷路ソルバーを作成する必要があります。
完成したときに本質的にすべてのコードが何をするかを簡単に見てみましょう。
7
10
_ _ _ _ _ _ _ _ _
|_ _ _ | _ _ _ |
| _ _| | | _ | |
| | | |_| | | |_| |
|_ _|_ _ _| |_ | |
| _ | | _ _| |_|
| |_ _| _| |_ |
|_ _ _ _|_ _|_ _ _| |
これに:
@ _ _ _ _ _ _ _ _ _
|@ @ @ @| _ _ _ |
| _ _|@| |@ @ @| |
| | |@|_|@| |@|_| |
|_ _|_ @ @ @| |@ @| |
| _ | | _ _|@|_|
| |_ _| _| |_ @ @|
|_ _ _ _|_ _|_ _ _|@|
@
これまでのところ、私が行ったことはすべて非常にうまくいっていますが、Maze オブジェクトで findPath() メソッドを実際にコーディングすると、私のコードは無効なパスを生成します。ファイルを取り込んで迷路を読み取ると、その迷路を多次元文字配列に変換し、その文字配列をセルの多次元配列に変換し、各セルの境界を北、南、東、西にマップしますブール値。
さて、実際に迷路をナビゲートする方法を理解することになります.MazeのfindPath()メソッドで試みましたが、実際には失敗しました.
@ @ @ @ . . . . . .
. . . @ . . . . . .
. @ @ @ . . . . . .
. @ @ @ @ . . . . .
. . @ @ @ . . . . .
. @ @ @ @ . . . . .
. . . . . . . . . .
まず、私が達成すべきことを説明するために、私の要件ドキュメントを見てみましょう。
The algorithm operates according to the following pseudo-code:
* Visit the starting Location.
* Add this Location to the set.
* Enqueue the Location in the queue.
while (ArrayQueue<E> != empty( ))
{
Dequeue a Location(next) from the queue
For each neighbor of Location(next) which has
not yet been placed in the set, repeat:
* Visit the neighbor of Location(next).
* Add the neighbor of Location(next) to the Set.
* Enqueue the neighbor of Location(next)in the Queue.
}
彼のアルゴリズムをある程度正しく使用したことはほぼ間違いありませんが、遭遇したパスを取得するために何が間違っていたのかわかりません。私の最大の頭痛の種は、以下に示した Maze オブジェクトの findPath() メソッドにあります。私の最大の質問は、何が間違っているのでしょうか? 私は何日もこれに取り組んできましたが、これを理解することはできません. どんな助けでも大歓迎です。私のコードは以下の通りです:
My Maze の findpath メソッド
public void findPath()
{
Location startLocation = new Location(0, 0);
theMaze[startLocation.getRow()][startLocation.getColumn()].setVisited(true);
Location endLocation = new Location(6, 9);
Location cursor;
locationQueue.enqueue(startLocation);
locationSet.enter(startLocation);
while(!locationQueue.isEmpty())
{
cursor = locationQueue.dequeue();
if(cursor == endLocation)
break;
for(int i = 0; i < 4; i++)
{
Location temp = cursor.getLoc(i);
if(theMaze[cursor.getRow()][cursor.getColumn()].validDirection(i) && (!locationSet.isElement(temp)) && !(theMaze[temp.getRow()][temp.getColumn()].isVisited()))
{
cursor = cursor.getLoc(i);
theMaze[cursor.getRow()][cursor.getColumn()].setVisited(true);
if(theMaze[cursor.getColumn()][cursor.getColumn()].getPathAmount() < 2)
{
cursor = startLocation;
continue;
}
locationSet.enter(cursor);
locationQueue.enqueue(cursor);
}
}
}
for(int i = 0; i < locationSet.size(); i++)
{
System.out.println("Row " + locationSet.get(i).getRow() + " Column " + locationSet.get(i).getColumn());
theMaze[locationSet.get(i).getRow()][locationSet.get(i).getColumn()].setPath();
}
for(int i = 0; i < theMaze.length; i++)
{
for(int j = 0; j < theMaze[i].length; j++)
{
System.out.print(theMaze[i][j].toString());
}
System.out.print("\n");
}
}
編集:私の問題は、他のクラスではなく Maze オブジェクトにあるため、基本的にクリーンアップしています。