結果

問題 No.424 立体迷路
ユーザー pekempeypekempey
提出日時 2017-01-27 20:37:28
言語 F#
(F# 4.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,544 bytes
コンパイル時間 4,150 ms
コンパイル使用メモリ 172,496 KB
実行使用メモリ 24,604 KB
最終ジャッジ日時 2023-09-18 17:00:30
合計ジャッジ時間 7,008 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
22,424 KB
testcase_01 AC 66 ms
24,444 KB
testcase_02 AC 65 ms
22,364 KB
testcase_03 AC 66 ms
22,460 KB
testcase_04 AC 65 ms
22,412 KB
testcase_05 AC 66 ms
22,380 KB
testcase_06 AC 65 ms
22,360 KB
testcase_07 AC 66 ms
22,368 KB
testcase_08 AC 67 ms
22,360 KB
testcase_09 AC 67 ms
22,532 KB
testcase_10 AC 67 ms
22,544 KB
testcase_11 AC 66 ms
22,360 KB
testcase_12 AC 65 ms
24,432 KB
testcase_13 AC 67 ms
22,436 KB
testcase_14 AC 66 ms
22,472 KB
testcase_15 AC 66 ms
22,436 KB
testcase_16 AC 67 ms
24,432 KB
testcase_17 AC 72 ms
24,436 KB
testcase_18 AC 69 ms
22,624 KB
testcase_19 AC 67 ms
22,468 KB
testcase_20 AC 68 ms
22,468 KB
testcase_21 AC 64 ms
22,404 KB
testcase_22 AC 65 ms
24,484 KB
testcase_23 AC 64 ms
22,376 KB
testcase_24 AC 67 ms
22,464 KB
testcase_25 AC 68 ms
24,604 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) F# Compiler version 11.0.0.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

ソースコード

diff #

type UnionFind(n) =
    let n = n
    let parent = [| 0 .. n - 1|]

    member this.Parent with get() = parent

    member this.Find x = 
        if this.Parent.[x] <> x then
            this.Parent.[x] <- this.Find this.Parent.[x]
        this.Parent.[x]

    member this.Unite x y =
        this.Parent.[this.Find x] <- this.Find y

    member this.Same x y = (this.Find x) = (this.Find y)

let h, w = System.Console.ReadLine().Split() |> Array.map int |> fun a -> a.[0], a.[1]
let sy, sx, gy, gx = System.Console.ReadLine().Split() |> Array.map int |> Array.map ((+) -1) |> fun a -> a.[0], a.[1], a.[2], a.[3]

let b = [|for i in 0 .. h - 1 -> System.Console.ReadLine().ToCharArray() |> Array.map int|]

let uf = new UnionFind(h * w)

for i = 0 to h - 1 do
    for j = 0 to w - 1 do
        for di, dj in [(0, 1); (1, 0); (0, -1); (-1, 0)] do
            let ii = i + di
            let jj = j + dj
            if 0 <= ii && ii < h && 0 <= jj && jj < w then
                if abs (b.[ii].[jj] - b.[i].[j]) <= 1 then
                    uf.Unite (i * w + j) (ii * w + jj)
        for di, dj in [(0, 1); (1, 0); (0, -1); (-1, 0)] do
            let ii = i + di
            let jj = j + dj
            let iii = i + di * 2
            let jjj = j + dj * 2
            if 0 <= iii && iii < h && 0 <= jjj && jjj < w then
                if b.[ii].[jj] < b.[i].[j] && b.[iii].[jjj] = b.[i].[j] then
                    uf.Unite (i * w + j) (iii * w + jjj)

if uf.Same (sy * w + sx) (gy * w + gx) then "YES" else "NO"
|> System.Console.WriteLine
0