미로의 경로를 찾으려고합니다. 코드는 다음과 같습니다. recursiveSolve 루프 안에 들어가는 것이 좋겠지 만, 두 번째 조건이 끝나면 나간다. 여기서 내가 잘못하고있는 것은 누군가 나를 도와 줄 수있다. 부디? im 설정 Washere 및 correctpath 배열은 기본적으로 false입니다.미로 알고리즘 경로 파인더
recursiveSolve(0, 0);
public static int[,] maze = {{0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 0},
{0, 1, 1, 0, 0, 1},
{0, 1, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 1}};
public static Boolean recursiveSolve(int row, int col) {
Boolean[,] wasHere = new Boolean[6, 6];
Boolean[,] correctPath = new Boolean[6, 6]; // The solution to the maze
if (maze[row, col] == 1 || wasHere[row, col]) {
return false;
}
else if (row == 0 || row == 6 - 1 || col == 0 || col ==6 - 1) {
correctPath[row, col] = true;
return true;
}
else {
wasHere[row, col] = true;
if (recursiveSolve(row - 1, col) || recursiveSolve(row + 1, col) ||
recursiveSolve(row, col - 1) ||
recursiveSolve(row, col +1)) {
correctPath[row, col] = true;
return true; // successfully escaped; this square is on path
}
else {
return false;
}
}
}
디버깅을 시도 했습니까? 두 번째 if 조건이 true로 표시되어 함수가 종료됩니다. –
'else if' :'else if (행 == 0 || 행 == 6 - 1 || col == 0 || col == 6 - 1)'의 조건을주의하십시오. recursiveSolve (0,0)''row == 0'과'col == 0' 조건은 모두 true이고 함수를 종료합니다. 미로의 가장자리에서 시작하지 않으셨습니까? –
@MattBurland 그저 시작 지점 일 수 있습니다. 시작 지점의 매개 변수는 무엇이 될까요? – Milas