2

我已经写了 NQueen 问题的解决方案。它也可以工作,但在最后一步出现异常。

这是我写的

let isNotDiagonal p c = 
    System.Math.Abs((int)((fst c) - (fst p))) <> System.Math.Abs((int)((snd c) - (snd p)));;

let isAllowed c = function
    | [] -> true
    | list -> List.forall (fun p -> (fst c) <> (fst p) && (snd c) <> (snd p) && (isNotDiagonal p c)) list;;

let comparePoints p1 p2 = 
    match p1, p2 with
    | p1, p2 when (fst p1 > fst p2) -> 1
    | p1, p2 when (fst p1 < fst p2) -> -1
    | p1, p2 when (fst p1 = fst p2) && (snd p1 > snd p2)-> 1
    | p1, p2 when (fst p1 = fst p2) && (snd p1 < snd p2) -> -1
    | p1, p2 when (fst p1 = fst p2) && (snd p1 = snd p2) -> 0
    | _, _ -> failwith "unknown pattern";;

let sort_list list = 
   List.sortWith comparePoints list;;

let print_list list = 
    printfn "%s" (List.fold (fun acc p -> acc + "(" + (fst p).ToString() + ", " + (snd p).ToString() + ")") "" (sort_list list));;

let rec moveQueen board pos list =
    match pos with 
    | (row, col) when row < board && col < board -> 
        if isAllowed pos list then
            moveQueen board (0, col + 1) (pos :: list)
        else
            moveQueen board (row + 1, col) (list)
    | (row, col) when row >= board && col < board ->
        moveQueen board (fst (List.head list) + 1, (snd (List.head list))) (List.tail list) 
    | (row, col) when col >= board ->    
        print_list list   
        moveQueen board (fst (List.head list) + 1, (snd (List.head list))) (List.tail list)
        ()
    | (_, _) -> failwith "unknown combination";;      

moveQueen 8 (0, 0) [];;

我得到以下输出

> (0, 0)(1, 6)(2, 4)(3, 7)(4, 1)(5, 3)(6, 5)(7, 2)
(0, 0)(1, 6)(2, 3)(3, 5)(4, 7)(5, 1)(6, 4)(7, 2)
(0, 0)(1, 5)(2, 7)(3, 2)(4, 6)(5, 3)(6, 1)(7, 4)
(0, 0)(1, 4)(2, 7)(3, 5)(4, 2)(5, 6)(6, 1)(7, 3)
(0, 5)(1, 0)(2, 4)(3, 1)(4, 7)(5, 2)(6, 6)(7, 3)
(0, 3)(1,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, 4)(7, 0)
(0, 4)(1, 1)(2, 3)(3, 6)(4, 2)(5, 7)(6, 5)(7, 0)
(0, 2)(1, 4)(2, 1)(3, 7)(4, 5)(5, 3)(6, 6)(7, 0)
(0, 2)(1, 5)(2, 3)(3, 1)(4, 7)(5, 4)(6, 6)(7, 0)

System.ArgumentException: The input list was empty.
Parameter name: list
   at Microsoft.FSharp.Collections.ListModule.Head[T](FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at FSI_0020.moveQueen(Int32 board, Int32 pos_0, Int32 pos_1, FSharpList`1 list)
   at <StartupCode$FSI_0021>.$FSI_0021.main@()
   at main@dm()
Stopped due to error

我无法理解为什么会出现异常。

另外,如何返回列表列表?每个列表都是一个解决方案?

我在排序和比较方法背后的目标是我应该能够消除重复的解决方案,并且能够找到适合各种板尺寸的独特解决方案。

4

1 回答 1

2

您得到异常是因为您在空列表上使用 List.head,但这不是真正的问题,问题是 moveQueen 没有结束条件。

您必须注意找到所有解决方案后会发生什么,在您的代码中,它将尝试获取空列表的头部并失败。

一种方法(我没有测试那么多)是:

let rec moveQueen board pos list : (int * int) list list =
    match pos with 
    | (row, col) when row < board && col < board -> 
        if isAllowed pos list then
            moveQueen board (0, col + 1) (pos :: list)
        else
            moveQueen board (row + 1, col) (list)
    | (row, _) when row >= board && List.isEmpty list ->  [] 
    | (row, col) when row >= board && col < board ->
        moveQueen board (fst (List.head list) + 1, (snd (List.head list))) (List.tail list) 
    | (_, col) when col >= board ->    
         list::moveQueen board (fst (List.head list) + 1, (snd (List.head list))) (List.tail list)    
    | (row, col) -> failwith ("unknown combination (" + row.ToString() + ", " + col.ToString() + ")");;  

List.iter print_list (moveQueen 8 (0, 0) []);;
于 2013-10-14T08:45:43.323 回答