ocaml: phase 5.1 unique_paths_obs.ml baseline (4x4 grid w/ obstacles = 3)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 25s
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.
This commit is contained in:
@@ -196,6 +196,7 @@
|
||||
"triangle_div.ml": 120,
|
||||
"twosum.ml": 5,
|
||||
"union_find.ml": 4,
|
||||
"unique_paths_obs.ml": 3,
|
||||
"unique_set.ml": 9,
|
||||
"validate.ml": 417,
|
||||
"word_count.ml": 3
|
||||
|
||||
28
lib/ocaml/baseline/unique_paths_obs.ml
Normal file
28
lib/ocaml/baseline/unique_paths_obs.ml
Normal file
@@ -0,0 +1,28 @@
|
||||
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 ()
|
||||
Reference in New Issue
Block a user