Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function Tenth(){
- let params=arguments[0]
- let sideX = params[0]
- let sideY = params[1]
- let startX = params[2]
- let startY = params[3]
- let matrix = []
- for(let i=0; i<sideY;i++){ // тук си правя array-те
- matrix[i]=new Array()
- }
- matrix[startY][startX]=1
- let numbers =[]
- numbers.push([startX,startY])
- while(numbers.length>0){ // проверявам за всяка клетка дали има по-големи от нея в 8-те посоки и ако има такава и задавам нова стойност, и я добавям към numbers, за оискедваща проверка
- let current = numbers.shift()
- let currentX = current[0]
- let currentY = current[1]
- // right
- if(currentX<sideX-1&¤tX>=0&¤tY>=0&¤tY<sideY){
- if(matrix[currentY][currentX]<matrix[currentY][currentX+1]||matrix[currentY][currentX+1]==undefined){
- matrix[currentY][currentX+1]=matrix[currentY][currentX]+1
- numbers.push([currentX+1,currentY])
- }
- }
- // down
- if(currentY<sideY-1&¤tY>=0&¤tX>=0&¤tX<=sideX){
- if(matrix[currentY][currentX]<matrix[currentY+1][currentX]||matrix[currentY+1][currentX]==undefined){
- matrix[currentY+1][currentX]=matrix[currentY][currentX]+1
- numbers.push([currentX,currentY+1])
- }
- }
- // left
- if(currentX>0&¤tX<sideX&¤tY>=0&¤tY<sideY){
- if(matrix[currentY][currentX]<matrix[currentY][currentX-1]||matrix[currentY][currentX-1]==undefined){
- matrix[currentY][currentX-1]=matrix[currentY][currentX]+1
- numbers.push([currentX-1,currentY])
- }
- }
- // up
- if(currentY<sideY&¤tY>0&¤tX>=0&¤tX<=sideX){
- if(matrix[currentY][currentX]<matrix[currentY-1][currentX]||matrix[currentY-1][currentX]==undefined){
- matrix[currentY-1][currentX]=matrix[currentY][currentX]+1
- numbers.push([currentX,currentY-1])
- }
- }
- // diagonal up right
- if(currentX<sideX-1&¤tX>=0&¤tY>0¤tY<sideY){
- if(matrix[currentY][currentX]<matrix[currentY-1][currentX+1]||matrix[currentY-1][currentX+1]==undefined){
- matrix[currentY-1][currentX+1]=matrix[currentY][currentX]+1
- numbers.push([currentX+1,currentY-1])
- }
- }
- //diagonal up left
- if(currentX>0&¤tX<sideX&¤tY>0&¤tY<sideY){
- if(matrix[currentY][currentX]<matrix[currentY-1][currentX-1]||matrix[currentY-1][currentX-1]==undefined){
- matrix[currentY-1][currentX-1]=matrix[currentY][currentX]+1
- numbers.push([currentX-1,currentY-1])
- }
- }
- //diagonal down left
- if(currentX>0&¤tX<sideX&¤tY>=0&¤tY<sideY-1){
- if(matrix[currentY][currentX]<matrix[currentY+1][currentX-1]||matrix[currentY+1][currentX-1]==undefined){
- matrix[currentY+1][currentX-1]=matrix[currentY][currentX]+1
- numbers.push([currentX-1,currentY+1])
- }
- }
- // diagonal down right
- if(currentX<sideX-1&¤tX>=0&¤tY>=0¤tY<sideY-1){
- if(matrix[currentY][currentX]<matrix[currentY+1][currentX+1]||matrix[currentY+1][currentX+1]==undefined){
- matrix[currentY+1][currentX+1]=matrix[currentY][currentX]+1
- numbers.push([currentX+1,currentY+1])
- }
- }
- }
- matrix.map(x=>console.log(x.join(' ')))
- }
- Tenth([3, 3, 2, 2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement