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)
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)(5, 7)(6, 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 の終了条件がないことです。

すべてのソリューションが見つかったときに何が起こるかを処理する必要があります。コードでは、空のリストの先頭を取得しようとして失敗します。

それを行う1つの方法(私はそれほどテストしていません)は次のとおりです。

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 に答える