バックトラッキングでのナイトツアーの問題を解決するには、SML コードを作成する必要があります。チェス ナイトはチェス盤 (サイズ: ) のいたるところを走らなければNxN
ならず、各マスを 1 回だけ訪れなければなりません (最後に最初のマスに戻る必要はありません)。
空のボードを作成する関数、ボード内の正方形を設定または取得する関数、騎士の次の動きのリストを取得する関数はすべて既に作成しています。しかし、再帰関数を SML で記述する方法がわかりません (このアルゴリズムを C で記述する方法は知っていますが、SML では記述できません)。
8x8 チェス盤の C のアルゴリズム
dl and dr are array : (delta to calculate next moves)
dl = [-2,-2, -1, 1, 2, 2, 1, -1]
dr = [-1, 1, 2, 2, 1, -1,-2, -2]
bool backtracking(int** board, int k /*current step*/, int line, int row, int* dl, int* dr) {
bool success = false;
int way = 0;
do {
way++;
int new_line = line + dl[way];
int new_row = row + dr[way];
if (legal_move(board, new_line, new_row)) {
setBoard(board,new_line, new_row,k); //write the current step number k in board
if (k < 64) {
success = backtracking(board, k+1, new_line, new_row, dl, dc);
if (!success) {
setBoard(board,new_line, new_row,0);
}
}
else
success = true;
}
} while(!(success || way = 8));
return success;
}