Advertisement
jasio1906

sadzawka

Jan 16th, 2020
1,133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.35 KB | None | 0 0
  1. let sadzawka arr start =
  2.         let n = Array.length arr in
  3.         let d = Array.make_matrix n n 0 in
  4.         let q = Queue.create () in
  5.         Queue.add start q;
  6.         let (x,y) = start in
  7.         d.(x).(y) <- arr.(x).(y);
  8.  
  9.         let decision (x1,y1) (x2,y2) =
  10.                 let m = min d.(x1).(y1) arr.(x2).(y2) in
  11.                 if(m > d.(x2).(y2)) then (d.(x2).(y2) <- m; true)
  12.                 else false
  13.         in
  14.  
  15.         while (Queue.is_empty q <> true)
  16.         do
  17.                 let (ci,cj) = Queue.take q in
  18.                 if ci>0 then
  19.                         if decision (ci,cj) (ci-1,cj) then (Queue.add (ci-1,cj) q;);
  20.                 if ci<n-1 then
  21.                         if decision (ci,cj) (ci+1,cj) then (Queue.add (ci+1,cj) q;);
  22.                 if cj>0 then
  23.                         if decision (ci,cj) (ci,cj-1) then (Queue.add (ci,cj-1) q;);
  24.                 if cj<n-1 then
  25.                         if decision (ci,cj) (ci,cj+1) then (Queue.add (ci,cj+1) q;);
  26.         done;
  27.  
  28.         let maxi = ref 0 in
  29.         for i = 0 to n-1
  30.         do
  31.                 if d.(0).(i) > !maxi then (maxi:=d.(0).(i););
  32.                 if d.(i).(0) > !maxi then (maxi:=d.(i).(0););
  33.                 if d.(n-1).(i) > !maxi then (maxi:=d.(n-1).(i););
  34.                 if d.(i).(n-1) > !maxi then (maxi:=d.(i).(n-1););
  35.         done;  
  36. !maxi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement