Advertisement
sluchaynayakotya

gus 3

Jun 30th, 2020
2,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // inits pascal triangle
  2. const n = 5; // width  (also B's x coordinate)
  3. const m = 3; // height (also B's y coordinate)
  4. const a = Array(n*m).fill(1);
  5. function init_a_recoursively(x, y) {
  6.   if (x>=n||y>=m) return;
  7.   const ox=x, oy=y;
  8.   for (   ; x<n; ++x) a[ x+oy*n] = a[( x-1)+(oy)*n]+a[( x)+(oy-1)*n];
  9.   for (y+1; y<m; ++y) a[ox+ y*n] = a[(ox-1)+( y)*n]+a[(ox)+( y-1)*n];
  10.   init_a_recoursively(ox+1, oy+1);
  11. }
  12. init_a_recoursively(1, 1);
  13.  
  14. // visualizing tho
  15. const aishere='A is here ->';
  16. const maxnumlen=[...a].sort((a,b)=>b-a)[0].toString().length+1;
  17. for (let y=m-1; y>=0; --y) {
  18.   let buf='';
  19.   for (let x=n-1; x>=0; --x)
  20.     buf+=' '.repeat(Math.max(0,maxnumlen-a[x+y*n].toString().length))+a[x+y*n];
  21.   console.log(
  22.     ((y===m-1)?aishere:' '.repeat(aishere.length))+
  23.     ((y===0)?buf.slice(0,-1)+'x <- B is here':buf)
  24.   );
  25. }
  26.  
  27. // result
  28. console.log('Paths num:', a[a.length-1]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement