Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arg1,arg2){
- let end = arg1*arg2;
- let matrix = [];
- for(let i = 0; i < arg1; i++) {
- matrix[i] = [];
- for(let j = 0; j < arg2; j++){
- matrix[i][j] = 0;
- }
- }
- let stepRow = [0,1,0,-1];
- let stepCol = [1,0,-1,0];
- let indexChange = 0;
- let index = indexChange%4;
- let row = 0;
- let col = 0;
- function isInvalid(){
- if(row < 0 || row > arg1 - 1 || col < 0 || col > arg2 - 1){
- return true;
- }
- if(row >=0 && row <= arg1 - 1 && col >= 0 && col <= arg2 - 1){
- if(matrix[row][col] !== 0){
- return true;
- }
- }
- }
- for(let i = 1; i <= end; i++){
- matrix[row][col] = i;
- if(i === end){break;}
- row += stepRow[index];
- col += stepCol[index];
- if(isInvalid()){
- row -= stepRow[index];
- col -= stepCol[index];
- indexChange++;
- index = indexChange%4;
- row += stepRow[index];
- col += stepCol[index];
- }
- }
- console.log(matrix.map(r => r.join(' ')).join('\n'));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement