Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
Standard 2D unique-paths DP with obstacles gating each cell:
dp[i][j] = if grid[i][j] = 1 then 0
else dp[i-1][j] + dp[i][j-1]
Grid (1s are obstacles):
. . . .
. # . .
. . . #
# . . .
dp:
1 1 1 1
1 0 1 2
1 1 2 0
0 1 3 3
Returns dp[3][3] = 3.
Complements grid_paths.ml (no-obstacles version) — same DP shape
but obstacles zero out cells and reshape the path count.
199 baseline programs total.
29 lines
613 B
OCaml
29 lines
613 B
OCaml
let h = 4
|
|
let w = 4
|
|
let grid = [|
|
|
[| 0; 0; 0; 0 |];
|
|
[| 0; 1; 0; 0 |];
|
|
[| 0; 0; 0; 1 |];
|
|
[| 1; 0; 0; 0 |]
|
|
|]
|
|
|
|
let paths_with_obs () =
|
|
let dp = Array.init h (fun _ -> Array.make w 0) in
|
|
if grid.(0).(0) = 0 then dp.(0).(0) <- 1;
|
|
for j = 1 to w - 1 do
|
|
if grid.(0).(j) = 0 then dp.(0).(j) <- dp.(0).(j - 1)
|
|
done;
|
|
for i = 1 to h - 1 do
|
|
if grid.(i).(0) = 0 then dp.(i).(0) <- dp.(i - 1).(0)
|
|
done;
|
|
for i = 1 to h - 1 do
|
|
for j = 1 to w - 1 do
|
|
if grid.(i).(j) = 0 then
|
|
dp.(i).(j) <- dp.(i - 1).(j) + dp.(i).(j - 1)
|
|
done
|
|
done;
|
|
dp.(h - 1).(w - 1)
|
|
;;
|
|
|
|
paths_with_obs ()
|