探寻路径

// Learn more about F# at http://fsharp.net

// See the 'F# Tutorial' project for more help.

module File1

open System.Windows.Forms

open System.Collections.Generic



type Loc =

    |A|B|C|D



let path_key a b = if a<b then (a,b) else (b,a)

let path_key_ls (ls:Loc list) = (ls.Head,A)



let pathData =[A,B,4;A,C,1;B,C,1;B,D,1;C,D,4]

let path_Dis_map =  pathData

                    |>List.map (fun (a,b,c)->((path_key a b)),c)

                    |>Map.ofList

path_Dis_map |> Map.toList |>printfn "%A" 



//let dict = new Dictionary<string, string>()

let path_Dis_map2 = new Dictionary< Loc*Loc , int>()



//Map.tryFind

                     

let path_map_query ls map1=

    match ls with

    |head::val1::tail->

        if ( Map.containsKey (A,head) map1) then

            map1.[(A,head)]

        else

            System.Int32.MaxValue

    |_->System.Int32.MaxValue



let rec path_cdis ls=

    match ls with

    |head::a::tail->

        let dis1 = path_Dis_map.[path_key head a]

        dis1 + path_cdis(a::tail)

    |_->0





printfn "dis = %d %d" (path_cdis [D;C;B;A]) (path_map_query [D;C;B;A] path_Dis_map)



let fileterme crr value=



        match value with

        | (a,b,c) when a=crr -> Some(b)

        | (a,b,c) when b=crr ->Some(a)

        | _ ->None

let findway (ls:Loc list) =

    List.choose (fileterme ls.Head) pathData

    |> List.filter (fun x-> not ( List.exists (fun i->i=x) ls))







let start1 =[[A]]







let rec work2 (start,pathmaps)=

    let once =

        List.collect (fun (ls)->

        let firstList = ls//List.head start

        let ways = findway firstList

        match ways with

        |[] -> []

        |_->

            let ends = List.map (fun x ->x::ls) ways

            ends

        ) start

    let map1 = pathmaps

    let Mapme = once|> List.fold (fun (acc:Map<(Loc*Loc),int>) (ls:Loc list)->

                let k=A,ls.Head

                let v= path_cdis ls

                match (Map.tryFind k acc) with

                    |Some n-> 

                        if v< n then

                            Map.add k v acc

                        else

                            acc

                    |_->Map.add k v acc

                ) map1  //

    //Mapme|>printf "Map me %A"

        





    List.filter (fun x-> match x with

                |D::tail ->

                    printfn "Find the way %A = %d" x (path_cdis x)

                    false

                |_->true

                ) once |>printfn "%A"

    //裁剪路径

    let once3 = List.filter (fun (ls:Loc list)->

                let k=A,ls.Head

                let v= path_cdis ls

                match (Map.tryFind k Mapme) with

                    |Some n-> 

                        if v <= n then

                            true

                        else

                            false

                    |_->true

                ) once

                |>  List.filter (fun (x:Loc list)-> not (D = x.Head)) 

    //once3 |> printfn ">>>>Left is %A"

    once3,Mapme



let rec work1 (start,map) =

    match start with

    |[] ->()

    |_->

        let data = work2 (start,map)

        work1 data



work1 (start1,path_Dis_map) |> printfn ">>>%A"



[<EntryPoint>]

let main argv =

    printfn "%A" argv

    0 // return an integer exit code

  

你可能感兴趣的:(路径)