私のコードの目的は、迷路を読み取り、それを 2D 配列に格納して解決するプログラムを作成することです。迷路を読み取って配列に入れるプログラムを取得しましたが、再帰アルゴリズムが機能していません。これを機能させるために変更したのは 3 回目です。それで、これを機能させるのを手伝ってもらえますか?
編集:迷路を解くために元のアルゴリズムの問題を見つけましたが、別の問題に遭遇しました。プログラムは正しいものを出力していません。また、これは私の好奇心のためだけです。私はすでにそれを別の方法で機能させる方法を見つけました。
迷路コード:
public static boolean goNorth(){
boolean success;
if (maze[currCol][currRow - 1] == maze[finishCol][finishRow]) {
success = true;
}
if(maze[currCol][currRow - 1] == CLEAR){
maze[currCol][currRow - 1] = PATH;
currRow = currRow - 1;
success = goNorth();
if(!success){
success = goWest();
if(!success){
success = goEast();
if(!success){
maze[currCol][currRow] = VISITED;
currRow = currRow + 1;
}
}
}
return success;
} else {
return false;
}
}
public static boolean goWest(){
boolean success;
if (maze[currCol - 1][currRow] == maze[finishCol][finishRow]) {
success = true;
}
if(maze[currCol - 1][currRow] == CLEAR){
maze[currCol - 1][currRow] = PATH;
currCol = currCol - 1;
success = goWest();
if(!success){
success = goSouth();
if(!success){
success = goNorth();
if(!success){
maze[currCol][currRow] = VISITED;
currCol = currCol + 1;
}
}
}
return success;
} else {
return false;
}
}
public static boolean goEast(){
boolean success;
if (maze[currCol + 1][currRow] == maze[finishCol][finishRow]) {
success = true;
}
if(maze[currCol + 1][currRow] == CLEAR){
maze[currCol + 1][currRow] = PATH;
currCol = currCol + 1;
success = goEast();
if(!success){
success = goNorth();
if(!success){
success = goSouth();
if(!success){
maze[currCol][currRow] = VISITED;
currCol = currCol - 1;
}
}
}
return success;
} else {
return false;
}
}
public static boolean goSouth(){
boolean success;
if (maze[currCol][currRow + 1] == maze[finishCol][finishRow]){
success = true;
}
if(maze[currCol][currRow + 1] == CLEAR){
maze[currCol][currRow + 1] = PATH;
currRow = currRow + 1;
success = goSouth();
if(!success){
success = goEast();
if(!success){
success = goWest();
if(!success){
maze[currCol][currRow] = VISITED;
currRow = currRow - 1;
}
}
}
return success;
} else {
return false;
}
}
}
迷路解法コード:
public class SolveMaze1
{
public static void main (String[] args)
{
Maze1 maze = new Maze1();
maze.readMaze();
maze.printMaze();
maze.goNorth();
maze.printMaze();
}
}
望ましい結果:
xxxxxxxxxxxxxxxxxxFx
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxxSxxxxxxx
xxxxxxxxxxxxxxxxxxFx
xVVVVVxPPPPPPPxxxxPx
xVxxxxxPxxxxxPPPxxPx
xVxxxxxPxxxxxxxPxxPx
xVVVVVVPPPPPPxxPxxPx
xVxxxxxxxxxxPxxPPPPx
xxxxxxxxxxxxSxxxxxxx
私の結果:
xxxxxxxxxxxxxxxxxxFx
xVVVVVxVVVVVVVxxxxVx
xVxxxxxVxxxxxVVVxxVx
xVxxxxxVxxxxxxxVxxVx
xVVVVVVVVVVVVxxVxxVx
xVxxxxxxxxxxVxxVVVVx
xxxxxxxxxxxxSxxxxxxx