그래서 재귀 자바 함수에서 미로를 풀어 달라는 요청을 받았지만 재귀 함수가 나던 올바른 경로를 '*'로 바꾸는 문제가 발견됩니다.자바 미로 해결 문제
도움을 주시면 감사하겠습니다.
public class Maze
{
/**
* This is only an example,
* you can change this to test other cases but don't forget to submit the work with this main.
* @param args
*/
public static void main(String[] args)
{
int M = 4;
int N = 4;
char[][] maze = {{'1','0','0','0'},{'1','1','0','0'},{'0','1','1','1'},{'0','0','0','1'}};
if (findPath(maze, 0,0))
printMaze(maze);
else
System.out.println("No solution");
}
private static void printMaze(char[][] maze)
{
for (int i = 0; i < maze.length; i++)
{
for (int j = 0; j < maze[0].length; j++)
{
System.out.print(maze[i][j] +" ");
}
System.out.println();
}
}
// you should implement this function
private static boolean findPath(char[][] maze, int i, int j)
{
if ((i+1 > maze.length) || (j+1 > maze[i].length))
return false;
else
{
if (maze[i][j] == 1)
{
maze[i][j] = '*';
if (maze[i+1][j] == 1)
{
return findPath(maze, i+1, j);
}
if (maze[i][j+1] == 1)
{
return findPath(maze, i, j+1);
}
}
}
return true;
}
}
무슨 일, 당신은 단지 바로 아래로 이동하여 경로를 찾기 위해 노력하고 왼쪽은 어떻습니까? – Dejan
나는 방향 코드를 끝내지 못한다. 내 문제는 '1'을 '*'로 바꾼다는 것이 아니라, 여기에 대한 답을 쉽게 찾을 수 있고, 나 혼자서 이해하고, 다른 방향을 찾는 것이 간단하다는 것이다. –