Advertisement
PearlyXD

Zadanie 4

Jan 9th, 2021
2,790
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.17 KB | None | 0 0
  1. (* ZADANIE 4 *)
  2. let wysokosc mapa =
  3.     let s1 = Array.length mapa and s2 = Array.length (mapa.(0)) in
  4.     let hmax = let h = ref mapa.(0).(0) in begin
  5.         for i = 0 to s1 - 1 do
  6.             for j = 0 to s2 - 1 do
  7.                 if mapa.(i).(j) > !h then begin
  8.                     h := mapa.(i).(j)
  9.                 end
  10.             done
  11.         done;
  12.         !h
  13.     end in
  14.    
  15.     let warunek h =
  16.         let odw = Array.make_matrix s1 s2 false in
  17.         let rec dfs x y =
  18.             if mapa.(x).(y) > h then () else
  19.             begin
  20.                 odw.(x).(y) <- true;
  21.                 if (x > 0) && not (odw.(x - 1).(y)) then dfs (x - 1) y;
  22.                 if (y > 0) && not (odw.(x).(y - 1)) then dfs x (y - 1);
  23.                 if (x < s1 - 1) && not (odw.(x + 1).(y)) then dfs (x + 1) y;
  24.                 if (y < s2 - 1) && not (odw.(x).(y + 1)) then dfs x (y + 1);
  25.             end
  26.         in dfs 0 0; odw.(s1 - 1).(s2 - 1)
  27.     in
  28.    
  29.     let kon = ref hmax and pocz = ref 0 in
  30.    
  31.     while !kon - !pocz > 1
  32.     do
  33.         let mid = (!kon + !pocz) / 2 in
  34.         if warunek mid
  35.         then
  36.             kon := mid
  37.         else
  38.             pocz := mid;
  39.            
  40.     done; !kon;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement