나이트 투어가 이전에 요청되었지만 여전히 문제가 있습니다. 체스 판의 모든 셀을 방문하는 재귀를 시도하고 있지만 52 회 이상 방문을 할 수는 없습니다. 그 후에 그것은 역 추적하고 방문한 세포의 수는 아래로 센다.나이트 투어 재귀가 해결책을 찾지 못했습니다
public class Ch7E22_3 {
public static int[][] chess;
public static int[][] adjacent;
public static void main(String[] args) {
chess = new int[8][8];
adjacent = new int[][] { { 2, 1 }, { 2, -1 }, { -2, 1 }, { -2, -1 }, { 1, 2 }, { -1, 2 }, { 1, -2 },
{ -1, -2 } };
initializeChess();
move(1, 0, 0);
}
private static void move(int cnt, int row, int col) {
chess[row][col] = cnt;
if (cnt == (8 * 8)) {
System.out.println("You moved around all cells: " + cnt);
} else {
for (int i = 0; i < 8; i++) {
if (((row + adjacent[i][0] >= 0) && (row + adjacent[i][0]) < 8)
&& ((col + adjacent[i][1] >= 0) && (col + adjacent[i][1] < 8))) {
if (chess[row + adjacent[i][0]][col + adjacent[i][1]] == 0) {
row = row + adjacent[i][0];
col = col + adjacent[i][1];
cnt++;
System.out.println(row + " " + col + " cnt = " + cnt);
move(cnt, row, col);
}
}
}
}
chess[row][col] = 0;
}
private static void initializeChess() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
chess[i][j] = 0;
}
}
}
}
초기화되지 않은 int 배열은 이미 0으로 채워져 있습니까? 따라서 main 메소드의 initializeChess()는 불필요합니다. – FatTony
@FatTony 맞아요. 이미 사용 된 배열을 지우고 다시 시작할 필요가있을뿐입니다.이 프로그램은하지 않습니다. –