momo3141

Untitled

Jan 5th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let N = +gets();
  2. let matrix = [];
  3. for(let i = 0; i <= N-1;i++){
  4.     matrix.push([]);
  5.     for (let j = 0;j <= N-1;j++){
  6.         matrix[i][j] = 0
  7.     }
  8. }
  9. let row = 0
  10. let col = 0
  11. console.table(matrix)
  12. //празна матрица
  13. let counter = 1;
  14. let maxCounter = N * N + 1;
  15. let up = 'up';
  16. let down = 'down';
  17. let right = 'right';
  18. let left = 'left'
  19. let direction = down;
  20. while (counter < maxCounter){
  21.     if (direction === down){
  22.         if(row <= N-1 && matrix[row][col] === 0){
  23.             matrix[row][col] = counter;
  24.             row++;
  25.         }else {
  26.             counter--;
  27.             row--;
  28.             col++;
  29.             direction = right;
  30.         }
  31.     } else if (direction === right){
  32.         if(col <= N-1 && matrix[row][col] === 0){
  33.             matrix[row][col] = counter;
  34.             col++;
  35.         } else {
  36.             counter--;
  37.             col--;
  38.             row--;
  39.             direction = up;
  40.         }
  41.     } else if (direction === up){
  42.         if(row >= 0 && matrix[row][col] === 0){
  43.             matrix[row][col] = counter;
  44.             row--;
  45.         } else {
  46.             counter--;
  47.             row++;
  48.             col--;
  49.             direction = left;
  50.         }
  51.     } else if (direction === left){
  52.         if (col >= 0 && matrix[row][col] === 0){
  53.             matrix[row][col] = counter;
  54.             col--;
  55.         } else {
  56.             direction = down;
  57.             row++
  58.             col++
  59.             counter--
  60.         }
  61.     }
  62.    
  63.  
  64.  
  65.     counter++
  66. }
  67. for (lines of matrix){
  68.     console.log( lines.join(' '))
  69. }
  70.  
Tags: Matrix
Advertisement
Add Comment
Please, Sign In to add comment