Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created July 15, 2012 03:23
Show Gist options
  • Save einblicker/3114762 to your computer and use it in GitHub Desktop.
Save einblicker/3114762 to your computer and use it in GitHub Desktop.
POJ 1979 Red and Black
let blackTile = '.'
let redTile = '#'
let man = '@'
let loopEnd = ref false
while not !loopEnd do
let [|I; J|] = System.Console.ReadLine().Split([|' '|]) |> Array.map int
if I = 0 && J = 0 then
loopEnd := true
else
let maze =
[| for x = 0 to J - 1 do
yield System.Console.ReadLine().ToCharArray() |]
let startI = ref 0
let startJ = ref 0
for i = 0 to I - 1 do
for j = 0 to J - 1 do
if maze.[j].[i] = man then
startI := i
startJ := j
let count = ref 1
let rec dfs i j =
if maze.[j].[i] = blackTile then
maze.[j].[i] <- '#'
incr count
for (dj, di) in [ (-1, 0); (0, -1); (1, 0); (0, 1) ] do
if j+dj >= 0 && i+di >= 0 && j+dj < J && i + di < I && maze.[j+dj].[i+di] = blackTile then
dfs (i+di) (j+dj)
dfs !startI !startJ
printfn "%A" !count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment