2

I have to write SML code to solve knight's tour problem in backtracking. The chess knight must run all over the chessboard (size: NxN) and must visit each square exactly once (no need to come back in first square at the end).

I already write all the functions to create an empty board, to set or get squares in the board, to get list of possible knight next moves. But I have no idea on how to write the recursive function in SML (I know how to write this algorithm in C but not in SML).

Algorithm in C for a 8x8 chessboard

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;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
Arie
  • 23
  • 5
  • If you know how to write it in C, write/paste the C and we'll help you convert it to SML. – Robin Green Apr 12 '11 at 09:48
  • I've edit my question because of the website restrictions... (I can't answer my question within 24 hours and it was too larger for a comment) – Arie Apr 12 '11 at 11:00
  • well that's the right place to put it - you shouldn't post an update to your question as an answer, and comments can't contain big code blocks. – Robin Green Apr 12 '11 at 11:10
  • Ok. I've just edit the while condition now... I've inversed it. – Arie Apr 12 '11 at 11:18
  • Do you think you'll have time to help me ? – Arie Apr 12 '11 at 21:37

1 Answers1

1

Don't think like C! That's a nasty way of thinking! Working out your algorithm in C/imperative is never helpful. You need to do the homework and learn to think properly.

How will you design the program? It has to store state, and each state has to record where the knight has gone. Design your state to be a tuple of the board (a list of list of bool recording squares traversed) and the moves (a list of (int, int)). So, the function calls will look something like this:

exception Back
fun iter (state, []) = 
      if boardFull state then state else raise Back
  | iter (state, next::ns) =
      iter (next, states next) handle Back => iter (state, ns)

fun states (board, ps) =
    <a move is a pair (int, int); write out the list of eight moves; map a next fn down the list, and filter it by legal moves> (2 or 3 lines of code for you to write)
fun boardFull (board, pos::ps) = <foldl twice to apply the 'and' fn over the whole board> (1 line of code for you to write)
Nicholas Wilson
  • 9,435
  • 1
  • 41
  • 80