結果

問題 No.13 囲みたい!
ユーザー pocarist
提出日時 2015-10-06 12:53:03
言語 F#
(F# 4.0)
結果
AC  
実行時間 160 ms / 5,000 ms
コード長 1,647 bytes
コンパイル時間 10,464 ms
コンパイル使用メモリ 186,244 KB
実行使用メモリ 55,424 KB
最終ジャッジ日時 2024-10-13 10:47:38
合計ジャッジ時間 12,920 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (288 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