Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created July 15, 2012 08:55
Show Gist options
  • Save einblicker/3115976 to your computer and use it in GitHub Desktop.
Save einblicker/3115976 to your computer and use it in GitHub Desktop.
POJ 3669 Meteor Shower
module Util =
let fordir x y f =
for (dx, dy) in [ (-1, 0); (0, -1); (1, 0); (0, 1) ] do
f (x+dx) (y+dy)
open Util
let size = System.Console.ReadLine() |> int
let inputs = [|
for x = 0 to size - 1 do
yield System.Console.ReadLine().Split([|' '|]) |> Array.map int
|]
let cost = Array2D.init 330 330 (fun _ _ -> System.Int32.MaxValue)
for [| x; y; t |] in inputs do
cost.[x, y] <- min cost.[x, y] t
fordir x y <| fun nx ny ->
if nx >= 0 && ny >= 0 then
cost.[nx, ny] <- min cost.[nx, ny] t
let queue = System.Collections.Generic.Queue<int*int*int>()
queue.Enqueue(0, 0, 0)
let visited = Array2D.init 330 330 (fun _ _ -> false)
exception Done
try
while queue.Count <> 0 do
let (c, x, y) = queue.Dequeue()
if not visited.[x, y] then
visited.[x, y] <- true
if cost.[x, y] = System.Int32.MaxValue then
printfn "%A" c
raise Done
fordir x y <| fun nx ny ->
if nx >= 0 && ny >= 0 && not visited.[nx, ny] && cost.[nx, ny] > c+ 1 then
queue.Enqueue(c+1, nx, ny)
printfn "%A" -1
with Done -> ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment