Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let N = +gets();
- let matrix = [];
- for(let i = 0; i <= N-1;i++){
- matrix.push([]);
- for (let j = 0;j <= N-1;j++){
- matrix[i][j] = 0
- }
- }
- let row = 0
- let col = 0
- console.table(matrix)
- //празна матрица
- let counter = 1;
- let maxCounter = N * N + 1;
- let up = 'up';
- let down = 'down';
- let right = 'right';
- let left = 'left'
- let direction = down;
- while (counter < maxCounter){
- if (direction === down){
- if(row <= N-1 && matrix[row][col] === 0){
- matrix[row][col] = counter;
- row++;
- }else {
- counter--;
- row--;
- col++;
- direction = right;
- }
- } else if (direction === right){
- if(col <= N-1 && matrix[row][col] === 0){
- matrix[row][col] = counter;
- col++;
- } else {
- counter--;
- col--;
- row--;
- direction = up;
- }
- } else if (direction === up){
- if(row >= 0 && matrix[row][col] === 0){
- matrix[row][col] = counter;
- row--;
- } else {
- counter--;
- row++;
- col--;
- direction = left;
- }
- } else if (direction === left){
- if (col >= 0 && matrix[row][col] === 0){
- matrix[row][col] = counter;
- col--;
- } else {
- direction = down;
- row++
- col++
- counter--
- }
- }
- counter++
- }
- for (lines of matrix){
- console.log( lines.join(' '))
- }
Advertisement
Add Comment
Please, Sign In to add comment