結果

問題 No.424 立体迷路
ユーザー ichibanshiboriichibanshibori
提出日時 2016-09-24 00:26:37
言語 F#
(F# 4.0)
結果
AC  
実行時間 1,456 ms / 2,000 ms
コード長 2,328 bytes
コンパイル時間 3,726 ms
コンパイル使用メモリ 168,676 KB
実行使用メモリ 30,272 KB
最終ジャッジ日時 2023-09-18 16:54:38
合計ジャッジ時間 8,879 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 106 ms
23,348 KB
testcase_01 AC 105 ms
23,452 KB
testcase_02 AC 96 ms
25,228 KB
testcase_03 AC 107 ms
23,512 KB
testcase_04 AC 106 ms
23,600 KB
testcase_05 AC 95 ms
25,240 KB
testcase_06 AC 87 ms
23,004 KB
testcase_07 AC 95 ms
25,248 KB
testcase_08 AC 88 ms
20,980 KB
testcase_09 AC 95 ms
23,264 KB
testcase_10 AC 96 ms
21,076 KB
testcase_11 AC 101 ms
23,372 KB
testcase_12 AC 100 ms
23,356 KB
testcase_13 AC 104 ms
23,524 KB
testcase_14 AC 96 ms
23,252 KB
testcase_15 AC 110 ms
23,580 KB
testcase_16 AC 108 ms
25,488 KB
testcase_17 AC 104 ms
23,428 KB
testcase_18 AC 95 ms
23,316 KB
testcase_19 AC 108 ms
23,420 KB
testcase_20 AC 93 ms
23,016 KB
testcase_21 AC 102 ms
23,428 KB
testcase_22 AC 108 ms
23,488 KB
testcase_23 AC 102 ms
23,288 KB
testcase_24 AC 113 ms
25,588 KB
testcase_25 AC 1,456 ms
30,272 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) F# Compiler version 11.0.0.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

ソースコード

diff #

open System

let () =
    let h, w = stdin.ReadLine () |>
               (fun l -> l.Split ()) |>
               (fun arr -> (int arr.[0], int arr.[1]))
    let sx, sy, gx, gy = stdin.ReadLine () |>
                         (fun l -> l.Split ()) |>
                         (fun arr -> (int arr.[0], int arr.[1],
                                      int arr.[2], int arr.[3]))

    let mapArr = Array2D.zeroCreateBased<int> 1 1 w h

    let readB (x, line:string) =
        seq {1..w} |> Seq.iter (fun y -> mapArr.[y, x] <- int line.[y - 1] - int '0')

    seq { for x in 1..h -> (x, stdin.ReadLine ()) } |> Seq.iter readB


    let searchPath startY startX =
        let dxy = [(1, 0); (-1, 0); (0, 1); (0, -1)]
        
        let canMove1 (cy, cx) (dy, dx) =
            let y, x = cy + dy, cx + dx

            y >= 1 && y <= w && x >= 1 && x <= h &&
            abs(mapArr.[y, x] - mapArr.[cy, cx]) <= 1

        let canMove2 (cy, cx) (dy, dx) =
            let y1, y2 = cy + dy, cy + dy * 2
            let x1, x2 = cx + dx, cx + dx * 2

            y2 >= 1 && y2 <= w && x2 >= 1 && x2 <= h &&
            mapArr.[cy, cx] = mapArr.[y2, x2] &&
            mapArr.[cy, cx] > mapArr.[y1, x1]

        let rec searchPath' lst chkedSet =
            match lst with
            | [] -> false
            | pos::rest ->
                let cy, cx = pos
                let cand1 = List.filter (canMove1 (cy, cx)) dxy
                let cand2 = List.filter (canMove2 (cy, cx)) dxy |>
                            List.map (fun (dy, dx) -> (dy * 2, dx * 2))
                let cand = List.append cand1 cand2
                let candLst = List.map (fun (dy, dx) -> (cy + dy, cx + dx)) cand

                if List.contains (gy, gx) candLst then true
                else
                    let nextLst = List.append candLst rest |>
                                  List.filter (fun pos -> not (Set.contains pos chkedSet)) |>
                                  List.sortBy (fun (y, x) -> abs(gy - y) + abs(gx - x))
                    let nextChkedSet = Set.add (cy, cx) chkedSet

                    searchPath' nextLst nextChkedSet
        
        if startY = gy && startX = gx then true
        else searchPath' [(startY, startX)] Set.empty

    searchPath sy sx |> (function | true -> "YES" | false -> "NO") |> printfn "%s"
0