Created
July 15, 2012 08:12
-
-
Save einblicker/3115842 to your computer and use it in GitHub Desktop.
POJ 3009 Curling 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exception Done of int | |
let loopEnd = ref false | |
while not !loopEnd do | |
let [| width; height |] = System.Console.ReadLine().Split([|' '|]) |> Array.map int | |
if height = 0 && width = 0 then loopEnd := true | |
else | |
let maze = | |
[| for x = 0 to height - 1 do | |
yield System.Console.ReadLine().Split([|' '|]) |> Array.map int |] | |
let start = ref (0, 0) | |
let goal = ref (0, 0) | |
for h = 0 to height - 1 do | |
for w = 0 to width - 1 do | |
if maze.[h].[w] = 2 then | |
maze.[h].[w] <- 0 | |
start := (h, w) | |
else if maze.[h].[w] = 3 then | |
maze.[h].[w] <- 0 | |
goal := (h, w) | |
let rec dfs h w c = | |
if c >= 10 then 11 | |
else | |
try | |
let answer = ref 11 | |
for (dh, dw) in [ (-1, 0); (0, -1); (1, 0); (0, 1) ] do | |
if h+dh >= 0 && w+dw >= 0 && h+dh < height && w+dw < width && maze.[h+dh].[w+dw] <> 1 then | |
let i = ref 1 | |
let flag = ref true | |
while !flag && h+dh* !i >= 0 && w+dw* !i >= 0 && h+dh* !i < height && w+dw* !i < width do | |
if (h+dh* !i, w+dw* !i) = !goal then | |
raise (Done (c+1)) | |
else if maze.[h+dh* !i].[w+dw* !i] = 0 then | |
incr i | |
else if maze.[h+dh* !i].[w+dw* !i] = 1 then | |
flag := false | |
if not !flag then | |
maze.[h+dh* !i].[w+dw* !i] <- maze.[h+dh* !i].[w+dw* !i] - 1 | |
answer := min (dfs (h + dh * (!i - 1)) (w + dw * (!i - 1)) (c+1)) !answer | |
maze.[h+dh* !i].[w+dw* !i] <- maze.[h+dh* !i].[w+dw* !i] + 1 | |
!answer | |
with Done(x) -> x | |
let answer = dfs (fst !start) (snd !start) 0 | |
printfn "%A" (if answer=11 then -1 else answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment