Advertisement
xapu

Untitled

May 27th, 2017
902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Tenth(){
  2.     let params=arguments[0]
  3.     let sideX = params[0]
  4.     let sideY = params[1]
  5.     let startX = params[2]
  6.     let startY = params[3]
  7.     let matrix = []
  8.     for(let i=0; i<sideY;i++){              // тук си правя array-те
  9.          matrix[i]=new Array()  
  10.     }
  11.     matrix[startY][startX]=1
  12.     let numbers =[]
  13.     numbers.push([startX,startY])
  14.     while(numbers.length>0){                // проверявам за всяка клетка дали има по-големи от нея в 8-те посоки и ако има такава и задавам нова стойност, и я добавям към numbers, за оискедваща проверка
  15.         let current = numbers.shift()
  16.         let currentX = current[0]
  17.         let currentY = current[1]
  18.         // right
  19.         if(currentX<sideX-1&&currentX>=0&&currentY>=0&&currentY<sideY){
  20.             if(matrix[currentY][currentX]<matrix[currentY][currentX+1]||matrix[currentY][currentX+1]==undefined){
  21.                 matrix[currentY][currentX+1]=matrix[currentY][currentX]+1
  22.                 numbers.push([currentX+1,currentY])
  23.             }
  24.         }
  25.         // down
  26.         if(currentY<sideY-1&&currentY>=0&&currentX>=0&&currentX<=sideX){
  27.             if(matrix[currentY][currentX]<matrix[currentY+1][currentX]||matrix[currentY+1][currentX]==undefined){
  28.                 matrix[currentY+1][currentX]=matrix[currentY][currentX]+1
  29.                 numbers.push([currentX,currentY+1])
  30.             }
  31.         }
  32.         // left
  33.         if(currentX>0&&currentX<sideX&&currentY>=0&&currentY<sideY){
  34.             if(matrix[currentY][currentX]<matrix[currentY][currentX-1]||matrix[currentY][currentX-1]==undefined){
  35.                 matrix[currentY][currentX-1]=matrix[currentY][currentX]+1
  36.                 numbers.push([currentX-1,currentY])
  37.             }
  38.         }
  39.         // up
  40.         if(currentY<sideY&&currentY>0&&currentX>=0&&currentX<=sideX){
  41.             if(matrix[currentY][currentX]<matrix[currentY-1][currentX]||matrix[currentY-1][currentX]==undefined){
  42.                 matrix[currentY-1][currentX]=matrix[currentY][currentX]+1
  43.                 numbers.push([currentX,currentY-1])
  44.             }
  45.         }
  46.  
  47.         // diagonal up right
  48.         if(currentX<sideX-1&&currentX>=0&&currentY>0&currentY<sideY){
  49.             if(matrix[currentY][currentX]<matrix[currentY-1][currentX+1]||matrix[currentY-1][currentX+1]==undefined){
  50.                 matrix[currentY-1][currentX+1]=matrix[currentY][currentX]+1
  51.                 numbers.push([currentX+1,currentY-1])
  52.             }
  53.         }
  54.         //diagonal up left
  55.         if(currentX>0&&currentX<sideX&&currentY>0&&currentY<sideY){
  56.             if(matrix[currentY][currentX]<matrix[currentY-1][currentX-1]||matrix[currentY-1][currentX-1]==undefined){
  57.                 matrix[currentY-1][currentX-1]=matrix[currentY][currentX]+1
  58.                 numbers.push([currentX-1,currentY-1])
  59.             }
  60.         }
  61.         //diagonal down left
  62.         if(currentX>0&&currentX<sideX&&currentY>=0&&currentY<sideY-1){
  63.             if(matrix[currentY][currentX]<matrix[currentY+1][currentX-1]||matrix[currentY+1][currentX-1]==undefined){
  64.                 matrix[currentY+1][currentX-1]=matrix[currentY][currentX]+1
  65.                 numbers.push([currentX-1,currentY+1])
  66.             }
  67.         }
  68.         // diagonal down right
  69.         if(currentX<sideX-1&&currentX>=0&&currentY>=0&currentY<sideY-1){
  70.             if(matrix[currentY][currentX]<matrix[currentY+1][currentX+1]||matrix[currentY+1][currentX+1]==undefined){
  71.                 matrix[currentY+1][currentX+1]=matrix[currentY][currentX]+1
  72.                 numbers.push([currentX+1,currentY+1])
  73.             }
  74.         }  
  75.     }
  76.     matrix.map(x=>console.log(x.join(' ')))
  77. }
  78.  
  79. Tenth([3, 3, 2, 2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement