結果

問題 No.13 囲みたい!
ユーザー pocaristpocarist
提出日時 2015-10-06 12:53:03
言語 F#
(F# 4.0)
結果
AC  
実行時間 166 ms / 5,000 ms
コード長 1,647 bytes
コンパイル時間 10,987 ms
コンパイル使用メモリ 185,484 KB
実行使用メモリ 55,396 KB
最終ジャッジ日時 2024-04-21 12:21:49
合計ジャッジ時間 13,952 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
31,872 KB
testcase_01 AC 87 ms
31,744 KB
testcase_02 AC 88 ms
31,744 KB
testcase_03 AC 161 ms
53,888 KB
testcase_04 AC 92 ms
33,024 KB
testcase_05 AC 166 ms
55,396 KB
testcase_06 AC 98 ms
35,072 KB
testcase_07 AC 124 ms
45,040 KB
testcase_08 AC 113 ms
41,572 KB
testcase_09 AC 112 ms
41,472 KB
testcase_10 AC 89 ms
33,252 KB
testcase_11 AC 104 ms
39,168 KB
testcase_12 AC 87 ms
32,384 KB
testcase_13 AC 95 ms
34,688 KB
testcase_14 AC 94 ms
34,816 KB
testcase_15 AC 86 ms
31,872 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (296 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

// http://yukicoder.me/problems/37
open System
open System.Collections.Generic

let dpfn fmt = Printf.kprintf Diagnostics.Debug.WriteLine fmt

let dir = [1,0; 0,-1; -1,0; 0,1]

[<EntryPoint>]
let main argv = 
    let W, H = Console.ReadLine().Trim().Split([|' '|]) |> Array.map int |> fun a -> a.[0], a.[1]
    let M = Array.init H (fun _ -> Console.ReadLine().Trim().Split([|' '|]) |> Array.map int)
    let memo = Array2D.init W H (fun _ _ -> false)
    let rec check_loop c hist queue =
        if Set.count queue = 0 then false else
        let depth, x, y = Set.maxElement queue
        let queue' = Set.remove (depth, x, y) queue
        memo.[x,y] <- true
        if Map.tryFind (x,y) hist <> None then true else
        let hist' = Map.add (x,y) depth hist
        let queue' =
            dir
            |> List.map (fun (dx,dy) -> x+dx,y+dy)
            |> List.filter (fun (x,y) -> x>=0 && y>=0 && x<W && y<H)
            |> List.filter (fun (x,y) -> M.[y].[x] = c &&
                                         (memo.[x,y] = false || 
                                          Map.tryFind (x,y) hist |> function | Some d -> depth+1-d>=4 | None -> false))
            |> List.fold (fun q (x,y) -> Set.add (depth+1,x,y) q) queue'
        check_loop c hist' queue'
    let rec loop x y =
        if y = H then false else
        if x = W then loop 0 (y+1) else
        if memo.[x,y] then loop (x+1) y else
        let ans = check_loop M.[y].[x] Map.empty (Set.singleton (0,x,y))
        if ans then ans else loop (x+1) y
    if loop 0 0 then "possible" else "impossible"
    |> printfn "%s"
    0 // 整数の終了コードを返します
0